<commit_before>class Watchlist < ActiveRecord::Base
  belongs_to :user
  belongs_to :anime
  attr_accessible :user, :anime, :positive, :status

  validates_uniqueness_of :user_id, :scope => :anime_id

  # Return an array of possible valid statuses.
  def self.valid_statuses
    ["Currently Watching", "Plan to Watch", "Completed", "On Hold", "Dropped"]
  end  
  
  validates :status, inclusion: { in: Watchlist.valid_statuses }

  def positive?
    rating && rating > 0
  end

  def negative?
    rating && rating < 0
  end

  # This method will take an array of "watchlist" object IDs and return a unique
  # string hash. This is used to check whether a user's recommendations are up to
  # date.
  def self.watchlist_hash(watchlist_ids)
    Digest::MD5.hexdigest( watchlist_ids.sort * ' ' )
  end
end<commit_msg>Automatically mark a series as completed if all episodes are watched.<commit_after>class Watchlist < ActiveRecord::Base
  belongs_to :user
  belongs_to :anime
  attr_accessible :user, :anime, :positive, :status

  validates_uniqueness_of :user_id, :scope => :anime_id

  # Return an array of possible valid statuses.
  def self.valid_statuses
    ["Currently Watching", "Plan to Watch", "Completed", "On Hold", "Dropped"]
  end  
  
  validates :status, inclusion: { in: Watchlist.valid_statuses }

  def positive?
    rating && rating > 0
  end

  def negative?
    rating && rating < 0
  end

  # This method will take an array of "watchlist" object IDs and return a unique
  # string hash. This is used to check whether a user's recommendations are up to
  # date.
  def self.watchlist_hash(watchlist_ids)
    Digest::MD5.hexdigest( watchlist_ids.sort * ' ' )
  end

  # Handle state transitions intelligently. There are a lot of subtleties
  # here, be careful while adding additional transition handling logic here, and
  # definitely consider how different UI flows are affected. 
  #
  # If `episodes_watched` is equal to the total number of episodes, automatically
  # mark the anime as "Completed".
  #
  # TODO: If the user updates an episode as viewed, the state _must_ be either
  #       "Currently Watching" or "Completed". The "Completed" case is handled
  #       here, but we also need to handle the "Currently Watching" case.
  before_save do
    if episodes_watched == anime.episode_count
      self.status = "Completed"
    end
  end
end
{'fraction_non_alphanumeric': 0.08048883270122208, 'fraction_numerical': 0.0025284450063211127, 'mean_word_length': 3.6457925636007826, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '319492', 'n_tokens_mistral': 737, 'n_tokens_neox': 708, 'n_words': 271}