Class: Voting::Vote

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/voting/models/voting/vote.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(author:, resource:, scopeable: nil, value:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/voting/models/voting/vote.rb', line 27

def self.create(author:, resource:, scopeable: nil, value:)
  value     = value.to_i
  record    = find_or_initialize_by(author: author, resource: resource, scopeable: scopeable)
  attribute = value.positive? ? :positive : :negative
  canceled  = record.persisted? && value.abs == record[attribute]

  record.negative   = 0
  record.positive   = 0
  record[attribute] = value.abs unless canceled

  record.save!

  record
end

.vote_for(author:, resource:, scopeable: nil) ⇒ Object



42
43
44
# File 'lib/voting/models/voting/vote.rb', line 42

def self.vote_for(author:, resource:, scopeable: nil)
  find_by author: author, resource: resource, scopeable: scopeable
end

Instance Method Details

#statusObject



21
22
23
24
25
# File 'lib/voting/models/voting/vote.rb', line 21

def status
  return 'positive' if positive == 1

  negative == 1 ? 'negative' : 'none'
end