Sha256: eeebd750634ef25ac24626809c25cd2a41345cfcd50e6678e715bdb26fe587b0

Contents?: true

Size: 722 Bytes

Versions: 10

Compression:

Stored size: 722 Bytes

Contents

module Neutral
  class Voting < ActiveRecord::Base
    before_destroy :delete_votes

    belongs_to :votingable, polymorphic: true

    delegate :votes, to: :votingable

    def difference
      positive - negative
    end

    def self.init(vote)
      create(votingable_type: vote.voteable_type, votingable_id: vote.voteable_id) do |voting|
        voting.increment(vote.nature)
      end
    end

    def add_to_existing(nature)
      increment!(nature)
    end

    def edit(nature)
      increment(nature)
      decrement(nature==:positive ? :negative : :positive)
      save
    end

    def remove(nature)
      decrement!(nature)
    end

    private

    def delete_votes
      votes.delete_all
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
neutral-0.0.10 app/models/neutral/voting.rb
neutral-0.0.9 app/models/neutral/voting.rb
neutral-0.0.8 app/models/neutral/voting.rb
neutral-0.0.7 app/models/neutral/voting.rb
neutral-0.0.6 app/models/neutral/voting.rb
neutral-0.0.5 app/models/neutral/voting.rb
neutral-0.0.4 app/models/neutral/voting.rb
neutral-0.0.3 app/models/neutral/voting.rb
neutral-0.0.2 app/models/neutral/voting.rb
neutral-0.0.1 app/models/neutral/voting.rb