From c67eccc43d603d7aaabe0ab01de1fc2559f7ae35 Mon Sep 17 00:00:00 2001 From: Wolfram Arnold Date: Tue, 15 Mar 2011 17:45:12 -0700 Subject: [PATCH 1/3] Update the configuration of model label as per discussion here: https://github.com/sferik/rails_admin/issues/319/#issue/319/comment/875868 The model label is now configurable at the model level only, and is no longer configurable at the section level (list, navigate, update,...), as this was considered overkill. This refactoring was in part motivated by issue #319 which reported that display of labels was very inconsistent across various screens, and the label configuration, if given, was not consistently effective. The Labelable module was removed, and the methods model into config/model.rb All references to label across the code have been updated to use the model configuration. Specs updated and passing. Readme also updated accordingly. --- README.mkd | 83 ++++++++++--------- .../rails_admin/history_controller.rb | 4 +- .../rails_admin/main_controller.rb | 26 +++--- app/views/layouts/rails_admin/delete.html.erb | 2 +- app/views/layouts/rails_admin/form.html.erb | 2 +- app/views/layouts/rails_admin/list.html.erb | 4 +- .../rails_admin/main/_delete_notice.html.erb | 6 +- ...s_and_belongs_to_many_association.html.erb | 2 +- .../main/_has_many_association.html.erb | 2 +- .../rails_admin/main/_navigation.html.erb | 4 +- app/views/rails_admin/main/delete.html.erb | 2 +- app/views/rails_admin/main/index.html.erb | 2 +- app/views/rails_admin/main/list.html.erb | 2 +- lib/rails_admin/config/base.rb | 2 +- lib/rails_admin/config/fields/association.rb | 2 +- .../fields/types/belongs_to_association.rb | 4 +- lib/rails_admin/config/labelable.rb | 20 ----- lib/rails_admin/config/model.rb | 22 +++++ lib/rails_admin/config/sections.rb | 9 +- lib/rails_admin/config/sections/list.rb | 2 - lib/rails_admin/config/sections/navigation.rb | 4 +- lib/rails_admin/config/sections/update.rb | 2 - .../rails_admin_config_navigation_spec.rb | 36 -------- .../config/rails_admin_config_spec.rb | 15 +++- 24 files changed, 117 insertions(+), 142 deletions(-) delete mode 100644 lib/rails_admin/config/labelable.rb diff --git a/README.mkd b/README.mkd index dddd3a4198..c88e27e255 100644 --- a/README.mkd +++ b/README.mkd @@ -30,6 +30,15 @@ Help If you have a question, you can ask the [official RailsAdmin mailing list](http://groups.google.com/group/rails_admin) or ping sferik on IRC in [#railsadmin on irc.freenode.net](http://webchat.freenode.net/?channels=railsadmin). +API Update Note +--------------- +The ability to set model labels for each section (list, navigation, update, ...) has been removed, +as it was deemed unnecessarily granular and was not fully honored all displays. +That also means that the methods `label_for_navigation`, etc. are no longer functional. They print a warning at the moment. +See details in the examples below for the currently supported way to label models. +This change was motivated by the conversation following a [bug report](https://github.com/sferik/rails_admin/issues/319/#issue/319/comment/875868) +about label display errors. + Screenshots ----------- ![List view](https://github.com/sferik/rails_admin/raw/master/screenshots/list.png "List view") @@ -163,27 +172,51 @@ Both also accept a block: **Setting the model's label** -If you need to customize the label of the model within the navigation tab, use: +If you need to customize the label of the model, use: RailsAdmin.config do |config| config.model Team do - navigation do - label "List of teams" - end + label "List of teams" end end -Remember, you can pass the value as an argument or as a block as with the -before mentioned visibility options. Besides that, the label also has a -shorthand syntax: +This label will be used anywhere the model name is shown, e.g. on the navigation tabs, +Dashboard page, list pages, etc. + +**The object_label method** + +The model configuration has another option `object_label` which configures +the title display of a single database record. + +By default it queries if the record in question has columns named "name" or +"title". If neither is found it returns the model's classname appended with its +database identifier. You can add label methods (or replace the default [:name, :title]) with: + + RailsAdmin.config {|c| c.label_methods << :description} + +This value is used in a number of places in RailsAdmin - for instance as the +output of belongs to associations in the listing views of related models, as +the option labels of the relational fields' input widgets in the edit views of +related models and as part of the audit information stored in the history +records - so keep in mind that this configuration option has widespread +effects. RailsAdmin.config do |config| config.model Team do - label_for_navigation "List of teams" + object_label do + "#{bindings[:object].name} - #{bindings[:object].league.name}" + end end end -which allows both forms of configuration value passing as well. +This would output "Team's name - Team's league's name" in all the places +mentioned in paragraph above example. + +*Difference between `label` and `object_label`* + +`label` and `object_label` are both model configuration options. `label` is used +whenever Rails Admin refers to a model class, while `object_label` is used whenever +Rails Admin refers to an instance of a model class (representing a single database record). **To enable CKEditor for a field** @@ -230,7 +263,6 @@ evaluated at runtime. * Sortability * Column CSS class * Column width - * The object_label method **Number of items per page** @@ -456,37 +488,6 @@ column, you can: end end -**Fields - The object_label method** - -List section has a configuration option `object_label` which configures -the title of a single database record. - -By default it queries if the record in question has columns named "name" or -"title". If neither is found it returns the model's classname appended with its -database identifier. You can add label methods (or replace the default [:name, :title]) with: - - RailsAdmin.config {|c| c.label_methods << :description} - -This value is used in a number of places in RailsAdmin - for instance as the -output of belongs to associations in the listing views of related models, as -the option labels of the relational fields' input widgets in the edit views of -related models and as part of the audit information stored in the history -records - so keep in mind that this configuration option has widespread -effects. - - RailsAdmin.config do |config| - config.model Team do - list do - object_label do - "#{bindings[:object].name} - #{bindings[:object].league.name}" - end - end - end - end - -This would output "Team's name - Team's league's name" in all the places -mentioned in paragraph above example. - ### Create and update views * Field groupings diff --git a/app/controllers/rails_admin/history_controller.rb b/app/controllers/rails_admin/history_controller.rb index e643e835c4..a2ea8dd7de 100644 --- a/app/controllers/rails_admin/history_controller.rb +++ b/app/controllers/rails_admin/history_controller.rb @@ -24,7 +24,7 @@ def slider def for_model @page_type = @abstract_model.pretty_name.downcase - @page_name = t("admin.history.page_name", :name => @model_config.list.label) + @page_name = t("admin.history.page_name", :name => @model_config.label) @general = true @page_count, @history = AbstractHistory.history_for_model @abstract_model, params[:query], params[:sort], params[:sort_reverse], params[:all], params[:page] @@ -34,7 +34,7 @@ def for_model def for_object @page_type = @abstract_model.pretty_name.downcase - @page_name = t("admin.history.page_name", :name => @model_config.list.with(:object => @object).object_label) + @page_name = t("admin.history.page_name", :name => @model_config.with(:object => @object).object_label) @general = false @history = AbstractHistory.history_for_object @abstract_model, @object, params[:query], params[:sort], params[:sort_reverse] diff --git a/app/controllers/rails_admin/main_controller.rb b/app/controllers/rails_admin/main_controller.rb index 1284895547..8db9430eec 100644 --- a/app/controllers/rails_admin/main_controller.rb +++ b/app/controllers/rails_admin/main_controller.rb @@ -50,7 +50,7 @@ def new end @authorization_adapter.authorize(:new, @abstract_model, @object) end - @page_name = t("admin.actions.create").capitalize + " " + @model_config.create.label.downcase + @page_name = t("admin.actions.create").capitalize + " " + @model_config.label.downcase @page_type = @abstract_model.pretty_name.downcase render :layout => 'rails_admin/form' end @@ -66,11 +66,11 @@ def create end @object.attributes = @attributes @object.associations = params[:associations] - @page_name = t("admin.actions.create").capitalize + " " + @model_config.create.label.downcase + @page_name = t("admin.actions.create").capitalize + " " + @model_config.label.downcase @page_type = @abstract_model.pretty_name.downcase if @object.save - AbstractHistory.create_history_item("Created #{@model_config.list.with(:object => @object).object_label}", @object, @abstract_model, _current_user) + AbstractHistory.create_history_item("Created #{@model_config.with(:object => @object).object_label}", @object, @abstract_model, _current_user) redirect_to_on_success else render_error @@ -80,7 +80,7 @@ def create def edit @authorization_adapter.authorize(:edit, @abstract_model, @object) if @authorization_adapter - @page_name = t("admin.actions.update").capitalize + " " + @model_config.update.label.downcase + @page_name = t("admin.actions.update").capitalize + " " + @model_config.label.downcase @page_type = @abstract_model.pretty_name.downcase render :layout => 'rails_admin/form' @@ -92,7 +92,7 @@ def update @cached_assocations_hash = associations_hash @modified_assoc = [] - @page_name = t("admin.actions.update").capitalize + " " + @model_config.update.label.downcase + @page_name = t("admin.actions.update").capitalize + " " + @model_config.label.downcase @page_type = @abstract_model.pretty_name.downcase @old_object = @object.clone @@ -111,7 +111,7 @@ def update def delete @authorization_adapter.authorize(:delete, @abstract_model, @object) if @authorization_adapter - @page_name = t("admin.actions.delete").capitalize + " " + @model_config.list.label.downcase + @page_name = t("admin.actions.delete").capitalize + " " + @model_config.label.downcase @page_type = @abstract_model.pretty_name.downcase render :layout => 'rails_admin/delete' @@ -121,9 +121,9 @@ def destroy @authorization_adapter.authorize(:destroy, @abstract_model, @object) if @authorization_adapter @object = @object.destroy - flash[:notice] = t("admin.delete.flash_confirmation", :name => @model_config.list.label) + flash[:notice] = t("admin.delete.flash_confirmation", :name => @model_config.label) - AbstractHistory.create_history_item("Destroyed #{@model_config.list.with(:object => @object).object_label}", @object, @abstract_model, _current_user) + AbstractHistory.create_history_item("Destroyed #{@model_config.with(:object => @object).object_label}", @object, @abstract_model, _current_user) redirect_to rails_admin_list_path(:model_name => @abstract_model.to_param) end @@ -131,7 +131,7 @@ def destroy def bulk_delete @authorization_adapter.authorize(:bulk_delete, @abstract_model) if @authorization_adapter - @page_name = t("admin.actions.delete").capitalize + " " + @model_config.list.label.downcase + @page_name = t("admin.actions.delete").capitalize + " " + @model_config.label.downcase @page_type = @abstract_model.pretty_name.downcase render :layout => 'rails_admin/delete' @@ -144,7 +144,7 @@ def bulk_destroy @destroyed_objects = @abstract_model.destroy(params[:bulk_ids], scope) @destroyed_objects.each do |object| - message = "Destroyed #{@model_config.list.with(:object => object).object_label}" + message = "Destroyed #{@model_config.with(:object => object).object_label}" AbstractHistory.create_history_item(message, object, @abstract_model, _current_user) end @@ -241,7 +241,7 @@ def get_attributes def redirect_to_on_success param = @abstract_model.to_param - pretty_name = @model_config.update.label + pretty_name = @model_config.label action = params[:action] if params[:_add_another] @@ -258,7 +258,7 @@ def redirect_to_on_success def render_error whereto = :new action = params[:action] - flash.now[:error] = t("admin.flash.error", :name => @model_config.update.label, :action => t("admin.actions.#{action}d")) + flash.now[:error] = t("admin.flash.error", :name => @model_config.label, :action => t("admin.actions.#{action}d")) render whereto, :layout => 'rails_admin/form' end @@ -301,7 +301,7 @@ def list_entries(other = {}) @record_count = @abstract_model.count(options, scope) @page_type = @abstract_model.pretty_name.downcase - @page_name = t("admin.list.select", :name => @model_config.list.label.downcase) + @page_name = t("admin.list.select", :name => @model_config.label.downcase) end def associations_hash diff --git a/app/views/layouts/rails_admin/delete.html.erb b/app/views/layouts/rails_admin/delete.html.erb index 20a0c5319e..b8896a5734 100644 --- a/app/views/layouts/rails_admin/delete.html.erb +++ b/app/views/layouts/rails_admin/delete.html.erb @@ -20,7 +20,7 @@
  • › - <%= link_to(@model_config.list.label, rails_admin_list_path(:model_name => @abstract_model.to_param)) %> + <%= link_to(@model_config.label, rails_admin_list_path(:model_name => @abstract_model.to_param)) %>
  • › diff --git a/app/views/layouts/rails_admin/form.html.erb b/app/views/layouts/rails_admin/form.html.erb index 8beec5e9cb..5c2ec755e8 100644 --- a/app/views/layouts/rails_admin/form.html.erb +++ b/app/views/layouts/rails_admin/form.html.erb @@ -69,7 +69,7 @@
  • › - <%= link_to(@model_config.update.label, rails_admin_list_path(:model_name => @abstract_model.to_param)) %> + <%= link_to(@model_config.label, rails_admin_list_path(:model_name => @abstract_model.to_param)) %>
  • › diff --git a/app/views/layouts/rails_admin/list.html.erb b/app/views/layouts/rails_admin/list.html.erb index b90f856cf5..d5f1d3d574 100644 --- a/app/views/layouts/rails_admin/list.html.erb +++ b/app/views/layouts/rails_admin/list.html.erb @@ -43,7 +43,7 @@ <% if @history %>
  • › - <%= link_to(@model_config.list.label, rails_admin_list_path(:model_name => @abstract_model.to_param)) %> + <%= link_to(@model_config.label, rails_admin_list_path(:model_name => @abstract_model.to_param)) %>
  • › @@ -52,7 +52,7 @@ <% else %>
  • › - <%= @model_config.list.label %> + <%= @model_config.label %>
  • <% end%> diff --git a/app/views/rails_admin/main/_delete_notice.html.erb b/app/views/rails_admin/main/_delete_notice.html.erb index f9985237fc..70cbff3c8c 100644 --- a/app/views/rails_admin/main/_delete_notice.html.erb +++ b/app/views/rails_admin/main/_delete_notice.html.erb @@ -5,12 +5,12 @@ %>