Class: Thredded::Messageboard

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/thredded/messageboard.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.friendly_find!(slug_or_id) ⇒ Thredded::Messageboard

Finds the messageboard by its slug or ID, or raises Thredded::Errors::MessageboardNotFound.

Parameters:

  • slug_or_id (String)

Returns:

Raises:


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

Parameters:


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/thredded/messageboard.rb', line 131

def unread_topics_counts(user:, topics_scope: Thredded::Topic.all)
  messageboards = arel_table
  read_states = Thredded::UserTopicReadState.arel_table
  topics = topics_scope.arel_table

  read_states_join_cond =
    messageboards[: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(
    messageboards.outer_join(read_states).on(read_states_join_cond).join_sources
  ).group(messageboards[:id])
  Thredded::ArelCompat.pluck(
    relation,
    :id,
    Arel::Nodes::Subtraction.new(topics[:id].count, read_states[:id].count)
  ).to_h
end

Instance Method Details

#ensure_positionObject


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_userObject


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