Class: Commontator::Thread

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/commontator/thread.rb

Instance Method Summary collapse

Instance Method Details

#can_be_edited_by?(user) ⇒ Boolean

Thread moderator capabilities

Returns:

  • (Boolean)


147
148
149
150
# File 'app/models/commontator/thread.rb', line 147

def can_be_edited_by?(user)
  !commontable.nil? && !user.nil? && user.is_commontator &&\
  config.thread_moderator_proc.call(self, user)
end

#can_be_read_by?(user) ⇒ Boolean

Reader capabilities (user can be nil or false)

Returns:

  • (Boolean)


140
141
142
143
144
# File 'app/models/commontator/thread.rb', line 140

def can_be_read_by?(user)
  return true if can_be_edited_by?(user)
  !commontable.nil? &&\
  config.thread_read_proc.call(self, user)
end

#can_subscribe?(user) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
# File 'app/models/commontator/thread.rb', line 152

def can_subscribe?(user)
  thread_sub = config.thread_subscription.to_sym
  !is_closed? && !user.nil? && user.is_commontator &&\
  (thread_sub == :m || thread_sub == :b) &&\
  can_be_read_by?(user)
end

#clearObject

Creates a new empty thread and assigns it to the commontable The old thread is kept in the database for archival purposes



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/commontator/thread.rb', line 119

def clear
  return if commontable.blank? || !is_closed?
  new_thread = Thread.new
  new_thread.commontable = commontable

  with_lock do
    self.commontable = nil
    save!
    new_thread.save!
    subscriptions.each do |s|
      s.thread = new_thread
      s.save!
    end
  end
end

#close(user = nil) ⇒ Object



75
76
77
78
79
80
# File 'app/models/commontator/thread.rb', line 75

def close(user = nil)
  return false if is_closed?
  self.closed_at = Time.now
  self.closer = user
  save
end

#configObject



14
15
16
# File 'app/models/commontator/thread.rb', line 14

def config
  @config ||= commontable.try(:commontable_config) || Commontator
end

#filtered_commentsObject



28
29
30
31
32
# File 'app/models/commontator/thread.rb', line 28

def filtered_comments
  cf = config.comment_filter
  return comments if cf.nil?
  comments.where(cf)
end

#is_closed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/commontator/thread.rb', line 71

def is_closed?
  !closed_at.blank?
end

#is_filtered?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/commontator/thread.rb', line 24

def is_filtered?
  will_paginate? || config.comment_filter
end

#mark_as_read_for(subscriber) ⇒ Object



111
112
113
114
115
# File 'app/models/commontator/thread.rb', line 111

def mark_as_read_for(subscriber)
  subscription = subscription_for(subscriber)
  return false unless subscription
  subscription.touch
end

#new_comment_page(per_page = config.comments_per_page) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/commontator/thread.rb', line 51

def new_comment_page(per_page = config.comments_per_page)
  return 1 if per_page.nil? || per_page.to_i <= 0
  comment_index = \
    case config.comment_order.to_sym
    when :l
      1 # First comment
    when :ve
      comment_arel = Comment.arel_table
      # Last comment with rating = 0
      filtered_comments.where((comment_arel[:cached_votes_up] - comment_arel[:cached_votes_down]).gteq 0).count
    when :vl
      comment_arel = Comment.arel_table
      # First comment with rating = 0
      filtered_comments.where((comment_arel[:cached_votes_up] - comment_arel[:cached_votes_down]).gt 0).count + 1
    else
      filtered_comments.count # Last comment
    end
  (comment_index.to_f/per_page.to_i).ceil
end

#ordered_comments(unfiltered = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'app/models/commontator/thread.rb', line 34

def ordered_comments(unfiltered = false)
  vc = unfiltered ? comments : filtered_comments
  case config.comment_order.to_sym
  when :l then vc.order('created_at DESC')
  when :e then vc.order('created_at ASC')
  when :ve then vc.order('cached_votes_down - cached_votes_up', 'created_at ASC')
  when :vl then vc.order('cached_votes_down - cached_votes_up', 'created_at DESC')
  else vc
  end
end

#paginated_comments(page = 1, per_page = config.comments_per_page) ⇒ Object



45
46
47
48
49
# File 'app/models/commontator/thread.rb', line 45

def paginated_comments(page = 1, per_page = config.comments_per_page)
  oc = ordered_comments
  return oc unless will_paginate?
  oc.paginate(page: page, per_page: per_page)
end

#reopenObject



82
83
84
85
86
# File 'app/models/commontator/thread.rb', line 82

def reopen
  return false unless is_closed? && !commontable.nil?
  self.closed_at = nil
  save
end

#subscribe(subscriber) ⇒ Object



97
98
99
100
101
102
103
# File 'app/models/commontator/thread.rb', line 97

def subscribe(subscriber)
  return false unless subscriber.is_commontator && !subscription_for(subscriber)
  subscription = Subscription.new
  subscription.subscriber = subscriber
  subscription.thread = self
  subscription.save
end

#subscribersObject



88
89
90
# File 'app/models/commontator/thread.rb', line 88

def subscribers
  subscriptions.collect{|s| s.subscriber}
end

#subscription_for(subscriber) ⇒ Object



92
93
94
95
# File 'app/models/commontator/thread.rb', line 92

def subscription_for(subscriber)
  return nil if !subscriber || !subscriber.is_commontator
  subscriber.subscriptions.where(thread_id: self.id).first
end

#unsubscribe(subscriber) ⇒ Object



105
106
107
108
109
# File 'app/models/commontator/thread.rb', line 105

def unsubscribe(subscriber)
  subscription = subscription_for(subscriber)
  return false unless subscription
  subscription.destroy
end

#will_paginate?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'app/models/commontator/thread.rb', line 18

def will_paginate?
  return false if config.comments_per_page.nil? || !comments.respond_to?(:paginate)
  require 'commontator/link_renderer'
  true
end