#698:small refactoring
This commit is contained in:
parent
5cf69bff92
commit
0cf0933074
|
@ -3,7 +3,7 @@ class Api::V1::AdvisoriesController < Api::V1::BaseController
|
|||
before_filter :authenticate_user!
|
||||
skip_before_filter :authenticate_user!, :only => [:index, :show] if APP_CONFIG['anonymous_access']
|
||||
load_resource :advisory, :find_by => :advisory_id
|
||||
before_filter :find_build_list, :only => [:create, :update]
|
||||
before_filter :find_and_authorize_build_list, :only => [:create, :update]
|
||||
authorize_resource :build_list, :only => [:create, :update]
|
||||
|
||||
def index
|
||||
|
@ -16,8 +16,9 @@ class Api::V1::AdvisoriesController < Api::V1::BaseController
|
|||
end
|
||||
|
||||
def create
|
||||
@advisory = @build_list.build_and_associate_advisory(params[:advisory])
|
||||
if can_attach? && @advisory.save && @build_list.save
|
||||
if @build_list.can_attach? &&
|
||||
@build_list.associate_and_create_advisory(params[:advisory]) &&
|
||||
@build_list.save
|
||||
render_json_response @advisory, 'Advisory has been created successfully'
|
||||
else
|
||||
render_validation_error @advisory, error_message(@build_list, 'Advisory has not been created')
|
||||
|
@ -25,9 +26,8 @@ class Api::V1::AdvisoriesController < Api::V1::BaseController
|
|||
end
|
||||
|
||||
def update
|
||||
if @advisory && can_attach?
|
||||
@advisory.attach_build_list(@build_list) &&
|
||||
@advisory.save && @build_list.save
|
||||
if @advisory && @build_list.can_attach? &&
|
||||
@advisory.attach_build_list(@build_list) && @build_list.save
|
||||
render_json_response @advisory, "Build list '#{@build_list.id}' has been attached to advisory successfully"
|
||||
else
|
||||
render_validation_error @advisory, error_message(@build_list, 'Build list has not been attached to advisory')
|
||||
|
@ -36,15 +36,9 @@ class Api::V1::AdvisoriesController < Api::V1::BaseController
|
|||
|
||||
protected
|
||||
|
||||
def find_build_list
|
||||
def find_and_authorize_build_list
|
||||
@build_list = BuildList.find params[:build_list_id]
|
||||
end
|
||||
|
||||
def can_attach?
|
||||
!@build_list.save_to_repository.publish_without_qa &&
|
||||
can?(:update, @build_list.save_to_platform) &&
|
||||
@build_list.save_to_platform.released &&
|
||||
@build_list.status == BuildList::BUILD_PUBLISHED
|
||||
authorize! :update, @build_list.save_to_platform
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -196,14 +196,13 @@ class Projects::BuildListsController < Projects::BaseController
|
|||
|
||||
if params[:attach_advisory] == 'new'
|
||||
# create new advisory
|
||||
advisory = @build_list.build_and_associate_advisory(params[:build_list][:advisory])
|
||||
unless advisory.save
|
||||
unless @build_list.associate_and_create_advisory(params[:build_list][:advisory])
|
||||
redirect_to :back, :notice => t('layout.build_lists.publish_fail') and return
|
||||
end
|
||||
else
|
||||
# attach existing advisory
|
||||
a = Advisory.where(:advisory_id => params[:attach_advisory]).limit(1).first
|
||||
if !(a && a.attach_build_list(@build_list) && a.save)
|
||||
a = Advisory.where(:advisory_id => params[:attach_advisory]).first
|
||||
unless (a && a.attach_build_list(@build_list))
|
||||
redirect_to :back, :notice => t('layout.build_lists.publish_fail') and return
|
||||
end
|
||||
end
|
||||
|
|
|
@ -26,7 +26,7 @@ class Advisory < ActiveRecord::Base
|
|||
self.platforms << build_list.save_to_platform unless platforms.include? build_list.save_to_platform
|
||||
self.projects << build_list.project unless projects.include? build_list.project
|
||||
build_list.advisory = self
|
||||
true
|
||||
save
|
||||
end
|
||||
|
||||
# this method fetches and structurize packages attached to current advisory.
|
||||
|
|
|
@ -303,12 +303,15 @@ class BuildList < ActiveRecord::Base
|
|||
#[WAITING_FOR_RESPONSE, BuildServer::BUILD_PENDING, BuildServer::BUILD_STARTED].include?(status)
|
||||
end
|
||||
|
||||
def build_and_associate_advisory(params)
|
||||
build_advisory(params) do |a|
|
||||
a.update_type = update_type
|
||||
a.projects << project
|
||||
a.platforms << save_to_platform unless a.platforms.include? save_to_platform
|
||||
end
|
||||
def associate_and_create_advisory(params)
|
||||
build_advisory(params){ |a| a.update_type = update_type }
|
||||
advisory.attach_build_list(self)
|
||||
end
|
||||
|
||||
def can_attach?
|
||||
!save_to_repository.publish_without_qa &&
|
||||
save_to_platform.released &&
|
||||
status == BUILD_PUBLISHED
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
Loading…
Reference in New Issue