Module: Honor

Extended by:
ActiveSupport::Concern
Defined in:
lib/honor.rb,
lib/honor/point.rb,
lib/honor/version.rb,
lib/honor/scorecard.rb,
lib/honor/model_additions.rb,
lib/generators/honor/install/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Point, Scorecard

Constant Summary collapse

VERSION =
"2.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/honor.rb', line 10

def self.included(receiver)
  receiver.class_eval %Q{
    has_many :points, :class_name => "Honor::Point", :foreign_key => "user_id", :dependent => :destroy
    has_one :scorecard, :class_name => "Honor::Scorecard", :foreign_key => "user_id", :dependent => :destroy
  }

  Honor::Point.class_eval %Q{
    belongs_to :#{receiver.to_s.underscore}, :class_name => "#{receiver.to_s}", :foreign_key => "user_id"
  }

  Honor::Scorecard.class_eval %Q{
    belongs_to :#{receiver.to_s.underscore}, :class_name => "#{receiver.to_s}", :foreign_key => "user_id"
  }
end

Instance Method Details

#add_points(number_of_points, message = "Manually granted through 'add_points'", category = 'default') ⇒ Object



4
5
6
# File 'lib/honor/model_additions.rb', line 4

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

#points_this_month(category = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/honor/model_additions.rb', line 36

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

#points_this_week(category = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/honor/model_additions.rb', line 28

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

#points_this_year(category = nil) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/honor/model_additions.rb', line 44

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

#points_today(category = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/honor/model_additions.rb', line 20

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

#points_total(category = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/honor/model_additions.rb', line 12

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

#subtract_points(number_of_points, message = "Manually granted through 'add_points'", category = 'default') ⇒ Object



8
9
10
# File 'lib/honor/model_additions.rb', line 8

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