Class: Formol::Topic
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Formol::Topic
- Includes:
- Tracking
- Defined in:
- app/models/formol/topic.rb
Defined Under Namespace
Classes: Subscription, Track
Instance Attribute Summary collapse
-
#attach_poll ⇒ Object
Virtual attributes.
Class Method Summary collapse
-
.ordered_for_listing ⇒ Object
scope for ordering topics conveniently for listing views order by updated_at and id id is used as discard if two topics have been updated at the same second.
-
.ready_for_breadcrumb ⇒ Object
this scope eager loads conveniently topic for using it in breadcrumb.
-
.ready_for_listing ⇒ Object
scope to get topics conveniently prepared for listing.
-
.unread(user, opts = {}) ⇒ Object
return all unread topics for a given forum and a given user A topic is unread if it’s * tracked for given user and marked_at is after last post’s created_at * not tracked and created after user registered.
Instance Method Summary collapse
-
#first_post?(post) ⇒ Boolean
Return if one post is the first post or not Should be in Formol::Post class but while :inverse_of does not work on has_many, optimization is easier in this sense.
- #lock_topic! ⇒ Object
-
#pin_topic! ⇒ Object
Business methods.
-
#register_last_post(post) ⇒ Object
Register a post being the last one of the current topic.
-
#register_subscriber(subscriber) ⇒ Object
Register a subscriber to the current topic.
-
#subscriber?(user) ⇒ Boolean
Tells if a user has subscribed (or not) to the current topic.
-
#track_for_user!(user) ⇒ Object
Track current topic for a given user.
- #unlock_topic! ⇒ Object
- #unpin_topic! ⇒ Object
-
#unregister_last_post ⇒ Object
Unregister a post by re-assigning last post.
-
#unregister_subscriber(subscriber) ⇒ Object
Remove a user from subscribers.
-
#view! ⇒ Object
Increments views counter.
Instance Attribute Details
#attach_poll ⇒ Object
Virtual attributes
11 12 13 |
# File 'app/models/formol/topic.rb', line 11 def attach_poll @attach_poll end |
Class Method Details
.ordered_for_listing ⇒ Object
scope for ordering topics conveniently for listing views order by updated_at and id id is used as discard if two topics have been updated at the same second
56 57 58 59 60 |
# File 'app/models/formol/topic.rb', line 56 def ordered_for_listing order('formol_topics.pinned DESC'). order('formol_topics.updated_at DESC'). order('formol_topics.id DESC') end |
.ready_for_breadcrumb ⇒ Object
this scope eager loads conveniently topic for using it in breadcrumb
68 69 70 |
# File 'app/models/formol/topic.rb', line 68 def includes(:forum => :category) end |
.ready_for_listing ⇒ Object
scope to get topics conveniently prepared for listing
63 64 65 |
# File 'app/models/formol/topic.rb', line 63 def ready_for_listing ordered_for_listing.includes(:user, { :last_post => :user }, :posts) end |
.unread(user, opts = {}) ⇒ Object
return all unread topics for a given forum and a given user A topic is unread if it’s
-
tracked for given user and marked_at is after last post’s created_at
-
not tracked and created after user registered
76 77 78 79 80 |
# File 'app/models/formol/topic.rb', line 76 def unread(user, opts = {}) unread_scope_base(user, opts) .select('formol_topics.id') .joins(:last_post) end |
Instance Method Details
#first_post?(post) ⇒ Boolean
Return if one post is the first post or not Should be in Formol::Post class but while :inverse_of does not work on has_many, optimization is easier in this sense
115 116 117 |
# File 'app/models/formol/topic.rb', line 115 def first_post?(post) post == first_post end |
#lock_topic! ⇒ Object
93 94 95 |
# File 'app/models/formol/topic.rb', line 93 def lock_topic! update_attribute(:locked, true) end |
#pin_topic! ⇒ Object
Business methods
85 86 87 |
# File 'app/models/formol/topic.rb', line 85 def pin_topic! update_attribute(:pinned, true) end |
#register_last_post(post) ⇒ Object
Register a post being the last one of the current topic
120 121 122 |
# File 'app/models/formol/topic.rb', line 120 def register_last_post(post) update_attribute(:last_post, post) end |
#register_subscriber(subscriber) ⇒ Object
Register a subscriber to the current topic
131 132 133 |
# File 'app/models/formol/topic.rb', line 131 def register_subscriber(subscriber) subscriptions.create!(:subscriber => subscriber, :topic => self) end |
#subscriber?(user) ⇒ Boolean
Tells if a user has subscribed (or not) to the current topic
142 143 144 |
# File 'app/models/formol/topic.rb', line 142 def subscriber?(user) subscriptions.where(:subscriber_id => user.id).exists? end |
#track_for_user!(user) ⇒ Object
Track current topic for a given user
108 109 110 |
# File 'app/models/formol/topic.rb', line 108 def track_for_user!(user) Track.mark_topic_for_user(self, user) end |
#unlock_topic! ⇒ Object
97 98 99 |
# File 'app/models/formol/topic.rb', line 97 def unlock_topic! update_attribute(:locked, false) end |
#unpin_topic! ⇒ Object
89 90 91 |
# File 'app/models/formol/topic.rb', line 89 def unpin_topic! update_attribute(:pinned, false) end |
#unregister_last_post ⇒ Object
Unregister a post by re-assigning last post. Needed when a post is destroyed
126 127 128 |
# File 'app/models/formol/topic.rb', line 126 def unregister_last_post update_attribute(:last_post, posts(true).last) end |
#unregister_subscriber(subscriber) ⇒ Object
Remove a user from subscribers
136 137 138 139 |
# File 'app/models/formol/topic.rb', line 136 def unregister_subscriber(subscriber) #subscriptions.where(:subscriber_id => subscriber.id).try(:destroy) subscribers.destroy(subscriber) end |
#view! ⇒ Object
Increments views counter
102 103 104 105 |
# File 'app/models/formol/topic.rb', line 102 def view! # Use class method instead of instance to avoid concurrence problems self.class.increment_counter(:views_count, id) end |