diff --git a/app/controllers/tolaria/resource_controller.rb b/app/controllers/tolaria/resource_controller.rb index f1d5a15..5f51231 100644 --- a/app/controllers/tolaria/resource_controller.rb +++ b/app/controllers/tolaria/resource_controller.rb @@ -31,7 +31,7 @@ def create display_name = Tolaria.display_name(@resource) if @resource.save - flash[:success] = "#{random_blingword} You created the #{@managed_class.model_name.human} “#{display_name}”." + flash[:success] = "#{random_blingword} You created the #{@managed_class.navigation_label.singularize} “#{display_name}”." return redirect_to form_completion_redirect_path(@managed_class, @resource) else log_validation_errors! @@ -53,7 +53,7 @@ def update display_name = Tolaria.display_name(@resource) if @resource.save - flash[:success] = "#{random_blingword} You updated the #{@managed_class.model_name.human.downcase} “#{display_name}”." + flash[:success] = "#{random_blingword} You updated the #{@managed_class.navigation_label.singularize.downcase} “#{display_name}”." return redirect_to form_completion_redirect_path(@managed_class, @resource) else log_validation_errors! @@ -75,7 +75,7 @@ def destroy return redirect_to form_completion_redirect_path(@managed_class, @resource) end - flash[:destructive] = "You deleted the #{@managed_class.model_name.human.downcase} “#{display_name}”." + flash[:destructive] = "You deleted the #{@managed_class.navigation_label.singularize.downcase} “#{display_name}”." return redirect_to form_completion_redirect_path(@managed_class) end diff --git a/app/views/admin/shared/_navigation.html.erb b/app/views/admin/shared/_navigation.html.erb index 6a41680..f23a1d9 100644 --- a/app/views/admin/shared/_navigation.html.erb +++ b/app/views/admin/shared/_navigation.html.erb @@ -9,7 +9,7 @@ <% if managed_class.allows?(:index) %>
  • <%= tolaria_navigation_link( - managed_class.model_name.human.pluralize.titleize, + managed_class.navigation_label, managed_class.icon, url_for(action:"index", controller:managed_class.plural) ) %> diff --git a/app/views/admin/tolaria_resource/_index_table.html.erb b/app/views/admin/tolaria_resource/_index_table.html.erb index f150f74..0337f03 100644 --- a/app/views/admin/tolaria_resource/_index_table.html.erb +++ b/app/views/admin/tolaria_resource/_index_table.html.erb @@ -5,13 +5,13 @@
    <%= fontawesome_icon @managed_class.icon %>

    - No <%= @managed_class.model_name.human.pluralize.titleize %> live here. + No <%= @managed_class.navigation_label %> live here.

    <% if @managed_class.allows?(:new) %>

    Get started on the first one:

    <%= link_to url_for(action:"new"), class:"button -primary" do %> <%= fontawesome_icon :plus %> - New <%= @managed_class.model_name.human.titleize %> + New <%= @managed_class.navigation_label.singularize %> <% end %> <% end %>
    @@ -23,7 +23,7 @@
    <%= fontawesome_icon :search %>

    - Your search didn't find any <%= @managed_class.model_name.human.pluralize.titleize %>. + Your search didn't find any <%= @managed_class.navigation_label %>.

    You can <%= link_to "clear the search form", url_for([:admin, @managed_class.klass]) %> diff --git a/app/views/admin/tolaria_resource/_search_form.html.erb b/app/views/admin/tolaria_resource/_search_form.html.erb index 716df7e..f55a4d5 100644 --- a/app/views/admin/tolaria_resource/_search_form.html.erb +++ b/app/views/admin/tolaria_resource/_search_form.html.erb @@ -19,7 +19,7 @@

    <%= f.button type:"submit", class:"button -primary", name:nil do %> <%= fontawesome_icon :search %> - Search <%= @managed_class.model_name.human.pluralize.titleize %> + Search <%= @managed_class.navigation_label %> <% end %> <%= link_to url_for([:admin, @managed_class.klass]), class:"button" do %> <%= fontawesome_icon :close %> diff --git a/app/views/admin/tolaria_resource/_show_buttons.html.erb b/app/views/admin/tolaria_resource/_show_buttons.html.erb index 693da8a..0f8ca01 100644 --- a/app/views/admin/tolaria_resource/_show_buttons.html.erb +++ b/app/views/admin/tolaria_resource/_show_buttons.html.erb @@ -8,6 +8,6 @@ <% if @managed_class.allows?(:edit) %> <%= link_to url_for(action:"edit", id:@resource.id), class:"button -primary" do %> <%= fontawesome_icon :pencil %> - Edit This <%= @managed_class.model_name.human.titleize %> + Edit This <%= @managed_class.navigation_label.singularize %> <% end %> <% end %> diff --git a/app/views/admin/tolaria_resource/edit.html.erb b/app/views/admin/tolaria_resource/edit.html.erb index 5328f5b..f832f3c 100644 --- a/app/views/admin/tolaria_resource/edit.html.erb +++ b/app/views/admin/tolaria_resource/edit.html.erb @@ -1,7 +1,7 @@ <% if @resource.persisted? %> <%= content_for :title, "#{Tolaria.display_name(@resource)}" %> <% else %> - <%= content_for :title, "New #{@managed_class.model_name.human.titleize}" %> + <%= content_for :title, "New #{@managed_class.navigation_label.singularize}" %> <% end %> <%= form_for [:admin, @resource], url:contextual_form_url, builder:Admin::FormBuilder, html:{class:"resource-form"} do |form_builder| %> @@ -27,9 +27,9 @@ <%= fontawesome_icon @managed_class.icon %> <% if @managed_class.allows? :index %> - <%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:ransack_params, p:page_param) %> + <%= link_to @managed_class.navigation_label, url_for(action:"index", controller:@managed_class.plural, q:ransack_params, p:page_param) %> <% else %> - <%= @managed_class.model_name.human.pluralize.titleize %> + <%= @managed_class.navigation_label %> <% end %> <%= content_for :title %> diff --git a/app/views/admin/tolaria_resource/index.html.erb b/app/views/admin/tolaria_resource/index.html.erb index 1a8a78f..934f635 100644 --- a/app/views/admin/tolaria_resource/index.html.erb +++ b/app/views/admin/tolaria_resource/index.html.erb @@ -1,4 +1,4 @@ -<%= content_for :title, "#{@managed_class.model_name.human.pluralize.titleize}" %> +<%= content_for :title, "#{@managed_class.navigation_label}" %>
    @@ -28,7 +28,7 @@ <% if @managed_class.allows?(:new) %> <%= link_to url_for(action:"new", q:ransack_params, p:page_param), class:"button -primary" do %> <%= fontawesome_icon :plus %> - New <%= @managed_class.model_name.human.titleize %> + New <%= @managed_class.navigation_label.singularize %> <% end %> <% end %> diff --git a/app/views/admin/tolaria_resource/show.html.erb b/app/views/admin/tolaria_resource/show.html.erb index ed8d432..e32feec 100644 --- a/app/views/admin/tolaria_resource/show.html.erb +++ b/app/views/admin/tolaria_resource/show.html.erb @@ -7,9 +7,9 @@ <%= fontawesome_icon @managed_class.icon %> <% if @managed_class.allows? :index %> - <%= link_to @managed_class.model_name.human.pluralize.titleize, url_for(action:"index", controller:@managed_class.plural, q:ransack_params, p:page_param) %> + <%= link_to @managed_class.navigation_label, url_for(action:"index", controller:@managed_class.plural, q:ransack_params, p:page_param) %> <% else %> - <%= @managed_class.model_name.human.pluralize.titleize %> + <%= @managed_class.navigation_label %> <% end %> <%= content_for :title %> diff --git a/lib/tolaria/active_record.rb b/lib/tolaria/active_record.rb index f6ae874..3216537 100644 --- a/lib/tolaria/active_record.rb +++ b/lib/tolaria/active_record.rb @@ -36,6 +36,8 @@ class ActiveRecord::Base # Tolaria will pass this array as the `only:` option to the router. # The default includes all CRUD actions: # `[:index, :show, :new, :create, :edit, :update, :destroy]` + # - `:navigation_label` - The navigation label to use for this resource. + # The default is `self.model_name.human.pluralize.titleize`. # # #### Example # diff --git a/lib/tolaria/managed_class.rb b/lib/tolaria/managed_class.rb index 973e74a..f9681f0 100644 --- a/lib/tolaria/managed_class.rb +++ b/lib/tolaria/managed_class.rb @@ -34,9 +34,12 @@ class ManagedClass # A stored symbol for the `params.permit` key for this resource attr_accessor :param_key + # A String to override the model's label in the primary admin navigation + attr_accessor :navigation_label + # A factory method that registers a new model in Tolaria and configures # its menu and param settings. Developers should use `ActiveRecord::Base.manage_with_tolaria` - def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"Settings", default_order:"id DESC", paginated:true, allowed_actions:[:index, :show, :new, :create, :edit, :update, :destroy]) + def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"Settings", default_order:"id DESC", paginated:true, allowed_actions:[:index, :show, :new, :create, :edit, :update, :destroy], navigation_label: klass.model_name.human.pluralize.titleize) managed_class = self.new managed_class.klass = klass @@ -49,6 +52,7 @@ def self.create(klass, icon:"file-o", permit_params:[], priority:10, category:"S managed_class.paginated = paginated.present? managed_class.permitted_params = permit_params.freeze managed_class.allowed_actions = allowed_actions.freeze + managed_class.navigation_label = navigation_label.freeze # Set auto-generated attributes managed_class.controller_name = "#{managed_class.model_name.collection.camelize}Controller".freeze diff --git a/test/unit/managed_classes_test.rb b/test/unit/managed_classes_test.rb index 1479c18..91fccdd 100644 --- a/test/unit/managed_classes_test.rb +++ b/test/unit/managed_classes_test.rb @@ -6,6 +6,7 @@ class ManagedClassesTest < ActiveSupport::TestCase class ::Card < ActiveRecord::Base manage_with_tolaria using:{ + navigation_label: "Widgets", icon: "credit-card", priority: 5, category: "Payments", @@ -32,6 +33,9 @@ class ::Card < ActiveRecord::Base assert_equal managed_class.allowed_actions, [:index, :show] assert managed_class.paginated, true + # Can we override the navigation label? + assert_equal managed_class.navigation_label, "Widgets" + # Can we check action allowances? assert managed_class.allows?(:index), "should allow `index` action" assert managed_class.allows?(:show), "should allow `show` action"