Class: BestBoy::MonthReport

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ObeysTestMode
Defined in:
lib/best_boy/models/active_record/best_boy/month_report.rb

Class Method Summary collapse

Methods included from ObeysTestMode

#save, #save!

Class Method Details

.create_for(owner, type, source = nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 45

def self.create_for(owner, type, source = nil)
  month_report = BestBoy::MonthReport.new
  month_report.owner_type   = owner.to_s
  month_report.event        = type
  month_report.event_source = source
  month_report.save ? month_report : nil
end

.current_for(date, owner, type, source = nil) ⇒ Object

class methods



36
37
38
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 36

def self.current_for(date, owner, type, source = nil)
  self.for(owner, type, source).between(date.beginning_of_month, date)
end

.current_or_create_for(owner, type, source = nil) ⇒ Object



40
41
42
43
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 40

def self.current_or_create_for(owner, type, source = nil)
  month_report = self.current_for(Time.zone.now, owner, type, source).last
  month_report.present? ? month_report : self.create_for(owner, type, source)
end

.for(owner, type, source = nil) ⇒ Object



53
54
55
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 53

def self.for(owner, type, source = nil)
  self.where(owner_type: owner, event: type, event_source: source)
end

.monthly_occurrences_for(owner, type, source = nil, date) ⇒ Object



57
58
59
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 57

def self.monthly_occurrences_for(owner, type, source = nil, date)
  self.for(owner, type, source).between(date.beginning_of_month, date.end_of_month).sum(:occurrences)
end

.overall_occurrences_for(owner, type, source = nil) ⇒ Object



65
66
67
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 65

def self.overall_occurrences_for(owner, type, source = nil)
  self.for(owner, type, source).sum(:occurrences)
end

.yearly_occurrences_for(owner, type, source = nil, date) ⇒ Object



61
62
63
# File 'lib/best_boy/models/active_record/best_boy/month_report.rb', line 61

def self.yearly_occurrences_for(owner, type, source = nil, date)
  self.for(owner, type, source).between(date.beginning_of_year, date).sum(:occurrences)
end