Skip to content

Commit

Permalink
Add message_for_resource_action
Browse files Browse the repository at this point in the history
Returns a translated message for a flash notice or json response message
without assigning it to `flash[:notice]`.
  • Loading branch information
tvdeyen committed May 9, 2024
1 parent 6c42bf4 commit f93b387
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
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

0 comments on commit f93b387

Please sign in to comment.