Module: MongoidTraffic::Log

Defined in:
lib/mongoid_traffic/log.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mongoid_traffic/log.rb', line 3

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    field :df, as: :date_from, type: Date
    field :dt, as: :date_to, type: Date
    field :ts, as: :time_scope, type: Integer

    field :ac, as: :access_count, type: Integer
    field :uat, as: :updated_at, type: Time

    validates :date_from, presence: true
    validates :date_to, presence: true

    scope :yearly, -> { where(ts: TIME_SCOPE_OPTIONS[:year]) }
    scope :monthly, -> { where(ts: TIME_SCOPE_OPTIONS[:month]) }
    scope :weekly, -> { where(ts: TIME_SCOPE_OPTIONS[:year]) }
    scope :daily, -> { where(ts: TIME_SCOPE_OPTIONS[:day]) }

    scope :for_dates, ->(date_from, date_to) { where(:date_from.lte => date_to, :date_to.gte => date_from) }
    scope :year, ->(year) { yearly.for_dates(Date.parse("01/01/#{year}"), Date.parse("01/01/#{year}").at_end_of_year) }
    scope :month, ->(month, year) { monthly.for_dates(Date.parse("01/#{month}/#{year}"), Date.parse("01/#{month}/#{year}").at_end_of_month) }
    scope :week, ->(week, year) { weekly.for_dates(Date.commercial(year, week), Date.commercial(year, week).at_end_of_week) }
    scope :day, ->(date) { daily.for_dates(date, date) }

    index time_scope: 1, date_from: 1, date_to: 1
  end
end