Sha256: d376715bde74d35dbf6d41a9b43026c85acc667c169fd1c9f495fcea086908e7
Contents?: true
Size: 1.3 KB
Versions: 1
Compression:
Stored size: 1.3 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.downcase.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) if values.is_a?(Array) case values.first when Array array_keys, array_values = values.transpose else array_keys = array_values = values end values = array_keys.to_hash_with(array_values) end values.invert[send(attribute)] end def to_label respond_to?(:name) ? send(:name) : [self.class, id].join("#") end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
typus-3.0.4 | lib/support/active_record.rb |