Module: Merit::Models::SashConcern
- Included in:
- ActiveRecord::Sash
- Defined in:
- lib/merit/models/sash_concern.rb
Instance Method Summary collapse
- #add_badge(badge_id) ⇒ 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) ⇒ Object
- #subtract_points(num_points, options = {}) ⇒ Object
Instance Method Details
#add_badge(badge_id) ⇒ Object
11 12 13 14 15 |
# File 'lib/merit/models/sash_concern.rb', line 11 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
33 34 35 36 37 38 39 40 41 |
# File 'lib/merit/models/sash_concern.rb', line 33 def add_points(num_points, = {}) point = Merit::Score::Point.new point.num_points = num_points 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
25 26 27 28 29 30 31 |
# File 'lib/merit/models/sash_concern.rb', line 25 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) ⇒ Object
17 18 19 |
# File 'lib/merit/models/sash_concern.rb', line 17 def rm_badge(badge_id) badges_sashes.where(badge_id: badge_id.to_i).first.try(:destroy) end |
#subtract_points(num_points, options = {}) ⇒ Object
43 44 45 |
# File 'lib/merit/models/sash_concern.rb', line 43 def subtract_points(num_points, = {}) add_points(-num_points, ) end |