From f3ccf31c5d308f64472ff23cc25aa7aa38c145ef Mon Sep 17 00:00:00 2001 From: Daniel Boggs Date: Mon, 28 Mar 2016 14:28:03 -0400 Subject: [PATCH] #46: add a navigation label override for models --- app/controllers/tolaria/resource_controller.rb | 6 +++--- app/views/admin/shared/_navigation.html.erb | 2 +- app/views/admin/tolaria_resource/_index_table.html.erb | 6 +++--- app/views/admin/tolaria_resource/_search_form.html.erb | 2 +- app/views/admin/tolaria_resource/_show_buttons.html.erb | 2 +- app/views/admin/tolaria_resource/edit.html.erb | 6 +++--- app/views/admin/tolaria_resource/index.html.erb | 4 ++-- app/views/admin/tolaria_resource/show.html.erb | 4 ++-- lib/tolaria/active_record.rb | 2 ++ lib/tolaria/managed_class.rb | 6 +++++- test/unit/managed_classes_test.rb | 4 ++++ 11 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/controllers/tolaria/resource_controller.rb b/app/controllers/tolaria/resource_controller.rb index 7aa7fdf..85249e9 100644 --- a/app/controllers/tolaria/resource_controller.rb +++ b/app/controllers/tolaria/resource_controller.rb @@ -32,7 +32,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! @@ -54,7 +54,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! @@ -76,7 +76,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 510cbbf..9eb5098 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 d860adc..17d334d 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| %> @@ -18,9 +18,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:params[:q]) %> + <%= link_to @managed_class.navigation_label, url_for(action:"index", controller:@managed_class.plural, q:params[:q]) %> <% 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 a8c7b64..f6dc86a 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:params[:q]), 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 dce5e6d..1846a79 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:params[:q]) %> + <%= link_to @managed_class.navigation_label, url_for(action:"index", controller:@managed_class.plural, q:params[:q]) %> <% 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"