Skip to content
Browse files

Add config.label_methods: defaults to [:name, :title]

This allows overriding the methods to look for when searching a label for
a record brought by a belongs_to association.
  • Loading branch information...
1 parent a7b2f32 commit 5ebc1819836a8ea21fe6ddae1ea38f071f7e49b4 @rosenfeld rosenfeld committed Jan 25, 2011
Showing with 11 additions and 11 deletions.
  1. +3 −1 README.mkd
  2. +5 −0 lib/rails_admin/config.rb
  3. +3 −10 lib/rails_admin/config/labelable.rb
View
4 README.mkd
@@ -414,7 +414,9 @@ 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.
+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
View
5 lib/rails_admin/config.rb
@@ -15,6 +15,11 @@ module Config
@@excluded_models = []
mattr_accessor :excluded_models
+ # Configuration option to specify which method names will be searched for
+ # to be used as a label for object records. This defaults to [:name, :title]
+ mattr_accessor :label_methods
+ self.label_methods = [:name, :title]
+
# Shortcut to access the list section's class configuration
# within a config DSL block
#
View
13 lib/rails_admin/config/labelable.rb
@@ -5,22 +5,15 @@ module Labelable
# Try to find a user-friendly label for an object, falling back
# to its class and ID.
def self.object_label(object)
- if object.respond_to?(:name) && object.name
- object.name
- elsif object.respond_to?(:title) && object.title
- object.title
- else
- "#{object.class.to_s} ##{object.try(:id)}"
- end
+ Config.label_methods.each {|l| return object.send l if object.respond_to? l}
+ "#{object.class.to_s} ##{object.try :id}"
end
def self.included(klass)
klass.register_instance_option(:label) do
abstract_model.model.model_name.human(:default => abstract_model.model.model_name.titleize)
end
- klass.register_instance_option(:object_label) do
- RailsAdmin::Config::Labelable.object_label bindings[:object]
- end
+ klass.register_instance_option(:object_label) {Labelable.object_label bindings[:object]}
end
end
end

0 comments on commit 5ebc181

Please sign in to comment.
Something went wrong with that request. Please try again.