Sha256: b54bfbee9c1702644b72997a7d7a2b52558d8e7a106a0e8181e4a4c097de3c72

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

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

    def badge_ids
      badges_sashes.map(&:badge_id)
    end

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

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

    # Retrieve the number of points from a category
    # By default all points are summed up
    # @param category [String] The category
    # @return [Integer] The number of points
    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

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

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

    private

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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
merit-4.0.3 lib/merit/models/sash_concern.rb
merit-4.0.2 lib/merit/models/sash_concern.rb
qalam_merit-4.5.00 lib/merit/models/sash_concern.rb
qalam_merit-4.0.4 lib/merit/models/sash_concern.rb
qalam_merit-4.0.35 lib/merit/models/sash_concern.rb
merit-4.0.1 lib/merit/models/sash_concern.rb
merit-4.0.0 lib/merit/models/sash_concern.rb