Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message_for_resource_action #2866

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/controllers/alchemy/admin/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,28 @@ def eligible_resource_filter_values
resource_filters.map(&:values).flatten
end

# Returns a translated +flash[:notice]+.
# The key should look like "Modelname successfully created|updated|destroyed."
def flash_notice_for_resource_action(action = params[:action])
# Returns a translated +flash[:notice]+ for current controller action.
def flash_notice_for_resource_action(action = action_name)
return if resource_instance_variable.errors.any?

flash[:notice] = message_for_resource_action(action)
end

# Returns a translated message for a +flash[:notice]+.
# The key should look like "Modelname successfully created|updated|destroyed."
def message_for_resource_action(action = action_name)
case action.to_sym
when :create
verb = "created"
verb = Alchemy.t("created", scope: "resources.actions")
when :update
verb = "updated"
verb = Alchemy.t("updated", scope: "resources.actions")
when :destroy
verb = "removed"
verb = Alchemy.t("removed", scope: "resources.actions")
end
flash[:notice] = Alchemy.t(
"#{resource_handler.resource_name.classify} successfully #{verb}",
default: Alchemy.t("Successfully #{verb}")
)
Alchemy.t("%{resource_name} successfully %{action}",
resource_name: resource_handler.model.model_name.human,
action: verb,
default: Alchemy.t("Successfully #{verb}"))
end

def is_alchemy_module?
Expand Down
5 changes: 5 additions & 0 deletions config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,12 @@ en:
replace: replace
replace_file: Replace file
"Replaced Tag": "Tag '%{old_tag}' was replaced with '%{new_tag}'"
"%{resource_name} successfully %{action}": "%{resource_name} successfully %{action}."
resources:
actions:
created: "created"
updated: "updated"
removed: "removed"
relation_select:
blank: "- none -"
right: "right"
Expand Down
8 changes: 4 additions & 4 deletions spec/features/admin/resources_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
end

it "shows a success message" do
expect(page).to have_content("Successfully created")
expect(page).to have_content("Event successfully created.")
end
end

Expand All @@ -246,7 +246,7 @@
end

it "should not display success notice" do
expect(page).not_to have_content("successfully created")
expect(page).not_to have_content("Event successfully created.")
end
end
end
Expand All @@ -263,7 +263,7 @@
end

it "shows a success message" do
expect(page).to have_content("Successfully updated")
expect(page).to have_content("Event successfully updated.")
end
end

Expand All @@ -283,7 +283,7 @@
end

it "should display success message" do
expect(page).to have_content("Successfully removed")
expect(page).to have_content("Event successfully removed.")
end
end

Expand Down