Class: Thredded::Topic
Overview
rubocop:disable Metrics/ClassLength
Class Method Summary
collapse
Instance Method Summary
collapse
#moderation_state_visible_to_all?, #moderation_state_visible_to_user?
#last_user, #private?, #user
Class Method Details
Finds the topic by its slug or ID, or raises Thredded::Errors::TopicNotFound.
97
98
99
100
101
|
# File 'app/models/thredded/topic.rb', line 97
def self.friendly_find!(slug_or_id)
friendly.find(slug_or_id)
rescue ActiveRecord::RecordNotFound
raise Thredded::Errors::TopicNotFound
end
|
118
119
120
121
122
123
124
125
126
|
# File 'app/models/thredded/topic.rb', line 118
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
134
135
136
|
# File 'app/models/thredded/topic.rb', line 134
def last_moderation_record
first_post.try(:last_moderation_record)
end
|
#normalize_friendly_id(input) ⇒ Object
156
157
158
|
# File 'app/models/thredded/topic.rb', line 156
def normalize_friendly_id(input)
Thredded.slugifier.call(input.to_s)
end
|
#public? ⇒ Boolean
129
130
131
|
# File 'app/models/thredded/topic.rb', line 129
def public?
true
end
|
#should_generate_new_friendly_id? ⇒ Boolean
152
153
154
|
# File 'app/models/thredded/topic.rb', line 152
def should_generate_new_friendly_id?
title_changed?
end
|
#update_last_user_and_time_from_last_post! ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'app/models/thredded/topic.rb', line 138
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
update_columns(last_user_id: nil, last_post_at: created_at, updated_at: Time.zone.now)
end
end
|