From 1c61ac1c64e136b391d472a24bbc5538352e299d Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 18 Feb 2014 20:17:26 +0200 Subject: [PATCH] Refactor MR code reload Signed-off-by: Dmitriy Zaporozhets --- app/controllers/projects/merge_requests_controller.rb | 3 --- app/models/merge_request.rb | 9 +++++++++ lib/api/merge_requests.rb | 2 -- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb index e3a0699ef3f..2b410c5a610 100644 --- a/app/controllers/projects/merge_requests_controller.rb +++ b/app/controllers/projects/merge_requests_controller.rb @@ -106,9 +106,6 @@ class Projects::MergeRequestsController < Projects::ApplicationController params[:merge_request].delete(:target_project_id) if @merge_request.update_attributes(params[:merge_request].merge(author_id_of_changes: current_user.id)) - @merge_request.reload_code - @merge_request.mark_as_unchecked - @merge_request.reset_events_cache respond_to do |format| diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index a3d786c2138..b596d7eb9cf 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -32,7 +32,9 @@ class MergeRequest < ActiveRecord::Base belongs_to :source_project, foreign_key: :source_project_id, class_name: "Project" has_one :merge_request_diff, dependent: :destroy + after_create :create_merge_request_diff + after_update :update_merge_request_diff delegate :commits, :diffs, :last_commit, :last_commit_short_sha, to: :merge_request_diff, prefix: nil @@ -125,6 +127,13 @@ class MergeRequest < ActiveRecord::Base end end + def update_merge_request_diff + if source_branch_changed? || target_branch_changed? + reload_code + mark_as_unchecked + end + end + def reload_code if merge_request_diff && opened? merge_request_diff.reload_content diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 0f62cac9a0c..58d2f79faff 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -116,8 +116,6 @@ module API authorize! :modify_merge_request, merge_request if merge_request.update_attributes attrs - merge_request.reload_code - merge_request.mark_as_unchecked present merge_request, with: Entities::MergeRequest else handle_merge_request_errors! merge_request.errors