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

Version Path
brainsome_typus-4.0.0.beta13 lib/support/active_record.rb
brainsome_typus-4.0.0.beta12 lib/support/active_record.rb
brainsome_typus-4.0.0.beta11 lib/support/active_record.rb
brainsome_typus-4.0.0.beta10 lib/support/active_record.rb
brainsome_typus-4.0.0.beta9 lib/support/active_record.rb
brainsome_typus-4.0.0.beta8 lib/support/active_record.rb
brainsome_typus-4.0.0.beta7 lib/support/active_record.rb
brainsome_typus-4.0.0.beta6 lib/support/active_record.rb
brainsome_typus-4.0.0.beta5 lib/support/active_record.rb
brainsome_typus-4.0.0.beta4 lib/support/active_record.rb
brainsome_typus-4.0.0.beta3 lib/support/active_record.rb
brainsome_typus-4.0.0.beta2 lib/support/active_record.rb
brainsome_typus-4.0.0.beta1 lib/support/active_record.rb