Sha256: 27f7da5d7caaf77f3f349571ac6154855132844dd395935518bc84a5f1867de5
Contents?: true
Size: 1.15 KB
Versions: 2
Compression:
Stored size: 1.15 KB
Contents
class ActiveRecord::Base def self.relationship_with(model) association = reflect_on_association(model.table_name.to_sym) || reflect_on_association(model.model_name.underscore.to_sym) association.macro end #-- # >> Post.to_resource # => "posts" # >> Admin::User.to_resource # => "admin/users" #++ def self.to_resource name.underscore.pluralize end #-- # On a model: # # class Post < ActiveRecord::Base # STATUS = { t("Published") => "published", # t("Pending") => "pending", # t("Draft") => "draft" } # 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::const_get(attribute.to_s.upcase).to_a array = values.first.is_a?(Array) ? values : values.map { |i| [i, i] } array.rassoc(send(attribute)).first end def to_label respond_to?(:name) ? send(:name) : [self.class, id].join("#") end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
typus-3.0.6 | lib/support/active_record.rb |
typus-3.0.5 | lib/support/active_record.rb |