Class: Commontator::Thread

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

Constant Summary collapse

RAILS_7_PRELOADER =
Rails::VERSION::MAJOR >= 7

Instance Method Summary collapse

Instance Method Details

#can_be_edited_by?(user) ⇒ Boolean

Thread moderator capabilities

Returns:

  • (Boolean)


271
272
273
274
# File 'app/models/commontator/thread.rb', line 271

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)


264
265
266
267
268
# File 'app/models/commontator/thread.rb', line 264

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)


276
277
278
279
280
# File 'app/models/commontator/thread.rb', line 276

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



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/commontator/thread.rb', line 245

def clear
  return if commontable.nil? || !is_closed?

  new_thread = Commontator::Thread.new
  new_thread.commontable = commontable

  with_lock do
    self.commontable = nil
    save!
    new_thread.save!
    Commontator::Subscription.where(thread: self).update_all(thread_id: new_thread.id)
  end
end

#close(user = nil) ⇒ Object



195
196
197
198
199
200
201
# File 'app/models/commontator/thread.rb', line 195

def close(user = nil)
  return false if is_closed?

  self.closed_at = Time.now
  self.closer = user
  save
end

#comments_with_parent_id(parent_id, show_all) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/commontator/thread.rb', line 63

def comments_with_parent_id(parent_id, show_all)
  oc = ordered_comments(show_all)

  if [ :i, :b ].include?(config.comment_reply_style)
    # Filter comments by parent_id so we display nested comments
    oc.where(parent_id: parent_id)
  elsif parent_id.nil?
    # Parent is the thread itself and nesting is disabled, so include all comments
    oc
  else
    # Parent is some comment and nesting is disabled, so return nothing
    oc.none
  end
end

#configObject



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

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

#filtered_comments(show_all) ⇒ Object



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

def filtered_comments(show_all)
  return comments if show_all

  cf = config.comment_filter
  return comments if cf.nil?

  comments.where(cf)
end

#is_closed?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'app/models/commontator/thread.rb', line 191

def is_closed?
  !closed_at.nil?
end

#is_filtered?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/commontator/thread.rb', line 21

def is_filtered?
  !config.comment_filter.nil?
end

#is_votable?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/models/commontator/thread.rb', line 17

def is_votable?
  config.comment_voting.to_sym != :n
end

#latest_comment(show_all) ⇒ Object



59
60
61
# File 'app/models/commontator/thread.rb', line 59

def latest_comment(show_all)
  @latest_comment ||= ordered_comments(show_all).last
end

#mark_as_read_for(subscriber) ⇒ Object



236
237
238
239
240
241
# File 'app/models/commontator/thread.rb', line 236

def mark_as_read_for(subscriber)
  subscription = subscription_for(subscriber)
  return false unless subscription

  subscription.touch
end

#nest_comments(comments, root_per_page, per_page_by_parent_id, count_by_parent_id, children_by_parent_id) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/commontator/thread.rb', line 84

def nest_comments(
  comments, root_per_page, per_page_by_parent_id, count_by_parent_id, children_by_parent_id
)
  comments.map do |comment|
    # Delete is used to ensure loops don't cause stack overflow
    children = children_by_parent_id.delete(comment.id) || []
    count = count_by_parent_id.delete(comment.id) || 0
    per_page = per_page_by_parent_id.delete(comment.id) || 0
    nested_children = nest_comments(
      children, root_per_page, per_page_by_parent_id, count_by_parent_id, children_by_parent_id
    )

    [ comment, Commontator::Collection.new(nested_children, count, root_per_page, per_page) ]
  end
end

#nested_comments_for(user, comments, show_all) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/commontator/thread.rb', line 100

