Sha256: 71cc385630d1f5fac0738e7f1a61686ae134bf05eca99cefb90938e3fdfbdc36

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Thredded
  module UserTopicReadStateCommon
    extend ActiveSupport::Concern
    included do
      extend ClassMethods
      validates :user_id, uniqueness: { scope: :postable_id }
    end

    # @return [Boolean]
    def read?
      postable.last_post_at <= read_at
    end

    # @param post [Post or PrivatePost]
    # @return [Boolean]
    def post_read?(post)
      post.created_at <= read_at
    end

    module ClassMethods
      # @param user_id [Integer]
      # @param topic_id [Integer]
      # @param post [Thredded::PostCommon]
      # @param post_page [Integer]
      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

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

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
threddedDANIEL-0.14.5 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.14.4 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.14.3 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.14.2 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.14.1 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.14.0 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.8 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.7 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.6 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.5 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.4 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.3 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.2 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.1 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.13.0 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.12.4 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.12.3 app/models/concerns/thredded/user_topic_read_state_common.rb
thredded-0.12.2 app/models/concerns/thredded/user_topic_read_state_common.rb