Module: ActsAsReadable::InstanceMethods
- Defined in:
- lib/acts_as_readable/acts_as_readable.rb
Instance Attribute Summary collapse
-
#cached_reading ⇒ Object
Returns the value of attribute cached_reading.
Instance Method Summary collapse
- #acts_like_readable? ⇒ Boolean
- #latest_update_read_by?(user) ⇒ Boolean
- #read_by!(user) ⇒ Object
- #read_by?(user) ⇒ Boolean
- #unread_by!(user) ⇒ Object
-
#updated?(user) ⇒ Boolean
Returns true if the user has read this at least once, but it has been updated since the last reading.
Instance Attribute Details
#cached_reading ⇒ Object
Returns the value of attribute cached_reading.
98 99 100 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 98 def cached_reading @cached_reading end |
Instance Method Details
#acts_like_readable? ⇒ Boolean
100 101 102 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 100 def acts_like_readable? true end |
#latest_update_read_by?(user) ⇒ Boolean
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 149 def latest_update_read_by?(user) if cached_reading cached_reading.read? && cached_reading.updated_at > self.updated_at elsif cached_reading == false user[[:cache]].to_f > self.updated_at.to_f elsif reading = readings.find_by_user_id(user.id) reading.read? && reading.updated_at > self.updated_at else user[[:cache]].to_f > self.updated_at.to_f end end |
#read_by!(user) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 104 def read_by!(user) # Find an existing reading and update the record so we can know when the thing was first read, and the last time we read it reading = Reading.find_or_initialize_by(:user_id => user.id, :readable_id => self.id, :readable_type => HelperMethods.readable_type(self.class)) reading.updated_at = Time.now # Explicitly set the read time to now in order to force a save in case we haven't changed anything else about the reading reading.state = :read reading.save! @read_by_retried = false rescue ActiveRecord::RecordNotUnique unless @read_by_retried @read_by_retried = true retry end end |
#read_by?(user) ⇒ Boolean
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 130 def read_by?(user) if cached_reading cached_reading.read? elsif cached_reading == false user[[:cache]].to_f > self.created_at.to_f elsif readers.loaded? readers.include?(user) elsif reading = readings.find_by_user_id(user.id) reading.read? else user[[:cache]].to_f > self.created_at.to_f end end |
#unread_by!(user) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 118 def unread_by!(user) reading = Reading.find_or_initialize_by(:user_id => user.id, :readable_id => self.id, :readable_type => HelperMethods.readable_type(self.class)) reading.state = :unread reading.save! @unread_by_retried = false rescue ActiveRecord::RecordNotUnique unless @unread_by_retried @unread_by_retried = true retry end end |
#updated?(user) ⇒ Boolean
Returns true if the user has read this at least once, but it has been updated since the last reading
145 146 147 |
# File 'lib/acts_as_readable/acts_as_readable.rb', line 145 def updated?(user) read_by?(user) && !latest_update_read_by?(user) end |