Module: Merit::Models::SashConcern

Included in:
ActiveRecord::Sash
Defined in:
lib/merit/models/sash_concern.rb

Instance Method Summary collapse

Instance Method Details

#add_badge(badge_id, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/merit/models/sash_concern.rb', line 11

def add_badge(badge_id, options = {})
  if (context = options[:context])
    bs = Merit::BadgesSash.new(badge_id: badge_id.to_i, context: context, badge_type: options[:badge_type])
  else
    bs = Merit::BadgesSash.new(badge_id: badge_id.to_i)
  end
    badges_sashes << bs
    bs
end

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/merit/models/sash_concern.rb', line 41

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

#badge_idsObject



7
8
9
# File 'lib/merit/models/sash_concern.rb', line 7

def badge_ids
  badges_sashes.map(&:badge_id)
end

#badgesObject



3
4
5
# File 'lib/merit/models/sash_concern.rb', line 3

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



33
34
35
36
37
38
39
# File 'lib/merit/models/sash_concern.rb', line 33

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, options = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/merit/models/sash_concern.rb', line 21

def rm_badge(badge_id, options = {})
  if (context = options[:context])
    badges_sashes.where(badge_id: badge_id.to_i, context: context, badge_type: options[:badge_type]).first.try(:destroy)
  else
    badges_sashes.where(badge_id: badge_id.to_i).first.try(:destroy)
  end
end

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



52
53
54
# File 'lib/merit/models/sash_concern.rb', line 52

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