Sha256: 076604ba110e22864a2772d04c34a3533df2656150af3f1faac164ba9833c1eb

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

module BestBoy
  class MonthReport < ActiveRecord::Base

    include BestBoy::ObeysTestMode
    include BestBoy::Reporting

    # db configuration
    #
    #

    self.table_name = "best_boy_month_reports"

    # associations
    #
    #

    belongs_to :owner, polymorphic: true
    has_many :day_reports

    # validations
    #
    #

    validates :owner_type, :event, presence: true

    # scopes
    #
    #

    scope :created_on, ->(date)                 { where(created_at: date.beginning_of_day..date.end_of_day) }
    scope :between,    ->(start_date, end_date) { where(created_at: start_date.beginning_of_day..end_date.end_of_day) }

    # class methods
    #
    #

    class << self

      def create_for(owner, type, source = nil, date = Time.zone.now)
        month_report              = BestBoy::MonthReport.new
        month_report.owner_type   = owner.to_s
        month_report.event        = type
        month_report.event_source = source
        month_report.created_at   = date || Time.zone.now

        month_report.save ? month_report : nil
      end

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
best_boy-3.1.0 app/models/best_boy/month_report.rb
best_boy-3.0.0 app/models/best_boy/month_report.rb
best_boy-2.2.3 lib/best_boy/models/active_record/best_boy/month_report.rb
best_boy-2.2.2 lib/best_boy/models/active_record/best_boy/month_report.rb
best_boy-2.2.1 lib/best_boy/models/active_record/best_boy/month_report.rb