Sha256: 0449cd7a5ab77d797976e3efc3bef541381a303664f59d85ae5f7e8ed2fa9cb2

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

# Sash is a container for reputation data for meritable models. It's an
# indirection between meritable models and badges and scores (one to one
# relationship).
#
# It's existence make join models like badges_users and scores_users
# unnecessary. It should be transparent at the application.
class Sash < ActiveRecord::Base
  has_many :badges_sashes, :dependent => :destroy
  has_many :scores, :dependent => :destroy, :class_name => 'Merit::Score'

  after_create :create_scores

  def badges
    badge_ids.collect { |b_id| Badge.find(b_id) }
  end

  def badge_ids
    badges_sashes.map(&:badge_id)
  end

  def add_badge(badge_id)
    bs = BadgesSash.new
    bs.badge_id = badge_id
    self.badges_sashes << bs
  end

  def rm_badge(badge_id)
    badges_sashes.find_by_badge_id(badge_id).try(:destroy)
  end


  def points(category = 'default')
    scores.where(:category => category).first.points
  end

  def add_points(num_points, log = 'Manually granted through `add_points`', category = 'default')
    point = Merit::Score::Point.new
    point.log = log
    point.num_points = num_points
    self.scores.where(:category => category).first.score_points << point
  end

  def substract_points(num_points, log = 'Manually granted through `add_points`', category = 'default')
    add_points -num_points, log, category
  end

  private

  def create_scores
    self.scores << Merit::Score.create
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
merit-1.4.0 lib/merit/models/active_record/sash.rb
merit-1.3.1 lib/merit/models/active_record/sash.rb
merit-1.3.0 lib/merit/models/active_record/sash.rb
merit-1.2.3 lib/merit/models/active_record/sash.rb
merit-1.2.2 lib/merit/models/active_record/sash.rb
merit-1.2.1 lib/merit/models/active_record/sash.rb
merit-1.2.0 lib/merit/models/active_record/sash.rb
merit-1.1.2 lib/merit/models/active_record/sash.rb