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

Allow translating resource names in CRUD messages #1626

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
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