Class: Thredded::Messageboard
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Thredded::Messageboard
- Extended by:
- FriendlyId
- Defined in:
- app/models/thredded/messageboard.rb
Class Method Summary collapse
-
.friendly_find!(slug_or_id) ⇒ Thredded::Messageboard
Finds the messageboard by its slug or ID, or raises Thredded::Errors::MessageboardNotFound.
- .unread_topics_counts(user:, topics_scope: Thredded::Topic.all) ⇒ Object
Instance Method Summary collapse
- #ensure_position ⇒ Object
- #last_user ⇒ Object
- #normalize_friendly_id(input) ⇒ Object
- #update_last_topic! ⇒ Object
Class Method Details
.friendly_find!(slug_or_id) ⇒ Thredded::Messageboard
Finds the messageboard by its slug or ID, or raises Thredded::Errors::MessageboardNotFound.
99 100 101 102 103 |
# File 'app/models/thredded/messageboard.rb', line 99 def self.friendly_find!(slug_or_id) friendly.find(slug_or_id) rescue ActiveRecord::RecordNotFound raise Thredded::Errors::MessageboardNotFound end |
.unread_topics_counts(user:, topics_scope: Thredded::Topic.all) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/thredded/messageboard.rb', line 131 def unread_topics_counts(user:, topics_scope: Thredded::Topic.all) = arel_table read_states = Thredded::UserTopicReadState.arel_table topics = topics_scope.arel_table read_states_join_cond = [:id].eq(read_states[:messageboard_id]) .and(read_states[:postable_id].eq(topics[:id])) .and(read_states[:user_id].eq(user.id)) .and(read_states[:unread_posts_count].eq(0)) relation = joins(:topics).merge(topics_scope).joins( .outer_join(read_states).on(read_states_join_cond).join_sources ).group([:id]) relation.pluck( :id, Arel::Nodes::Subtraction.new(topics[:id].count, read_states[:id].count) ).to_h end |
Instance Method Details
#ensure_position ⇒ Object
33 34 35 |
# File 'app/models/thredded/messageboard.rb', line 33 def ensure_position self.position ||= (created_at || Time.zone.now).to_i end |
#last_user ⇒ Object
105 106 107 |
# File 'app/models/thredded/messageboard.rb', line 105 def last_user last_topic.try(:last_user) end |
#normalize_friendly_id(input) ⇒ Object
115 116 117 |
# File 'app/models/thredded/messageboard.rb', line 115 def normalize_friendly_id(input) Thredded.slugifier.call(input.to_s) end |
#update_last_topic! ⇒ Object
109 110 111 112 113 |
# File 'app/models/thredded/messageboard.rb', line 109 def update_last_topic! return if destroyed? self.last_topic = topics.order_recently_posted_first.moderation_state_visible_to_all.first save! if last_topic_id_changed? end |