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



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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongoid_traffic/log.rb', line 5

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    field :s, as: :scope, type: String

    field :ac, as: :access_count, type: Integer

    field :b, as: :browsers, type: Hash, default: {}
    field :c, as: :countries, type: Hash, default: {}
    field :r, as: :referers, type: Hash, default: {}
    field :u, as: :unique_ids, type: Hash, default: {}

    field :uat, as: :updated_at, type: Time

    # ---------------------------------------------------------------------

    field :df, as: :date_from, type: Date
    field :dt, as: :date_to, type: Date

    # ---------------------------------------------------------------------

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

    # ---------------------------------------------------------------------

    default_scope -> { where(scope: nil) }

    scope :for_dates, -> (date_from, date_to) { where(date_from: date_from, date_to: date_to) }

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

    scope :scoped_to, -> (scope) { where(scope: scope) }

    # ---------------------------------------------------------------------

    index(scope: 1, date_from: 1, date_to: 1)
  end
end