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 admin capabilities

Returns:

  • (Boolean)


133
134
135
136
137
# File 'app/models/commontator/thread.rb', line 133

def can_be_edited_by?(user) # Thread admin capabilities
  !commontable.blank? && (config.can_edit_thread_method.blank? ?
    (user.commontator_config.is_admin_method.blank? ? false : user.send(user.commontator_config.is_admin_method)) :
    commontable.send(config.can_edit_thread_method, user))
end

#can_be_read_by?(user) ⇒ Boolean

Access control methods #

Returns:

  • (Boolean)


127
128
129
130
131
# File 'app/models/commontator/thread.rb', line 127

def can_be_read_by?(user) # Reader and poster capabilities
  (!commontable.blank? && (!is_closed? || config.closed_threads_are_readable) &&\
    config.can_read_thread_method.blank? ? true : commontable.send(config.can_read_thread_method, user)) ||\
    can_be_edited_by?(user)
end

#can_subscribe?(user) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/commontator/thread.rb', line 139

def can_subscribe?(user)
  !commontable.blank? && config.can_subscribe_to_thread && !is_closed? && 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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/commontator/thread.rb', line 77

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

#close(user = nil) ⇒ Object



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

def close(user = nil)
  self.closed_at = Time.now
  self.closer = user
  self.save!
end

#comment_created_callback(user, comment) ⇒ Object

Callback methods #



99
100
101
# File 'app/models/commontator/thread.rb', line 99

def comment_created_callback(user, comment)
  commontable.blank? || config.comment_created_callback.blank? || commontable.send(config.comment_created_callback, user, comment)
end

#comment_deleted_callback(user, comment) ⇒ Object



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

def comment_deleted_callback(user, comment)
  commontable.blank? || config.comment_deleted_callback.blank? || commontable.send(config.comment_deleted_callback, user, comment)
end

#comment_edited_callback(user, comment) ⇒ Object



103
104
105
# File 'app/models/commontator/thread.rb', line 103

def comment_edited_callback(user, comment)
  commontable.blank? || config.comment_edited_callback.blank? || commontable.send(config.comment_edited_callback, user, comment)
end

#configObject



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

def config
  commontable.try(:commontable_config)
end

#is_closed?Boolean

Returns:

  • (Boolean)


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

def is_closed?
  !closed_at.blank?
end

#is_subscribed?(subscriber) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#mark_as_read_for(subscriber) ⇒ Object



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

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

#mark_as_unread_except_for(subscriber) ⇒ Object



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

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

#ordered_commentsObject



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

def ordered_comments
  (!commontable.blank? && config.comments_can_be_voted_on && config.comments_ordered_by_votes) ? \
    comments.order("cached_votes_down - cached_votes_up") : comments
end

#reopenObject



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

def reopen
  self.closed_at = nil
  self.save!
end

#subscribe(subscriber) ⇒ Object



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

def subscribe(subscriber)
  return false if is_subscribed?(subscriber)
  subscription = Subscription.create(
    :subscriber => subscriber, :thread => self)
  subscribe_callback(subscriber)
end

#subscribe_callback(user) ⇒ Object



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

def subscribe_callback(user)
  commontable.blank? || config.subscribe_callback.blank? || commontable.send(config.subscribe_callback, user)
end

#subscribersObject



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

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

#subscription_for(subscriber) ⇒ Object



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

def subscription_for(subscriber)
  Subscription.find_by_thread_id_and_subscriber_id_and_subscriber_type(self.id, subscriber.id, subscriber.class.name)
end

#thread_closed_callback(user) ⇒ Object



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

def thread_closed_callback(user)
  commontable.blank? || config.thread_closed_callback.blank? || commontable.send(config.thread_closed_callback, user)
end

#unsubscribe(subscriber) ⇒ Object



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

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

#unsubscribe_callback(user) ⇒ Object



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

def unsubscribe_callback(user)
  commontable.blank? || config.unsubscribe_callback.blank? || commontable.send(config.unsubscribe_callback, user)
end