Sha256: 4227ede7841adbbebf4a14b164ea1a025a084e9a5ec69e0a562fc4b85c61a546

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 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

  #--
  # 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)
    klass = self.class
    values = klass.send(attribute.to_s.pluralize)

    array = values.first.is_a?(Array) ? values : values.map { |i| [i, i] }

    value = array.rassoc(send(attribute))
    value ? value.first : send(attribute)
  end

  def to_str
    if respond_to?(:name) && send(:name).present?
      send(:name)
    else
      [self.class, id].join("#")
    end
  end

  alias_method :to_label, :to_str

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typus-3.1.0.rc18 lib/support/active_record.rb