Sha256: 22200b1ad4dd5a66f62020ee3e4cf8fef6500616d2c2b758d06626926c9da010
Contents?: true
Size: 1.48 KB
Versions: 9
Compression:
Stored size: 1.48 KB
Contents
module HasConstant module Orm module ActiveRecord extend ActiveSupport::Concern module ClassMethods def has_constant( name, values, options = {} ) super(name, values, options) singular = (options[:accessor] || name.to_s.singularize).to_s # Add the getter method. This returns the string representation of the stored value define_method(singular) do self.class.send(name)[read_attribute(singular).to_i] if read_attribute(singular) end define_method("#{singular}=") do |val| if val.instance_of?(String) write_attribute singular.to_sym, self.class.send(name.to_s).index(val) else write_attribute singular.to_sym, val end end class_eval do named_scope :by_constant, lambda { |constant,value| { :conditions => { constant.to_sym => eval("#{self.to_s}.#{constant.pluralize}.index(value)") } } } named_scope "#{singular}_is".to_sym, lambda { |*values| { :conditions => { singular.to_sym => indexes_for(name, values) } } } named_scope "#{singular}_is_not".to_sym, lambda { |*values| { :conditions => ["#{singular} NOT IN (?)", indexes_for(name, values)] } } end end private def indexes_for( name, values ) values.map { |v| self.send(name.to_sym).index(v) } end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems