Class: BestBoy::DayReport

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ObeysTestMode
Defined in:
lib/best_boy/models/active_record/best_boy/day_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
52
53
54
55
# File 'lib/best_boy/models/active_record/best_boy/day_report.rb', line 45

def self.create_for(owner, type, source = nil)
  month_report = BestBoy::MonthReport.current_or_create_for(owner, type, source)
  day_report   = BestBoy::DayReport.new

  day_report.owner_type      = owner
  day_report.event           = type
  day_report.month_report_id = month_report.id
  day_report.event_source    = source

  day_report.save ? day_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/day_report.rb', line 36

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

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



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

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

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



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

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

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



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

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

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



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

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