Sha256: 94952a1c93917af9335eed21468efa6ba4cc523dca2fdac7d5ec3e55ee541410

Contents?: true

Size: 945 Bytes

Versions: 1

Compression:

Stored size: 945 Bytes

Contents

module Enumitation
  module ClassMethods

    def self.extended(base)
      class << base
        attr_accessor :enumitation_values
      end

      base.enumitation_values = {}
    end

    def enumitation(attribute, values)
      enumitation_values[attribute] = Array(values)
      add_inclusion_validation(attribute, 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.1 lib/enumitation/class_methods.rb