Skip to content

Commit

Permalink
Allow translating resource names in flashes (#1626)
Browse files Browse the repository at this point in the history
Fixes #1242
  • Loading branch information
betelgeuse authored Oct 6, 2020
1 parent 3a7e887 commit 5a7ddca
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/administrate/resource_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def resource_name
end

def resource_title
model_path_parts.join(" ")
resource_class.model_name.human
end

private
Expand Down
20 changes: 15 additions & 5 deletions spec/features/edit_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,23 @@
new_email = "example@example.com"
customer = create(:customer)

visit edit_admin_customer_path(customer)
fill_in "Name", with: new_name
fill_in "Email", with: new_email
click_on "Update Customer"
translations = {
activerecord: {
models: {
customer: "Custom name",
},
},
}

with_translations(:en, translations) do
visit edit_admin_customer_path(customer)
fill_in "Name", with: new_name
fill_in "Email", with: new_email
click_on "Update Custom name"
end

expect(page).to have_text(new_name)
expect(page).to have_text(new_email)
expect(page).to have_flash("Customer was successfully updated.")
expect(page).to have_flash("Custom name was successfully updated.")
end
end
2 changes: 1 addition & 1 deletion spec/features/log_entries_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

expect(page).to have_link(customer.name)
expect(page).to have_flash(
t("administrate.controller.create.success", resource: "LogEntry"),
t("administrate.controller.create.success", resource: "Log entry"),
)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/features/log_entries_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
click_on t("administrate.actions.destroy")
end
expect(page).to have_flash(
t("administrate.controller.destroy.success", resource: "LogEntry"),
t("administrate.controller.destroy.success", resource: "Log entry"),
)
end
end
27 changes: 22 additions & 5 deletions spec/lib/administrate/resource_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
require "spec_helper"
require "active_support/core_ext/string/inflections"
require "support/constant_helpers"
require "administrate/resource_resolver"
require "rails_helper"

describe Administrate::ResourceResolver do
describe "#dashboard_class" do
Expand Down Expand Up @@ -62,15 +59,35 @@ module Library; class Book; end; end

describe "#resource_title" do
it "handles global-namepsace models" do
class User < ApplicationRecord; self.abstract_class = true; end
resolver = Administrate::ResourceResolver.new("admin/users")

expect(resolver.resource_title).to eq("User")
ensure
remove_constants :User
end

it "handles namespaced models" do
module Library
class Book < ApplicationRecord; self.abstract_class = true; end
end
resolver = Administrate::ResourceResolver.new("admin/library/books")

expect(resolver.resource_title).to eq("Library Book")
expect(resolver.resource_title).to eq("Book")

translations = {
activerecord: {
models: {
"library/book": "Library Book",
},
},
}

with_translations(:en, translations) do
expect(resolver.resource_title).to eq("Library Book")
end
ensure
remove_constants :Library
end
end

Expand Down

0 comments on commit 5a7ddca

Please sign in to comment.