Skip to content

Commit

Permalink
refactor Labelable to allow some of its code to be called directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccabot committed Jan 24, 2011
1 parent 3cdfabd commit 72299e5
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/rails_admin/config/labelable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ module RailsAdmin
module Config
# Defines a generic label/name/title configuration
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
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
if bindings[:object].respond_to?(:name) && bindings[:object].name
bindings[:object].name
elsif bindings[:object].respond_to?(:title) && bindings[:object].title
bindings[:object].title
else
"#{bindings[:object].class.to_s} ##{bindings[:object].id}"
end
RailsAdmin::Config::Labelable.object_label bindings[:object]
end
end
end
Expand Down

0 comments on commit 72299e5

Please sign in to comment.