Sha256: afd745bea724426d70b8bd5ea093d9a7cf98305a52d7a89788353e706868888a

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Formol
  module Tracking
    extend ActiveSupport::Concern
    
    module ClassMethods
      
      # SQL condition corresponding to an unread topic
      # A topic is considered as unread if:
      # * it's been tracked before last_post creation
      # * OR last_post created after a given date
      def unread_condition(date)
        posts_table = Formol::Post.table_name
        
        cond = "
          (
            tt.id IS NOT NULL
            AND marked_at < #{posts_table}.created_at
          ) OR (
            tt.id IS NULL
            AND #{posts_table}.created_at >= ?
          )
        ".squish #to keep logs clean
        
        where(cond, date)
      end
      
      # return common conditions to get unread topics / forums
      def unread_scope_base(user, opts = {})
        tracks_table = Formol::Topic::Track.table_name
        topics_table = Formol::Topic.table_name
        
        sql_join =  "LEFT OUTER JOIN #{tracks_table} "
        sql_join << "tt ON tt.topic_id = #{topics_table}.id "
        sql_join << "AND tt.user_id = #{user.id}"
        
        scope = joins(sql_join).unread_condition(user.created_at)

        scope = scope.where("#{topics_table}.forum_id = ?", opts[:forum_id])  if opts[:forum_id]
        scope = scope.where("#{topics_table}.id IN (?)",    opts[:topic_ids]) if opts[:topic_ids]
        
        scope
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
formol-0.0.6 app/models/formol/tracking.rb
formol-0.0.5 app/models/formol/tracking.rb
formol-0.0.4 app/models/formol/tracking.rb