Class: Commontator::Thread

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

Instance Method Summary collapse

Instance Method Details

#active_subscribersObject



33
34
35
# File 'app/models/commontator/thread.rb', line 33

def active_subscribers
  subscribers.select{|s| s.is_commontator && s.commontator_config.subscription_email_enable_proc.call(s)}
end

#add_unread_except_for(subscriber) ⇒ Object



60
61
62
63
64
# File 'app/models/commontator/thread.rb', line 60

def add_unread_except_for(subscriber)
  Subscription.transaction do
    subscriptions.each{|s| s.add_unread unless s.subscriber == subscriber}
  end
end

#can_be_edited_by?(user) ⇒ Boolean

Thread moderator capabilities

Returns:

  • (Boolean)


117
118
119
120
121
# File 'app/models/commontator/thread.rb', line 117

def can_be_edited_by?(user)
  !commontable.nil? && user && user.is_commontator &&\
  (user.commontator_config.user_admin_proc.call(user) ||\
    config.can_edit_thread_proc.call(self, user))
end

#can_be_read_by?(user) ⇒ Boolean

Reader capabilities (user can be nil or false)

Returns:

  • (Boolean)


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

def can_be_read_by?(user)
  (!commontable.nil? &&\
    (!is_closed? || config.closed_threads_are_readable) &&\
    config.can_read_thread_proc.call(self, user)) ||\
  can_be_edited_by?(user)
end

#can_subscribe?(user) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
# File 'app/models/commontator/thread.rb', line 123

def can_subscribe?(user)
  !is_closed? && user && user.is_commontator &&\
  config.can_subscribe_to_thread && can_be_read_by?(user)
end

#clear(user = nil) ⇒ Object

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



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

def clear(user = nil)
  return if commontable.blank?
  new_thread = Thread.new
  new_thread.commontable = commontable
  with_lock do
    self.commontable = nil
    self.closed_at = Time.now
    self.closer = user
    save!
    new_thread.save!
    subscriptions.each do |s|
      s.thread = new_thread
      s.save!
      s.mark_as_read
    end
  end
end

#close(user = nil) ⇒ Object



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

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

#configObject



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

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

#is_closed?Boolean

Returns:

  • (Boolean)


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

def is_closed?
  !closed_at.blank?
end

#is_subscribed?(subscriber) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/commontator/thread.rb', line 42

def is_subscribed?(subscriber)
  !subscription_for(subscriber).blank?
end

#mark_as_read_for(subscriber) ⇒ Object



66
67
68
69
# File 'app/models/commontator/thread.rb', line 66

def mark_as_read_for(subscriber)
  return if !subscription_for(subscriber)
  subscription_for(subscriber).mark_as_read
end

#ordered_commentsObject



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

def ordered_comments
  case config.comments_order
    when :l then comments.order('id DESC')
    when :ve then comments.order('cached_votes_down - cached_votes_up')
    when :vl then comments.order('cached_votes_down - cached_votes_up', 'id DESC')
    else comments
  end
end

#reopenObject



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

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

#subscribe(subscriber) ⇒ Object



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

def subscribe(subscriber)
  return false if is_subscribed?(subscriber) || !subscriber.is_commontator
  subscription = Subscription.new
  subscription.subscriber = subscriber
  subscription.thread = self
  subscription.save
end

#subscribersObject



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

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

#subscription_for(subscriber) ⇒ Object



37
38
39
40
# File 'app/models/commontator/thread.rb', line 37

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

#unsubscribe(subscriber) ⇒ Object



54
55
56
57
58
# File 'app/models/commontator/thread.rb', line 54

def unsubscribe(subscriber)
  subscription = subscription_for(subscriber)
  return false if subscription.blank?
  subscription.destroy
end