Module: YeshuaCrm::ActsAsViewed::Viewed::ViewMethods

Defined in:
lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



123
124
125
# File 'lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 123

def self.included(base) #:nodoc:
  base.extend ClassMethods
end

Instance Method Details

#increase_views_count(options) ⇒ Object

Change views count (total_views and views) if it’s existing in object If options == true count of unique views doesn’t change



142
143
144
145
146
147
148
149
150
# File 'lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 142

def increase_views_count(options)
  if attributes.has_key?('views') && attributes.has_key?('total_views')
    target = self
    target.views = ((target.views || 0) + 1) unless options[:only_total]
    target.total_views = ((target.total_views || 0) + 1)
    target.record_timestamps = false
    target.save(:validate => false, :touch => false)
  end
end

#view(ip, viewer = nil) ⇒ Object

View the object with or without a viewer - create new or update as needed

  • ip - the viewer ip

  • viewer - an object of the viewer class. Must be valid and with an id to be used. Or nil



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 156

def view(ip, viewer = nil)
  # Sanity checks for the parameters
  viewing_class = acts_as_viewed_options[:viewing_class].constantize
  if viewer && !(acts_as_viewed_options[:viewer_class].constantize === viewer)
    raise ViewedError, "the viewer object must be the one used when defining acts_as_viewed (or a descendent of it). other objects are not acceptable"
  end

  viewing_class.transaction do
    if !viewed_by? ip, viewer
      view = viewing_class.new
      view.viewer_id = viewer.id if viewer && !viewer.id.nil?
      view.ip = ip
      viewings << view
      view.save
      increase_views_count(:only_total => false)
    else
      increase_views_count(:only_total => true)
    end
    true
  end
end

#view_countObject

Get the number of viewings for this object based on the views field, or with a SQL query if the viewed objects doesn’t have the views field



135
136
137
138
# File 'lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 135

def view_count
  return ("#{self.total_views}(#{self.views})" || 0) if attributes.has_key? 'views'
  viewings.count
end

#viewed?Boolean

Is this object viewed already?

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 128

def viewed?
  return (!self.views.nil? && self.views > 0) if attributes.has_key? 'views'
  !viewings.first.nil?
end

#viewed_by?(ip, viewer = nil) ⇒ Boolean

Check if an item was already viewed by the given viewer

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
# File 'lib/yeshua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 179

def viewed_by?(ip, viewer = nil)
  if viewer && !viewer.nil? && !(acts_as_viewed_options[:viewer_class].constantize === viewer)
    raise ViewedError, "the viewer object must be the one used when defining acts_as_viewed (or a descendent of it). other objects are not acceptable"
  end
  if viewer && !viewer.id.nil? && !viewer.anonymous?
    return viewings.where("viewer_id = '#{viewer.id}'").any?
  else
    return viewings.where("ip = '#{ip}'").any?
  end
end