<commit_before>class Vote < ActiveRecord::Base
  LIKE = 1
  DISLIKE = -1

  belongs_to :proposal
  belongs_to :user

  validates_presence_of :direction
  validates_associated :user
  validates_associated :proposal

  validates_uniqueness_of :proposal_id, :scope  =>  :user_id

  scope :positives,  where(:direction => LIKE)
  scope :negatives,  where(:direction => DISLIKE)

  def self.like(proposal, user)
    vote = new
    vote.proposal = proposal
    vote.user = user
    vote.direction = LIKE
    vote.save
  end

  def self.dislike!(proposal, user)
    vote = new
    vote.proposal = proposal
    vote.user = user
    vote.direction = DISLIKE
    vote.save!

    vote
  end
end<commit_msg>Refactor: Isolate the vote definition, so it can be used both by like and dislike methods.<commit_after>class Vote < ActiveRecord::Base
  LIKE = 1
  DISLIKE = -1

  belongs_to :proposal
  belongs_to :user

  validates_presence_of :direction
  validates_associated :user
  validates_associated :proposal

  validates_uniqueness_of :proposal_id, :scope  =>  :user_id

  scope :positives,  where(:direction => LIKE)
  scope :negatives,  where(:direction => DISLIKE)

  def self.like(proposal, user)
    vote proposal, user, LIKE
  end

  def self.dislike!(proposal, user)
    vote = new
    vote.proposal = proposal
    vote.user = user
    vote.direction = DISLIKE
    vote.save!

    vote
  end

  private

  def self.vote(proposal, user, direction)
    vote = new
    vote.proposal = proposal
    vote.user = user
    vote.direction = direction
    vote.save
  end
end
{'fraction_non_alphanumeric': 0.08317338451695458, 'fraction_numerical': 0.0025591810620601407, 'mean_word_length': 3.25, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6769644', 'n_tokens_mistral': 539, 'n_tokens_neox': 511, 'n_words': 138}