Module: BallotBox::Voting::InstanceMethods

Defined in:
lib/ballot_box/voting.rb

Instance Method Summary collapse

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ballot_box/voting.rb', line 105

def anonymous?
  voter.nil?
end

#as_json(options = nil) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/ballot_box/voting.rb', line 109

def as_json(options = nil)
  options = { 
    :methods => [:ip], 
    :only => [:referrer, :value, :browser_version, :browser_name, :user_agent, :browser_platform ] 
  }.merge(options || {})
  
  super(options)
end

#browserObject



101
102
103
# File 'lib/ballot_box/voting.rb', line 101

def browser
  @browser ||= Browser.new(:ua => user_agent, :accept_language => "en-us")
end

#callObject



130
131
132
133
134
135
136
# File 'lib/ballot_box/voting.rb', line 130

def call
  if register
    [self.to_json, 200]
  else
    [self.errors.to_json, 422]
  end
end

#registerObject



138
139
140
141
142
143
144
145
146
# File 'lib/ballot_box/voting.rb', line 138

def register
  if voteable && voteable.ballot_box_valid?(self)
    voteable.current_vote = self
    
    voteable.run_callbacks(:vote) do
      errors.empty? && save
    end
  end
end

#requestObject



118
119
120
# File 'lib/ballot_box/voting.rb', line 118

def request
  @request
end

#request=(req) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/ballot_box/voting.rb', line 122

def request=(req)
  self.ip = req.ip
  self.user_agent = req.user_agent
  self.referrer = req.referer
  self.voteable_id = req.params["id"]
  @request = req
end