Sha256: f0f2b88348d8671f3a48d5ee8acd1703c7f25f51995b736d32ddbf6a34dbac67

Contents?: true

Size: 972 Bytes

Versions: 4

Compression:

Stored size: 972 Bytes

Contents

# Models the pattern of a string attribute that has a fixed number of allowed choices with additional human labels.
# origin: RM
module ChoiceTrait

  as_trait do |field, options|

    options[:choices] = options[:choices].zip(options[:choices]).flatten if options[:no_hash]
    choices = ActiveSupport::OrderedHash[*options[:choices]]

    available_values = "available_#{field.to_s.pluralize}"

    metaclass ||= singleton_class
    metaclass.send(:define_method, available_values) do
      choices
    end

    validates_inclusion_of field.to_sym, :in => send(available_values).keys, :allow_blank => !!options[:allow_blank]

    if options.has_key?(:default)
      has_defaults field.to_sym => options[:default]
    end

    define_method "humanized_#{field}" do
      choices[send(field)]
    end

    if options[:query_methods]
      choices.keys.each do |key|
        define_method "#{key}?" do
          send(field) == key
        end
      end
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
serum-rails-0.2.1 spec/test_apps/rails-2-3/app/models/shared/choice_trait.rb
serum-rails-0.2.0 spec/test_apps/rails-2-3/app/models/shared/choice_trait.rb
serum-rails-0.1.1 spec/test_app/app/models/shared/choice_trait.rb
serum-rails-0.1.0 spec/test_app/app/models/shared/choice_trait.rb