Module: Formol::Tracking::ClassMethods

Defined in:
app/models/formol/tracking.rb

Instance Method Summary collapse

Instance Method Details

#unread_condition(date) ⇒ Object

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/formol/tracking.rb', line 11

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

#unread_scope_base(user, opts = {}) ⇒ Object

return common conditions to get unread topics / forums



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/formol/tracking.rb', line 28

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