Sha256: 4bcf5ed56c7336eafb3231d1e810bce66aea6da347bb9a341384d15bfc977699
Contents?: true
Size: 918 Bytes
Versions: 13
Compression:
Stored size: 918 Bytes
Contents
class ActiveRecord::Base #-- # On a model: # # class Post < ActiveRecord::Base # def self.statuses # { t("Published") => "published", # t("Pending") => "pending", # t("Draft") => "draft" } # end # end # # >> Post.first.status # => "published" # >> Post.first.mapping(:status) # => "Published" # >> I18n.locale = :es # => :es # >> Post.first.mapping(:status) # => "Publicado" #++ def mapping(attribute) values = self.class.send(attribute.to_s.pluralize) array = values.first.is_a?(Array) ? values : values.map { |i| [i, i] } value = array.to_a.rassoc(send(attribute)) value ? value.first : send(attribute) end def to_label if respond_to?(:name) && name.present? name else [self.class, id].join("#") end end def identifier "##{id}" end end
Version data entries
13 entries across 13 versions & 1 rubygems