Class: Thredded::Topic

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

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

.find_by_slug!(slug) ⇒ Object



80
81
82
83
84
# File 'app/models/thredded/topic.rb', line 80

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

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

Parameters:

Returns:



101
102
103
104
105
106
107
108
109
# File 'app/models/thredded/topic.rb', line 101

def with_read_and_follow_states(user)
  null_read_state = Thredded::NullUserTopicReadState.new
  return current_scope.zip([null_read_state, nil]) if user.thredded_anonymous?
  read_states_by_topic = read_states_by_postable_hash(user)
  follows_by_topic = follows_by_topic_hash(user)
  current_scope.map do |topic|
    [topic, read_states_by_topic[topic] || null_read_state, follows_by_topic[topic]]
  end
end

Instance Method Details

#last_moderation_recordThredded::PostModerationRecord?



121
122
123
# File 'app/models/thredded/topic.rb', line 121

def last_moderation_record
  first_post.try(:last_moderation_record)
end

#public?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/thredded/topic.rb', line 112

def public?
  true
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/thredded/topic.rb', line 116

def should_generate_new_friendly_id?
  title_changed?
end

#update_last_user_and_time_from_last_post!Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/thredded/topic.rb', line 125

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