Class: Gricer::Mongoid::Request

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Request, ActiveModel::Statistics, CounterCache, Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/gricer/mongoid/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Statistics

included, #model_type

Methods included from ActiveModel::Request

included, #ip_address=, #locale, #locale=, #params=, #referer=, #request=

Methods included from CounterCache

included

Class Method Details

.browsersMongoid::Criteria

Filter out anything that is not a Browser or MobileBrowser

Returns:

  • (Mongoid::Criteria)


52
53
54
# File 'app/models/gricer/mongoid/request.rb', line 52

def self.browsers
  any_in agent_id: Gricer::Mongoid::Agent.browsers.only(:id).map{|x| x.id}
end

.first_by_id(id) ⇒ Object



56
57
58
# File 'app/models/gricer/mongoid/request.rb', line 56

def self.first_by_id(id)
  where('_id' => id).first
end

Instance Method Details

#agent_header=(agent_header) ⇒ Gricer::Agent

Find or Create Gricer::Agent corrosponding to the given user agent string as given in the HTTP header

Parameters:

  • agent_header (String)

    A user agent string as in a HTTP header

Returns:

  • (Gricer::Agent)


64
65
66
# File 'app/models/gricer/mongoid/request.rb', line 64

def agent_header=(agent_header)
  self.agent = "Gricer::#{model_type}::Agent".constantize.find_or_create_by request_header: agent_header
end

#init_sessionGricer::Session

Init the corrosponding Gricer::Session (called before create)

Returns:

  • (Gricer::Session)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/gricer/mongoid/request.rb', line 72

def init_session
  if session
    if session.updated_at < Time.now - ::Gricer.config.max_session_duration
      self.session = Session.create previous_session: session, ip_address: @ip_address, agent: agent, requested_locale: @request_locale
    else
      self.session.touch
    end
  else
    self.is_first_in_session = true
    self.session = Session.create ip_address: @ip_address, agent: agent, requested_locale: @request_locale
    self.session.touch
  end
      
  session
end