Class: Notches::Event

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/notches/event.rb

Class Method Summary collapse

Class Method Details

.log(attributes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/notches/event.rb', line 14

def self.log(attributes)
  event = self.new
  event.transaction do
    Rails.logger.info("[Notches] Tracking #{attributes.inspect} at #{Date.today} #{Time.now}")
    now         = Time.zone.now
    event.name  = Notches::Name.find_or_create_by(name: attributes[:name])
    event.scope = Notches::Scope.find_or_create_by(scope_attributes_for(attributes[:scope]))
    event.date  = Notches::Date.find_or_create_by(date: now.to_date)
    event.time  = Notches::Time.find_or_create_by_time(now)

    begin
      event.save!
    rescue
      Rails.logger.info "Skipping non-unique event"
    end
  end
  event
end

.scope_attributes_for(scope) ⇒ Object



33
34
35
36
37
38
39
# File 'app/models/notches/event.rb', line 33

def self.scope_attributes_for(scope)
  scope = [scope] if !scope.is_a?(Array)
  {
    primary:   scope.first.to_s,
    secondary: scope.second.to_s
  }
end