Class: Thredded::Topic

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Includes:
ContentModerationState, TopicCommon
Defined in:
app/models/thredded/topic.rb

Overview

rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContentModerationState

#moderation_state_visible_to_all?, #moderation_state_visible_to_user?

Methods included from TopicCommon

#last_user, #private?, #user

Class Method Details

.friendly_find!(slug_or_id) ⇒ Thredded::Topic

Finds the topic by its slug or ID, or raises Thredded::Errors::TopicNotFound.

Parameters:

  • slug_or_id (String)

Returns:

Raises:



99
100
101
102
103
# File 'app/models/thredded/topic.rb', line 99

def self.friendly_find!(slug_or_id)
  friendly.find(slug_or_id)
rescue ActiveRecord::RecordNotFound
  raise Thredded::Errors::TopicNotFound
end

.post_classObject



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

def post_class
  Thredded::Post
end

.with_read_and_follow_states(user) ⇒ Array<[TopicCommon, UserTopicReadStateCommon, UserTopicFollow]>

Parameters:

Returns:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/thredded/topic.rb', line 124

def with_read_and_follow_states(user)
  topics = current_scope.to_a
  if user.thredded_anonymous?
    post_counts = post_counts_for_user_and_topics(user, topics.map(&:id))
    topics.map do |topic|
      [topic, Thredded::NullUserTopicReadState.new(posts_count: post_counts[topic.id] || 0), nil]
    end
  else
    read_states_by_topic = read_states_by_postable_hash(user)
    post_counts = post_counts_for_user_and_topics(
      user, topics.reject { |topic| read_states_by_topic.key?(topic) }.map(&:id)
    )
    follows_by_topic = follows_by_topic_hash(user)
    current_scope.map do |topic|
      [
        topic,
        read_states_by_topic[topic] ||
          Thredded::NullUserTopicReadState.new(posts_count: post_counts[topic.id] || 0),
        follows_by_topic[topic]
      ]
    end
  end
end

Instance Method Details

#last_moderation_recordThredded::PostModerationRecord?



154
155
156
# File 'app/models/thredded/topic.rb', line 154

def last_moderation_record
  first_post.try(:last_moderation_record)
end

#normalize_friendly_id(input) ⇒ Object



176
177
178
# File 'app/models/thredded/topic.rb', line 176

def normalize_friendly_id(input)
  Thredded.slugifier.call(input.to_s)
end

#public?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/models/thredded/topic.rb', line 149

def public?
  true
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'app/models/thredded/topic.rb', line 172

def should_generate_new_friendly_id?
  title_changed?
end

#update_last_user_and_time_from_last_post!Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/thredded/topic.rb', line 158

def update_last_user_and_time_from_last_post!
  return if destroyed?
  scope = posts.order_newest_first
  scope = scope.moderation_state_visible_to_all if moderation_state_visible_to_all?
  last_post = scope.select(:user_id, :created_at).first
  if last_post
    update_columns(last_user_id: last_post.user_id, last_post_at: last_post.created_at, updated_at: Time.zone.now)
  else
    # Either a visible topic is left with no visible posts, or an invisible topic is left with no posts at all.
    # This shouldn't happen in stock Thredded.
    update_columns(last_user_id: nil, last_post_at: created_at, updated_at: Time.zone.now)
  end
end