Module: BallotBox::Voting::ClassMethods

Defined in:
lib/ballot_box/voting.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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
    # Associations
    belongs_to :voteable, :polymorphic => true
    belongs_to :voter, :polymorphic => true
    
    # Validations
    validates_presence_of :ip_address, :user_agent, :voteable_id, :voteable_type
    validates_numericality_of :value, :only_integer => true
    
    # Callbacks
    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 = ? AND voteable_type = ?", record.id, record.class.name]) }
  end
end

Instance Method Details

#chart(mode) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/ballot_box/voting.rb', line 40

def chart(mode)
  result = case mode.to_s.downcase
    when 'dates' then chart_dates
    when 'browsers' then chart_browsers
    when 'platforms' then chart_platforms
  end
  
  [ result ].to_json
end