module YeshouaCrm::ActsAsViewed::Viewed::ViewMethods
Public Instance Methods
increase_views_count(options)
click to toggle source
Change views count (total_views and views) if it's existing in object If options == true count of unique views doesn't change
# File lib/yeshoua_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)
click to toggle source
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
# File lib/yeshoua_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_count()
click to toggle source
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
# File lib/yeshoua_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?()
click to toggle source
Is this object viewed already?
# File lib/yeshoua_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)
click to toggle source
Check if an item was already viewed by the given viewer
# File lib/yeshoua_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