Module: Thredded::UserTopicReadStateCommon::ClassMethods

Defined in:
app/models/concerns/thredded/user_topic_read_state_common.rb

Instance Method Summary collapse

Instance Method Details

#read_on_first_post!(user, topic) ⇒ Object



37
38
39
# File 'app/models/concerns/thredded/user_topic_read_state_common.rb', line 37

def read_on_first_post!(user, topic)
  create!(user: user, postable: topic, read_at: Time.zone.now, page: 1)
end

#touch!(user_id, topic_id, post, post_page) ⇒ Object

Parameters:



27
28
29
30
31
32
33
34
35
# File 'app/models/concerns/thredded/user_topic_read_state_common.rb', line 27

def touch!(user_id, topic_id, post, post_page)
  # TODO: Switch to upsert once Travis supports PostgreSQL 9.5.
  # Travis issue: https://github.com/travis-ci/travis-ci/issues/4264
  # Upsert gem: https://github.com/seamusabshere/upsert
  state = find_or_initialize_by(user_id: user_id, postable_id: topic_id)
  fail ArgumentError, "expected post_page >= 1, given #{post_page.inspect}" if post_page < 1
  return unless !state.read_at? || state.read_at < post.created_at
  state.update!(read_at: post.created_at, page: post_page)
end