Module: ActsAsReadable::ClassMethods

Defined in:
lib/acts_as_readable/acts_as_readable.rb

Instance Method Summary collapse

Instance Method Details

#cache_readings_for(readables, user) ⇒ Object

Find all the readings of the readables by the user in a single SQL query and cache them in the readables for use in the view.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/acts_as_readable/acts_as_readable.rb', line 57

def cache_readings_for(readables, user)
  readings = []
  Reading.where(:readable_type => HelperMethods.readable_type(self), :readable_id => readables.collect(&:id), :user_id => user.id).each do |reading|
    readings[reading.readable_id] = reading
  end

  for readable in readables
    readable.cached_reading = readings[readable.id] || false
  end

  return readables
end

#read_by!(user) ⇒ Object

Mark all records as read by the user If a :cache option has been set in acts_as_readable, a timestamp will be updated on the user instead of creating individual readings for each record



72
73
74
75
76
77
78
79
80
81
# File 'lib/acts_as_readable/acts_as_readable.rb', line 72

def read_by!(user)
  if user.has_attribute?(acts_as_readable_options[:cache])
    Reading.where(:user_id => user.id, :readable_type => HelperMethods.readable_type(self)).delete_all
    user.update_column(acts_as_readable_options[:cache], Time.now)
  else
    unread_by(user).find_each do |record|
      record.read_by!(user)
    end
  end
end