Class: Formol::Topic

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Tracking
Defined in:
app/models/formol/topic.rb

Defined Under Namespace

Classes: Subscription, Track

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attach_pollObject

Virtual attributes



11
12
13
# File 'app/models/formol/topic.rb', line 11

def attach_poll
  @attach_poll
end

Class Method Details

.ordered_for_listingObject

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



54
55
56
57
58
# File 'app/models/formol/topic.rb', line 54

def ordered_for_listing
  order('formol_topics.pinned DESC').
  order('formol_topics.updated_at DESC').
  order('formol_topics.id DESC')
end

.ready_for_breadcrumbObject

this scope eager loads conveniently topic for using it in breadcrumb



66
67
68
# File 'app/models/formol/topic.rb', line 66

def ready_for_breadcrumb
  includes(:forum => :category)
end

.ready_for_listingObject

scope to get topics conveniently prepared for listing



61
62
63
# File 'app/models/formol/topic.rb', line 61

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



74
75
76
77
78
# File 'app/models/formol/topic.rb', line 74

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

Returns:

  • (Boolean)


97
98
99
# File 'app/models/formol/topic.rb', line 97

def first_post?(post)
  post == first_post
end

#register_last_post(post) ⇒ Object

Register a post being the last one of the current topic



102
103
104
# File 'app/models/formol/topic.rb', line 102

def register_last_post(post)
  update_attribute(:last_post, post)
end

#register_subscriber(subscriber) ⇒ Object

Register a subscriber to the current topic



113
114
115
# File 'app/models/formol/topic.rb', line 113

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

Returns:

  • (Boolean)


124
125
126
# File 'app/models/formol/topic.rb', line 124

def subscriber?(user)
  subscriptions.where(:subscriber_id => user.id).exists?
end

#track_for_user!(user) ⇒ Object

Track current topic for a given user



90
91
92
# File 'app/models/formol/topic.rb', line 90

def track_for_user!(user)
  Track.mark_topic_for_user(self, user)
end

#unregister_last_postObject

Unregister a post by re-assigning last post. Needed when a post is destroyed



108
109
110
# File 'app/models/formol/topic.rb', line 108

def unregister_last_post
  update_attribute(:last_post, posts(true).last)
end

#unregister_subscriber(subscriber) ⇒ Object

Remove a user from subscribers



118
119
120
121
# File 'app/models/formol/topic.rb', line 118

def unregister_subscriber(subscriber)
  #subscriptions.where(:subscriber_id => subscriber.id).try(:destroy)
  subscribers.destroy(subscriber)
end

#view!Object

Increments views counter



84
85
86
87
# File 'app/models/formol/topic.rb', line 84

def view!
  # Use class method instead of instance to avoid concurrence problems
  self.class.increment_counter(:views_count, id)
end