Sha256: a8de2d9af50dec8c782e6e3737398d194512220d5ffe802c8d7220455b7cdfe9

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 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, options = {})
      if (course_id = options[:course_id])
        bs = Merit::BadgesSash.new(badge_id: badge_id.to_i, course_id: course_id.to_i)
      else
        bs = Merit::BadgesSash.new(badge_id: badge_id.to_i)
      end
        badges_sashes << bs
        bs
    end

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

    ########################QALAM_DEV#########################
    def add_qalam_badge(badge_id, course_id)
      bs = Merit::BadgesSash.new(badge_id: badge_id.to_i, course_id: course_id.to_i)
      badges_sashes << bs
      bs
    end
    ########################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::QalamScore::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::QalamScore.create
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qalam_merit-4.5.06 lib/merit/models/sash_concern.rb