Sha256: 139b2fd263a753412cd0dfc13095495b07467f03f6d8e117c0d53e809dacba7e

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 KB

Contents

module Merit
  module Models::Base
    module Sash
      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
end

Version data entries

7 entries across 7 versions & 2 rubygems

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