class Voting::Vote

Public Class Methods

create(author:, resource:, scopeable: nil, value:) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

status() click to toggle source
# File lib/voting/models/voting/vote.rb, line 21
def status
  return 'positive' if positive == 1

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

Private Instance Methods

update_voting() click to toggle source
# File lib/voting/models/voting/vote.rb, line 48
def update_voting
  ::Voting::Voting.update_voting resource, scopeable
end