Sha256: dae5bded891cdf8894e167b4f0f5cff368b646b26c13b3f69355a94e45ec8eb8

Contents?: true

Size: 903 Bytes

Versions: 1

Compression:

Stored size: 903 Bytes

Contents

module Enumitation
  module ClassMethods

    def self.extended(base)
      # If we've already been extended, don't do it again
      return if defined? base.enumitation_values

      class << base
        attr_accessor :enumitation_values
      end

      base.enumitation_values = {}
    end

    def select_options_for(attribute)
      return [] if enumitation_values.empty?

      enumitation_values[attribute].map do |val|
        [display_value(attribute, val), val]
      end
    end

    private

    def display_value(attribute, val)
      # Try looking up using i18n.  If nothing found, just return the val.

      I18n.t(val,
             :scope => "enumitation.models.#{self.name.underscore}.#{attribute.to_s.underscore}",
             :default => val)
    end

    def add_inclusion_validation(attribute, values)
      self.validates_inclusion_of attribute, :in => values
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enumitation-0.0.3 lib/enumitation/class_methods.rb