Module: ActsAsReadable::InstanceMethods

Defined in:
lib/acts_as_readable/acts_as_readable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cached_readingObject

Returns the value of attribute cached_reading.



85
86
87
# File 'lib/acts_as_readable/acts_as_readable.rb', line 85

def cached_reading
  @cached_reading
end

Instance Method Details

#acts_like_readable?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/acts_as_readable/acts_as_readable.rb', line 87

def acts_like_readable?
  true
end

#latest_update_read_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/acts_as_readable/acts_as_readable.rb', line 127

def latest_update_read_by?(user)
  if cached_reading
    cached_reading.read? && cached_reading.updated_at > self.updated_at
  elsif cached_reading == false
    user[acts_as_readable_options[: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[acts_as_readable_options[:cache]].to_f > self.updated_at.to_f
  end
end

#read_by!(user) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/acts_as_readable/acts_as_readable.rb', line 91

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!
rescue ActiveRecord::RecordNotUnique
  # Database-level uniqueness constraint failed.
  return self
end

#read_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/acts_as_readable/acts_as_readable.rb', line 108

def read_by?(user)
  if cached_reading
    cached_reading.read?
  elsif cached_reading == false
    user[acts_as_readable_options[: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[acts_as_readable_options[:cache]].to_f > self.created_at.to_f
  end
end

#unread_by!(user) ⇒ Object



102
103
104
105
106
# File 'lib/acts_as_readable/acts_as_readable.rb', line 102

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!
end

#updated?(user) ⇒ Boolean

Returns true if the user has read this at least once, but it has been updated since the last reading

Returns:

  • (Boolean)


123
124
125
# File 'lib/acts_as_readable/acts_as_readable.rb', line 123

def updated?(user)
  read_by?(user) && !latest_update_read_by?(user)
end