Module: Zodiac::ActiveRecord::ClassMethods

Defined in:
lib/zodiac/activerecord.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#date_for_zodiacObject (readonly)

Returns the value of attribute date_for_zodiac.



25
26
27
# File 'lib/zodiac/activerecord.rb', line 25

def date_for_zodiac
  @date_for_zodiac
end

#zodiac_sign_id_fieldObject (readonly)

Returns the value of attribute zodiac_sign_id_field.



25
26
27
# File 'lib/zodiac/activerecord.rb', line 25

def zodiac_sign_id_field
  @zodiac_sign_id_field
end

Instance Method Details

#zodiac_reader(dob_attribute, options = {:sign_id_attribute => :zodiac_sign_id}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zodiac/activerecord.rb', line 27

def zodiac_reader(dob_attribute, options = {:sign_id_attribute => :zodiac_sign_id})
  @date_for_zodiac = dob_attribute
  @zodiac_sign_id_field = options[:sign_id_attribute]
  
  # if the migration was applied, we should update the sign attribute before each save
  # and define some scopes
  if self.column_names.include?(@zodiac_sign_id_field.to_s)
    self.before_save do |object|
      object.send(:update_sign_id)
    end
    
    # Person.by_zodiac(7 || :libra) == Person.where(:zodiac_sign_id => 7)
    scope :by_zodiac, lambda { |sign|
      case sign
      when Symbol
        where(self.zodiac_sign_id_field => Zodiac::Finder::SIGN_IDS[sign])
      when Fixnum
        where(self.zodiac_sign_id_field => sign)
      else
        raise ArgumentError, "Invalid attribute type #{sign.class} for #{self}.by_zodiac"
      end
    }
    
    # Person.gemini == Person.by_zodiac(3)
    Zodiac.each_sign do |symbol, integer|
      scope symbol, by_zodiac(integer)
    end
  end
end