Module: Merit::Models::SashConcern
- Included in:
- ActiveRecord::Sash
- Defined in:
- lib/merit/models/sash_concern.rb
Instance Method Summary collapse
- #add_badge(badge_id, options = {}) ⇒ Object
- #add_points(num_points, options = {}) ⇒ Object
- #badge_ids ⇒ Object
- #badges ⇒ Object
-
#points(options = {}) ⇒ Integer
Retrieve the number of points from a category By default all points are summed up.
- #rm_badge(badge_id, options = {}) ⇒ Object
- #subtract_points(num_points, options = {}) ⇒ Object
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, = {}) if (context = [:context]) bs = Merit::BadgesSash.new(badge_id: badge_id.to_i, context: context, badge_type: [: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, = {}) point = Merit::QalamScore::Point.new point.num_points = num_points point.context = [:context] scores .where(category: [:category] || 'default') .first_or_create .score_points << point point end |
#badge_ids ⇒ Object
7 8 9 |
# File 'lib/merit/models/sash_concern.rb', line 7 def badge_ids badges_sashes.map(&:badge_id) end |
#badges ⇒ Object
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
33 34 35 36 37 38 39 |
# File 'lib/merit/models/sash_concern.rb', line 33 def points( = {}) if (category = [: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, = {}) if (context = [:context]) badges_sashes.where(badge_id: badge_id.to_i, context: context, badge_type: [: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, = {}) add_points(-num_points, ) end |