Class: Notches::Hit

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

Class Method Summary collapse

Class Method Details

.log(attributes) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/notches/hit.rb', line 19

def self.log(attributes)
  hit = self.new
  hit.transaction do
    Rails.logger.info("[Notches] Tracking #{attributes.inspect} at #{Date.today} #{Time.now}")
    hit.url = Notches::URL.find_or_create_by_url(attributes[:url])
    hit.user_agent = Notches::UserAgent.find_or_create_by_user_agent(attributes[:user_agent])
    hit.session = Notches::Session.find_or_create_by_session_id(attributes[:session_id])
    hit.ip = Notches::IP.find_or_create_by_ip(attributes[:ip])
    hit.date = Notches::Date.find_or_create_by_date(Date.today)
    hit.time = Notches::Time.find_or_create_by_time(Time.now)
    begin
      hit.save!
    rescue #ActiveRecord::RecordNotUnique
      Rails.logger.info "Skipping non-unique hit"
    end
  end
  hit
end