Module: Redmineup::ActsAsViewed::Viewed::ViewMethods

Defined in:
lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



128
129
130
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 128

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



147
148
149
150
151
152
153
154
155
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 147

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


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 161

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 ⇒ Object

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



140
141
142
143
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 140

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)


133
134
135
136
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 133

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)


184
185
186
187
188
189
190
191
192
193
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 184

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", ip: ip).any?
  end
end