Module: TracksVisits::ActiveRecord::Visitable::InstanceMethods

Defined in:
lib/tracks_visits/visitable.rb

Instance Method Summary collapse

Instance Method Details

#first_visited_atObject

first_visit = created_at.



128
129
130
# File 'lib/tracks_visits/visitable.rb', line 128

def first_visited_at
  self.created_at if self.respond_to?(:created_at)
end

#last_visited_atObject

last_visit = updated_at.



134
135
136
# File 'lib/tracks_visits/visitable.rb', line 134

def last_visited_at
  self.updated_at if self.respond_to?(:updated_at)
end

#reset_visits!Object

Delete all tracked visits for this visitable object.



190
191
192
193
# File 'lib/tracks_visits/visitable.rb', line 190

def reset_visits!
  self.visits.delete_all
  self.total_visits_count = self.unique_visits_count = 0 if self.has_cached_fields?
end

#total_visitsObject Also known as: number_of_visits

Get the total number of visits for this object.



152
153
154
155
156
157
158
# File 'lib/tracks_visits/visitable.rb', line 152

def total_visits
  if self.has_cached_fields?
    self.total_visits || 0
  else
    tracks_visits_options[:visit_class].sum(:visits, :conditions => {:visitable_id => self.id})
  end
end

#unique_visitsObject Also known as: number_of_visitors

Get the unique number of visits for this object based on the visits field, or with a SQL query if the visited objects doesn’t have the visits field



141
142
143
144
145
146
147
# File 'lib/tracks_visits/visitable.rb', line 141

def unique_visits
  if self.has_cached_fields?
    self.unique_visits || 0
  else
    self.visits.size
  end
end

#visit!(identifiers) ⇒ Object

View the object with and identifier (user or ip) - create new if new visitor.

Identifiers hash:

  • :ip - identify with IP

  • :visitor - identify with a visitor-model (e.g. User, …)

  • :user - (same as above)

  • :account - (same as above)



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/tracks_visits/visitable.rb', line 203

def visit!(identifiers)
  visitor, ip = self.validate_visitor(identifiers)
  
  begin
    # Count unique visits only, based on account or IP.
    visit = self.visits.find_by_visitor_id(visitor.id) if visitor.present?
    visit ||= self.visits.find_by_ip(ip) if ip.present?
    
    # Try to get existing visit for the current visitor, 
    # or create a new otherwise and set new attributes.
    if visit.present?
      visit.visits += 1
      
      unique_visit = false
    else
      visit = tracks_visits_options[:visit_class].new do |v|
        #v.visitor = visitor
        #v.visitable = self
        v.visitable_id    = self.id
        v.visitable_type  = self.class.name
        v.visitor_id      = visitor.id if visitor
        v.visitor_type    = visitor.class.name if visitor
        v.ip              = ip if ip.present?
        v.visits          = 1
      end
      self.visits << visit
      
      unique_visit = true
    end
    
    visit.save
    
    # Maintain cached value if cached field is available.
    #
    if self.has_cached_fields?
      self.unique_visits += 1 if unique_visit
      self.total_visits += 1
      self.save_without_validation
    end
    
    true
  rescue Exception => e
    raise TracksVisitsError, "Database transaction failed: #{e}"
    false
  end
end

#visitable?Boolean Also known as: is_visitable?

Does this object count/track visits?

Returns:

  • (Boolean)


121
122
123
# File 'lib/tracks_visits/visitable.rb', line 121

def visitable?
  self.class.visitable?
end

#visited?Boolean Also known as: is_visited?

Is this object visited by anyone?

Returns:

  • (Boolean)


163
164
165
# File 'lib/tracks_visits/visitable.rb', line 163

def visited?
  self.unique_visits > 0
end

#visited_by?(identifiers) ⇒ Boolean Also known as: is_visited_by?

Check if an item was already visited by the given visitor or ip.

Identifiers hash:

  • :ip - identify with IP

  • :visitor - identify with a visitor-model (e.g. User, …)

  • :user - (same as above)

  • :account - (same as above)

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
184
185
# File 'lib/tracks_visits/visitable.rb', line 176

def visited_by?(identifiers)
  visitor, ip = self.validate_visitor(identifiers)
  
  conditions = if visitor.present?
    {:visitor => visitor}
  else # ip
    {:ip => (ip ? ip.to_s.strip : nil)}
  end
  self.visits.count(:conditions => conditions) > 0
end