Skip to content

Commit

Permalink
Add callback for managers assignment
Browse files Browse the repository at this point in the history
When we change the managers group of a staging_workflow we trigger an
`before_update` to update the managers group of related the staging
projects.
  • Loading branch information
DavidKang authored and Moises Deniz Aleman committed Nov 19, 2018
1 parent 619b365 commit bcdc6a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/api/app/controllers/webui/staging/workflows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,11 @@ def destroy
def update
authorize @staging_workflow

begin
Staging::Workflow.transaction do
old_managers_group = @staging_workflow.managers_group
new_managers_group = Group.find_by(title: params[:managers_title])

@staging_workflow.staging_projects.each do |staging_project|
staging_project.unassign_managers_group(old_managers_group)
staging_project.assign_managers_group(new_managers_group)
staging_project.store
end

@staging_workflow.managers_group = new_managers_group
@staging_workflow.save!
end
flash[:success] = 'Managers group if staging was successfully assigned'
rescue
flash[:error] = "Sorry, something unexpected happended and the group couldn't be assigned to this staging"
@staging_workflow.managers_group = Group.find_by(title: params[:managers_title])
if @staging_workflow.save
flash[:success] = 'Managers group was successfully assigned'
else
flash[:error] = "Managers group couldn't be assigned: #{@staging_workflow.errors.full_messages.to_sentence}."
end

redirect_to edit_staging_workflow_path(@staging_workflow)
Expand Down
18 changes: 18 additions & 0 deletions src/api/app/models/staging/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def ready_to_stage
validates :managers_group, presence: true

after_create :create_staging_projects
before_update :update_staging_projects_managers_group

def unassigned_requests
target_of_bs_requests.stageable.where.not(id: ignored_requests | staged_requests)
Expand Down Expand Up @@ -60,4 +61,21 @@ def create_staging_projects
staging_project.store
end
end

def update_staging_projects_managers_group
return unless changes[:managers_group_id]

old_managers_group = Group.find(changes[:managers_group_id].first)
new_managers_group = managers_group

staging_projects.each do |staging_project|
staging_project.unassign_managers_group(old_managers_group)
staging_project.assign_managers_group(new_managers_group)
staging_project.store
end

# FIXME: This assignation is need because after store a staging_project
# the object is reloaded and we lost the changes.
self.managers_group = new_managers_group
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
describe 'POST #create' do
context 'invalid user' do
before do
staging_workflow

login other_user
post :create, params: { staging_project_name: staging_project.name, format: :xml },
body: "<requests><number>#{bs_request.number}</number></requests>"
Expand Down

0 comments on commit bcdc6a9

Please sign in to comment.