Class: Commontator::Thread
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Commontator::Thread
- Defined in:
- app/models/commontator/thread.rb
Instance Method Summary collapse
-
#can_be_edited_by?(user) ⇒ Boolean
Thread moderator capabilities.
-
#can_be_read_by?(user) ⇒ Boolean
Reader capabilities (user can be nil or false).
- #can_subscribe?(user) ⇒ Boolean
-
#clear ⇒ Object
Creates a new empty thread and assigns it to the commontable The old thread is kept in the database for archival purposes.
- #close(user = nil) ⇒ Object
- #config ⇒ Object
- #filtered_comments ⇒ Object
- #is_closed? ⇒ Boolean
- #is_filtered? ⇒ Boolean
- #mark_as_read_for(subscriber) ⇒ Object
- #new_comment_page(per_page = config.comments_per_page) ⇒ Object
- #ordered_comments(unfiltered = false) ⇒ Object
- #paginated_comments(page = 1, per_page = config.comments_per_page) ⇒ Object
- #reopen ⇒ Object
- #subscribe(subscriber) ⇒ Object
- #subscribers ⇒ Object
- #subscription_for(subscriber) ⇒ Object
- #unsubscribe(subscriber) ⇒ Object
- #will_paginate? ⇒ Boolean
Instance Method Details
#can_be_edited_by?(user) ⇒ Boolean
Thread moderator capabilities
146 147 148 149 |
# File 'app/models/commontator/thread.rb', line 146 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)
139 140 141 142 143 |
# File 'app/models/commontator/thread.rb', line 139 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
151 152 153 154 155 156 |
# File 'app/models/commontator/thread.rb', line 151 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 |
#clear ⇒ Object
Creates a new empty thread and assigns it to the commontable The old thread is kept in the database for archival purposes
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/commontator/thread.rb', line 118 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
74 75 76 77 78 79 |
# File 'app/models/commontator/thread.rb', line 74 def close(user = nil) return false if is_closed? self.closed_at = Time.now self.closer = user save end |
#config ⇒ Object
14 15 16 |
# File 'app/models/commontator/thread.rb', line 14 def config @config ||= commontable.try(:commontable_config) || Commontator end |
#filtered_comments ⇒ Object
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
70 71 72 |
# File 'app/models/commontator/thread.rb', line 70 def is_closed? !closed_at.blank? end |
#is_filtered? ⇒ 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
110 111 112 113 114 |
# File 'app/models/commontator/thread.rb', line 110 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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/commontator/thread.rb', line 50 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 |
# 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('id DESC') when :ve then vc.order('cached_votes_down - cached_votes_up') when :vl then vc.order('cached_votes_down - cached_votes_up', 'id DESC') else vc end end |
#paginated_comments(page = 1, per_page = config.comments_per_page) ⇒ Object
44 45 46 47 48 |
# File 'app/models/commontator/thread.rb', line 44 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 |
#reopen ⇒ Object
81 82 83 84 85 |
# File 'app/models/commontator/thread.rb', line 81 def reopen return false unless is_closed? && !commontable.nil? self.closed_at = nil save end |
#subscribe(subscriber) ⇒ Object
96 97 98 99 100 101 102 |
# File 'app/models/commontator/thread.rb', line 96 def subscribe(subscriber) return false unless subscriber.is_commontator && !subscription_for(subscriber) subscription = Subscription.new subscription.subscriber = subscriber subscription.thread = self subscription.save end |
#subscribers ⇒ Object
87 88 89 |
# File 'app/models/commontator/thread.rb', line 87 def subscribers subscriptions.collect{|s| s.subscriber} end |
#subscription_for(subscriber) ⇒ Object
91 92 93 94 |
# File 'app/models/commontator/thread.rb', line 91 def subscription_for(subscriber) return nil if !subscriber || !subscriber.is_commontator subscriber.subscriptions.where(:thread_id => self.id).first end |
#unsubscribe(subscriber) ⇒ Object
104 105 106 107 108 |
# File 'app/models/commontator/thread.rb', line 104 def unsubscribe(subscriber) subscription = subscription_for(subscriber) return false unless subscription subscription.destroy end |
#will_paginate? ⇒ 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 |