Sha256: 228ed4b22ab5efc5f1ee20b912dc7104d80f385cbbe1ab95c975fa1517972190

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

module Honor
  extend ActiveSupport::Concern

  def add_points(number_of_points, message = "Manually granted through 'add_points'", category = 'default' )
    points.create!({value: number_of_points, message: message, category: category})
  end

  def subtract_points(number_of_points, message = "Manually granted through 'add_points'", category = 'default' )
    add_points -number_of_points, message, category
  end

  def points_total(category = nil)
    if category.nil?
      points.sum(:value)
    else
      points.where("points.category = ?", category).sum(:value)
    end
  end

  def points_today(category = nil)
    if category.nil?
      points.where(:created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).sum(:value)
    else
      points.where(:created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day).where("points.category = ?", category).sum(:value)
    end
  end

  def points_this_week(category = nil)
    if category.nil?
      points.where(:created_at => Time.zone.now.beginning_of_week..Time.zone.now.end_of_week).sum(:value)
    else
      points.where(:created_at => Time.zone.now.beginning_of_week..Time.zone.now.end_of_week).where("points.category = ?", category).sum(:value)
    end
  end

  def points_this_month(category = nil)
    if category.nil?
      points.where(:created_at => Time.zone.now.beginning_of_month..Time.zone.now.end_of_month).sum(:value)
    else
      points.where(:created_at => Time.zone.now.beginning_of_month..Time.zone.now.end_of_month).where("points.category = ?", category).sum(:value)
    end
  end

  def points_this_year(category = nil)
    if category.nil?
      points.where(:created_at => Time.zone.now.beginning_of_year..Time.zone.now.end_of_year).sum(:value)
    else
      points.where(:created_at => Time.zone.now.beginning_of_year..Time.zone.now.end_of_year).where("points.category = ?", category).sum(:value)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
honor-1.0.3 lib/honor/model_additions.rb
honor-1.0.2 lib/honor/model_additions.rb
honor-1.0.1 lib/honor/model_additions.rb
honor-1.0.0 lib/honor/model_additions.rb