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



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

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



56
57
58
59
60
# File 'app/models/commontator/thread.rb', line 56

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)


113
114
115
116
117
# File 'app/models/commontator/thread.rb', line 113

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)


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

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)


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

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



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

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



67
68
69
70
71
72
# File 'app/models/commontator/thread.rb', line 67

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)


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

def is_closed?
  !closed_at.blank?
end

#is_subscribed?(subscriber) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mark_as_read_for(subscriber) ⇒ Object



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

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

#ordered_commentsObject



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

def ordered_comments
  (config.can_vote_on_comments && config.comments_ordered_by_votes) ? \
    comments.order("cached_votes_down - cached_votes_up") : comments
end

#reopenObject



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

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

#subscribe(subscriber) ⇒ Object



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

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



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

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

#subscription_for(subscriber) ⇒ Object



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

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

#unsubscribe(subscriber) ⇒ Object



50
51
52
53
54
# File 'app/models/commontator/thread.rb', line 50

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