Module: Outpost::Model::Naming
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/outpost/model/naming.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #simple_title ⇒ Object
-
#title_method ⇒ Object
Convert any AR object into a human-readable title Tries the attributes in config.title_attributes And falls back to “BlogEntry #99”.
- #to_title ⇒ Object
Instance Method Details
#simple_title ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/outpost/model/naming.rb', line 55 def simple_title @simple_title ||= begin if self.new_record? "New #{self.class.to_title}" else "#{self.class.to_title} ##{self.id}" end end end |
#title_method ⇒ Object
Convert any AR object into a human-readable title Tries the attributes in config.title_attributes And falls back to “BlogEntry #99”
This allows us to get a human-readable title regardless of what an object’s “title” attribute happens to be.
To define your own set of attributes, do so with the config
Outpost.config.title_attributes = [:title, :full_name]
The :simple_title method will automatically be added to the array and acts as the fallback.
Usage:
story = NewsStory.last
#=> NewsStory(id: 900, title: "Cool Story, Bro")
blog = Blog.last
#=> Blog(id: 5, name: "Some Blog")
photo = Photo.last
#=> Photo(id: 10, url: "http://photos.com/kitty")
story.to_title #=> "Cool Story, Bro"
blog.to_title #=> "Some Blog"
photo.to_title #=> "Photo #10"
44 45 46 47 48 49 |
# File 'lib/outpost/model/naming.rb', line 44 def title_method @title_method ||= begin attributes = Outpost.config.title_attributes attributes.find { |a| self.respond_to?(a) } end end |
#to_title ⇒ Object
51 52 53 |
# File 'lib/outpost/model/naming.rb', line 51 def to_title @to_title ||= self.send(self.title_method) end |