Refactor `TopicsController` and remove code duplication

This commit is contained in:
Vipul A M 2013-06-18 11:22:09 +05:30
parent f13f6adc0b
commit 531587c5ca
1 changed files with 22 additions and 15 deletions

View File

@ -160,11 +160,7 @@ class TopicsController < ApplicationController
guardian.ensure_can_move_posts!(topic) guardian.ensure_can_move_posts!(topic)
dest_topic = topic.move_posts(current_user, topic.posts.pluck(:id), destination_topic_id: params[:destination_topic_id].to_i) dest_topic = topic.move_posts(current_user, topic.posts.pluck(:id), destination_topic_id: params[:destination_topic_id].to_i)
if dest_topic.present? render_topic_changes(dest_topic)
render json: {success: true, url: dest_topic.relative_url}
else
render json: {success: false}
end
end end
def move_posts def move_posts
@ -173,16 +169,8 @@ class TopicsController < ApplicationController
topic = Topic.where(id: params[:topic_id]).first topic = Topic.where(id: params[:topic_id]).first
guardian.ensure_can_move_posts!(topic) guardian.ensure_can_move_posts!(topic)
args = {} dest_topic = move_post_to_destination(topic)
args[:title] = params[:title] if params[:title].present? render_topic_changes(dest_topic)
args[:destination_topic_id] = params[:destination_topic_id].to_i if params[:destination_topic_id].present?
dest_topic = topic.move_posts(current_user, params[:post_ids].map {|p| p.to_i}, args)
if dest_topic.present?
render json: {success: true, url: dest_topic.relative_url}
else
render json: {success: false}
end
end end
def clear_pin def clear_pin
@ -258,4 +246,23 @@ class TopicsController < ApplicationController
end end
end end
end end
def render_topic_changes(dest_topic)
if dest_topic.present?
render json: {success: true, url: dest_topic.relative_url}
else
render json: {success: false}
end
end
private
def move_post_to_destination(topic)
args = {}
args[:title] = params[:title] if params[:title].present?
args[:destination_topic_id] = params[:destination_topic_id].to_i if params[:destination_topic_id].present?
topic.move_posts(current_user, params[:post_ids].map {|p| p.to_i}, args)
end
end end