Extract callbacks and validations for Post

Move Post create callbacks to PostCreate
Extract Post validations
Move stripped_length_validator to lib/validators
This commit is contained in:
Navin
2013-06-09 18:48:44 +02:00
parent 26a81b30c2
commit 3fdba0019b
4 changed files with 89 additions and 78 deletions

View File

@@ -73,12 +73,37 @@ class PostCreator
@post
end
# Shortcut
def self.create(user, opts)
PostCreator.new(user, opts).create
end
def self.before_create_tasks(post)
if post.reply_to_post_number.present?
post.reply_to_user_id ||= Post.select(:user_id).where(topic_id: post.topic_id, post_number: post.reply_to_post_number).first.try(:user_id)
end
post.post_number ||= Topic.next_post_number(post.topic_id, post.reply_to_post_number.present?)
post.cooked ||= post.cook(post.raw, topic_id: post.topic_id)
post.sort_order = post.post_number
DiscourseEvent.trigger(:before_create_post, post)
post.last_version_at ||= Time.now
end
def self.after_create_tasks(post)
Rails.logger.info (">" * 30) + "#{post.no_bump} #{post.created_at}"
# Update attributes on the topic - featured users and last posted.
attrs = {last_posted_at: post.created_at, last_post_user_id: post.user_id}
attrs[:bumped_at] = post.created_at unless post.no_bump
post.topic.update_attributes(attrs)
# Update topic user data
TopicUser.change(post.user,
post.topic.id,
posted: true,
last_read_post_number: post.post_number,
seen_post_count: post.post_number)
end
protected
def secure_group_ids(topic)