Skip to content

Commit

Permalink
Allow translating resource names in CRUD messages
Browse files Browse the repository at this point in the history
Fixes #1242
  • Loading branch information
betelgeuse committed Apr 28, 2020
1 parent 043e7c6 commit d5cc51c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 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"
},
},
}

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"
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 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 @@ -55,7 +55,7 @@
click_on t("administrate.actions.destroy")

expect(page).to have_flash(
t("administrate.controller.destroy.success", resource: "LogEntry"),
t("administrate.controller.destroy.success", resource: "Log entry"),
)
end
end
26 changes: 25 additions & 1 deletion spec/lib/administrate/resource_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "spec_helper"
require "active_support/core_ext/string/inflections"
require "active_record"
require "support/constant_helpers"
require "administrate/resource_resolver"
require "support/i18n"

describe Administrate::ResourceResolver do
describe "#dashboard_class" do
Expand Down Expand Up @@ -61,17 +63,39 @@ module Library; class Book; end; end
end

describe "#resource_title" do
# rubocop:disable Rails/ApplicationRecord
it "handles global-namepsace models" do
class User < ActiveRecord::Base; 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 < ActiveRecord::Base; 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
# rubocop:enable Rails/ApplicationRecord
end

describe "#resource_name" do
Expand Down
2 changes: 0 additions & 2 deletions spec/support/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
RSpec.configure do |config|
config.include AbstractController::Translation

def with_translations(locale, translations)
original_backend = I18n.backend

Expand Down
3 changes: 3 additions & 0 deletions spec/support/rails_i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include AbstractController::Translation
end

0 comments on commit d5cc51c

Please sign in to comment.