The belongs_to_creator macro was used to initialize the creator_id field to the CurrentUser. This made tests complicated because it meant you had to create and set the current user every time you wanted to create an object, when lead to the current user being set over and over again. It also meant you had to constantly be aware of what the CurrentUser was in many different contexts, which was often confusing. Setting creators explicitly simplifies everything greatly.
39 lines
1001 B
Ruby
39 lines
1001 B
Ruby
class ForumUpdater
|
|
attr_reader :forum_topic, :forum_post, :expected_title
|
|
|
|
def initialize(forum_topic, options = {})
|
|
@forum_topic = forum_topic
|
|
@forum_post = options[:forum_post]
|
|
@expected_title = options[:expected_title]
|
|
@skip_update = options[:skip_update]
|
|
end
|
|
|
|
def update(message, title_tag = nil)
|
|
return if forum_topic.nil?
|
|
|
|
CurrentUser.scoped(User.system) do
|
|
create_response(message)
|
|
update_title(title_tag) if title_tag
|
|
|
|
if forum_post
|
|
update_post(message)
|
|
end
|
|
end
|
|
end
|
|
|
|
def create_response(body)
|
|
forum_topic.posts.create(body: body, skip_mention_notifications: true, creator: User.system)
|
|
end
|
|
|
|
def update_title(title_tag)
|
|
if forum_topic.title == expected_title
|
|
forum_topic.update(:title => "[#{title_tag}] #{forum_topic.title}")
|
|
end
|
|
end
|
|
|
|
def update_post(body)
|
|
return if @skip_update
|
|
forum_post.update(body: "#{forum_post.body}\n\nEDIT: #{body}", skip_mention_notifications: true)
|
|
end
|
|
end
|