From 6d1684e0e6123d8c71e1717a67b14e457a6e16a3 Mon Sep 17 00:00:00 2001 From: mbrookes Date: Thu, 20 Nov 2014 03:43:09 +0000 Subject: [PATCH] Support for customized model name addressing #65 Enables 'display_name "Name"' in an AdminModel --- lib/upmin/model.rb | 8 +++++++- spec/features/edit_model_spec.rb | 2 +- spec/features/navbar_spec.rb | 8 ++++---- spec/features/new_model_spec.rb | 2 +- test_app_upmin/models/admin_user.rb | 2 ++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/upmin/model.rb b/lib/upmin/model.rb index d38ba33..fe204c0 100644 --- a/lib/upmin/model.rb +++ b/lib/upmin/model.rb @@ -170,10 +170,12 @@ def Model.model_name end def Model.humanized_name(type = :plural) - names = model_class_name.split(/(?=[A-Z])/).map{|n| n.gsub(":", "")} + names = @display_name ? [@display_name] : model_class_name.split(/(?=[A-Z])/).map{|n| n.gsub(":", "")} + if type == :plural names[names.length-1] = names.last.pluralize end + return names.join(" ") end @@ -286,6 +288,10 @@ def Model.items_per_page(items = Upmin.configuration.items_per_page) return @items_per_page ||= items end + def Model.display_name (name) + return @display_name ||= name + end + ########################################################### ### Methods that need to be to be overridden. If the diff --git a/spec/features/edit_model_spec.rb b/spec/features/edit_model_spec.rb index 2c9af6b..67d911f 100644 --- a/spec/features/edit_model_spec.rb +++ b/spec/features/edit_model_spec.rb @@ -41,7 +41,7 @@ click_button("Save") within(".alert.alert-danger") do - expect(page).to(have_content("User was NOT updated.")) + expect(page).to(have_content("Customer was NOT updated.")) expect(page).to(have_selector("li", text: /email/i)) end diff --git a/spec/features/navbar_spec.rb b/spec/features/navbar_spec.rb index 0855a1e..98c86a7 100644 --- a/spec/features/navbar_spec.rb +++ b/spec/features/navbar_spec.rb @@ -10,13 +10,13 @@ visit ("/upmin") within(".navbar ul.nav") do - expect(page).to(have_selector("li", text: "Users")) + expect(page).to(have_selector("li", text: "Customers")) expect(page).to(have_selector("li", text: "Products")) expect(page).to(have_selector("li", text: "Orders")) expect(page).to(have_selector("li", text: "Product Orders")) expect(page).to(have_selector("li", text: "Shipments")) - click_link("Users") + click_link("Customers") end expect(page).to(have_selector(".upmin-model", minimum: 10)) @@ -30,13 +30,13 @@ visit ("/upmin") within(".navbar ul.nav") do - expect(page).to(have_selector("li", text: "Users")) + expect(page).to(have_selector("li", text: "Customers")) expect(page).to(have_selector("li", text: "Products")) expect(page).not_to(have_selector("li", text: "Orders")) expect(page).not_to(have_selector("li", text: "Product Orders")) expect(page).not_to(have_selector("li", text: "Shipments")) - click_link("Users") + click_link("Customers") end expect(page).to(have_selector(".upmin-model", minimum: 10)) diff --git a/spec/features/new_model_spec.rb b/spec/features/new_model_spec.rb index ebc0567..4299672 100644 --- a/spec/features/new_model_spec.rb +++ b/spec/features/new_model_spec.rb @@ -34,7 +34,7 @@ expect { click_button("Create") }.not_to(change(User, :count)) within(".alert.alert-danger") do - expect(page).to(have_content("User was NOT created.")) + expect(page).to(have_content("Customer was NOT created.")) end within(".field_with_errors") do diff --git a/test_app_upmin/models/admin_user.rb b/test_app_upmin/models/admin_user.rb index e5455bd..0b9c74e 100644 --- a/test_app_upmin/models/admin_user.rb +++ b/test_app_upmin/models/admin_user.rb @@ -1,5 +1,7 @@ class AdminUser < Upmin::Model + display_name "Customer" + action :issue_coupon end