Sha256: 721a1c9489defef79aa4176e561efd793b37fa5ba19e9ca8ffdebf932bc3a8b5
Contents?: true
Size: 1.35 KB
Versions: 1
Compression:
Stored size: 1.35 KB
Contents
# frozen_string_literal: true module Voting class Vote < ActiveRecord::Base self.table_name = 'voting_votes' after_save :update_voting belongs_to :author, polymorphic: true belongs_to :resource, polymorphic: true belongs_to :scopeable, polymorphic: true validates :author, :negative, :positive, :resource, presence: true validates :negative, :positive, inclusion: { in: [0, 1] } validates :author_id, uniqueness: { case_sensitive: false, scope: %i[author_type resource_id resource_type scopeable_id scopeable_type] } def status return 'positive' if positive == 1 negative == 1 ? 'negative' : 'none' end def self.create(author:, resource:, scopeable: nil, value:) 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 def self.vote_for(author:, resource:, scopeable: nil) find_by author: author, resource: resource, scopeable: scopeable end private def update_voting ::Voting::Voting.update_voting resource, scopeable end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
voting-0.3.0 | lib/voting/models/voting/vote.rb |