13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/ballot_box/voting.rb', line 13
def self.extended(base)
base.class_eval do
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
validates_presence_of :ip_address, :user_agent, :voteable_id, :voteable_type
validates_numericality_of :value, :only_integer => true
before_create :parse_browser
after_save :update_cached_columns, :if => :refresh?
after_destroy :update_cached_columns, :if => :refresh?
attr_accessible :request, :ip_address, :user_agent
composed_of :ip,
:class_name => 'IPAddr',
:mapping => %w(ip_address to_i),
:constructor => Proc.new { |ip_address| IPAddr.new(ip_address, Socket::AF_INET) },
:converter => Proc.new { |value| value.is_a?(Integer) ? IPAddr.new(value, Socket::AF_INET) : IPAddr.new(value.to_s) }
scope :with_voteable, lambda { |record| where(:voteable_id => record.id, :voteable_type => record.class.name) }
end
end
|