def nested_comments_for(user, comments, show_all)
  includes = [ :thread, :creator, :editor ]
  total_entries = comments.total_entries
  root_per_page = config.comments_per_page[0]
  current_page = comments.current_page.to_i
  comments = comments.includes(includes).to_a
  count_by_parent_id = {}
  per_page_by_parent_id = {}
  children_by_parent_id = Hash.new { |hash, key| hash[key] = [] }

  if [ :i, :b ].include? config.comment_reply_style
    all_parent_ids = comments.map(&:id)
    (config.comments_per_page[1..-1] + [ 0 ]).each_with_index do |per_page, index|
      filtered_comments(show_all).where(parent_id: all_parent_ids)
                                 .group(:parent_id)
                                 .count
                                 .each do |parent_id, count|
        count_by_parent_id[parent_id] = count
        per_page_by_parent_id[parent_id] = per_page
      end

      next if per_page == 0

      children = all_parent_ids.empty? ? [] : Commontator::Comment.find_by_sql(
        all_parent_ids.map do |parent_id|
          Commontator::Comment.select(Arel.star).from(
            Arel::Nodes::TableAlias.new(
              Arel::Nodes::Grouping.new(
                Arel::Nodes::SqlLiteral.new(
                  ordered_comments(show_all).where(parent_id: parent_id).limit(per_page).to_sql
                )
              ), :commontator_comments
            )
          ).to_sql
        end.reduce { |memo, sql| memo.nil? ? sql : "#{memo} UNION ALL #{sql}" }
      )
      children.each { |comment| children_by_parent_id[comment.parent_id] << comment }
      all_parent_ids = children.map(&:id)
    end
  end

  Commontator::Collection.new(
    nest_comments(
      comments, root_per_page, per_page_by_parent_id, count_by_parent_id, children_by_parent_id
    ),
    total_entries,
    root_per_page,
    root_per_page,
    current_page
  ).tap do |nested_comments|
    next unless is_votable?

    if RAILS_7_PRELOADER
      ActiveRecord::Associations::Preloader.new(
        records: nested_comments.flatten,
        associations: :votes_for,
        scope: ActsAsVotable::Vote.where(voter: user)
      ).call
    else
      ActiveRecord::Associations::Preloader.new.preload(
        nested_comments.flatten, :votes_for, ActsAsVotable::Vote.where(voter: user)
      )
    end
  end
end

#new_comment_page(parent_id, show_all) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/commontator/thread.rb', line 166

def new_comment_page(parent_id, show_all)
  per_page = config.comments_per_page[0].to_i
  return 1 if per_page <= 0

  comment_order = config.comment_order.to_sym
  return 1 if comment_order == :l

  cp = comments_with_parent_id(parent_id, show_all)
  cc = Commontator::Comment.arel_table
  comment_index = case config.comment_order.to_sym
  when :l
    1 # First comment
  when :ve
    # Last comment with rating == 0
    cp.where((cc[:cached_votes_up] - cc[:cached_votes_down]).gteq(0)).count
  when :vl
    # First comment with rating == 0
    cp.where((cc[:cached_votes_up] - cc[:cached_votes_down]).gt(0)).count + 1
  else
    cp.count # Last comment
  end

  (comment_index.to_f/per_page).ceil
end

#ordered_comments(show_all) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/commontator/thread.rb', line 34

def ordered_comments(show_all)
  fc = filtered_comments(show_all)
  cc = Commontator::Comment.arel_table

  # ID is used as a tie-breaker because MySQL lacks sub-second timestamp resolution
  case config.comment_order.to_sym
  when :l
    fc.order(cc[:created_at].desc, cc[:id].desc)
  when :ve
    fc.order(
      Arel::Nodes::Descending.new(cc[:cached_votes_up] - cc[:cached_votes_down]),
      cc[:created_at].asc,
      cc[:id].asc
    )
  when :vl
    fc.order(
      Arel::Nodes::Descending.new(cc[:cached_votes_up] - cc[:cached_votes_down]),
      cc[:created_at].desc,
      cc[:id].desc
    )
  else
    fc.order(cc[:created_at].asc, cc[:id].asc)
  end
end

#paginated_comments(page, parent_id, show_all) ⇒ Object



78
79
80
81
82
# File 'app/models/commontator/thread.rb', line 78

def paginated_comments(page, parent_id, show_all)
  cp = comments_with_parent_id(parent_id, show_all)

  cp.paginate(page: page, per_page: config.comments_per_page[0])
end

#reopenObject



203
204
205
206
207
208
# File 'app/models/commontator/thread.rb', line 203

def reopen
  return false unless is_closed? && !commontable.nil?

  self.closed_at = nil
  save
end

#subscribe(subscriber) ⇒ Object



220
221
222
223
224
225
226
227
# File 'app/models/commontator/thread.rb', line 220

def subscribe(subscriber)
  return false unless subscriber.is_commontator && !subscription_for(subscriber)

  subscription = Commontator::Subscription.new
  subscription.subscriber = subscriber
  subscription.thread = self
  subscription.save
end

#subscribersObject



210
211
212
# File 'app/models/commontator/thread.rb', line 210

def subscribers
  subscriptions.map(&:subscriber)
end

#subscription_for(subscriber) ⇒ Object



214
215
216
217
218
# File 'app/models/commontator/thread.rb', line 214

def subscription_for(subscriber)
  return nil if !subscriber || !subscriber.is_commontator

  subscriber.commontator_subscriptions.find_by(thread_id: self.id)
end

#unsubscribe(subscriber) ⇒ Object



229
230
231
232
233
234
# File 'app/models/commontator/thread.rb', line 229

def unsubscribe(subscriber)
  subscription = subscription_for(subscriber)
  return false unless subscription

  subscription.destroy
end