Module: Models::Base::Sash

Defined in:
lib/merit/models/base/sash.rb

Instance Method Summary collapse

Instance Method Details

#add_badge(badge_id) ⇒ Object



12
13
14
15
16
# File 'lib/merit/models/base/sash.rb', line 12

def add_badge(badge_id)
  bs = Merit::BadgesSash.new(badge_id: badge_id.to_i)
  badges_sashes << bs
  bs
end

#add_points(num_points, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/merit/models/base/sash.rb', line 34

def add_points(num_points, options = {})
  point = Merit::QalamScore::Point.new
  point.num_points = num_points
  scores
    .where(category: options[:category] || 'default')
    .first_or_create
    .score_points << point
  point
end

#badge_idsObject



8
9
10
# File 'lib/merit/models/base/sash.rb', line 8

def badge_ids
  badges_sashes.map(&:badge_id)
end

#badgesObject



4
5
6
# File 'lib/merit/models/base/sash.rb', line 4

def badges
  badge_ids.map { |id| Merit::Badge.find id }
end

#points(options = {}) ⇒ Integer

Retrieve the number of points from a category By default all points are summed up

Parameters:

  • category (String)

    The category

Returns:

  • (Integer)

    The number of points



26
27
28
29
30
31
32
# File 'lib/merit/models/base/sash.rb', line 26

def points(options = {})
  if (category = options[:category])
    scores.where(category: category).first.try(:points) || 0
  else
    scores.reduce(0) { |sum, score| sum + score.points }
  end
end

#rm_badge(badge_id) ⇒ Object



18
19
20
# File 'lib/merit/models/base/sash.rb', line 18

def rm_badge(badge_id)
  badges_sashes.where(badge_id: badge_id.to_i).first.try(:destroy)
end

#subtract_points(num_points, options = {}) ⇒ Object



44
45
46
# File 'lib/merit/models/base/sash.rb', line 44

def subtract_points(num_points, options = {})
  add_points(-num_points, options)
end