From c65853e072e44d40bfb5e8f5d19cf1c468e3f552 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Wed, 9 Apr 2014 22:44:19 +0400 Subject: [PATCH] #373: added ability to destroy ProductBuildList --- .../product_build_list_controller.js.erb | 9 +++++++++ .../product_build_lists_controller.rb | 11 +++-------- app/models/product_build_list.rb | 6 +++++- .../product_build_lists/show.html.haml | 19 ++++++++++--------- .../product_build_lists/show.json.jbuilder | 3 ++- config/routes.rb | 2 +- .../product_build_lists_controller_spec.rb | 8 +++++++- 7 files changed, 37 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/angularjs/controllers/product_build_list_controller.js.erb b/app/assets/javascripts/angularjs/controllers/product_build_list_controller.js.erb index 33875bc07..972f5718c 100644 --- a/app/assets/javascripts/angularjs/controllers/product_build_list_controller.js.erb +++ b/app/assets/javascripts/angularjs/controllers/product_build_list_controller.js.erb @@ -28,4 +28,13 @@ RosaABF.controller('ProductBuildListController', ['$scope', '$http', '$timeout', } $scope.refresh(); + $scope.updateStatus = function() { + $http.put( + Routes.product_build_list_path($scope.id), + {product_build_list: {not_delete: $scope.not_delete}, format: 'json'} + ).success(function(results) { + $scope.getProductBuildList(); + }); + } + }]); \ No newline at end of file diff --git a/app/controllers/platforms/product_build_lists_controller.rb b/app/controllers/platforms/product_build_lists_controller.rb index 91ad44d67..3a93c5f40 100644 --- a/app/controllers/platforms/product_build_lists_controller.rb +++ b/app/controllers/platforms/product_build_lists_controller.rb @@ -3,7 +3,7 @@ class Platforms::ProductBuildListsController < Platforms::BaseController before_filter :authenticate_user! skip_before_filter :authenticate_user!, only: [:index, :show, :log] if APP_CONFIG['anonymous_access'] - before_filter :redirect_to_full_path_if_short_url, only: :show + before_filter :redirect_to_full_path_if_short_url, only: [:show, :update] load_and_authorize_resource :platform, except: :index load_and_authorize_resource :product, through: :platform, except: :index load_and_authorize_resource :product_build_list, through: :product, except: :index @@ -25,13 +25,8 @@ class Platforms::ProductBuildListsController < Platforms::BaseController end def update - if @product_build_list.update_attributes(not_delete: (params[:product_build_list] || {})[:not_delete]) - flash[:notice] = t('flash.product_build_list.updated') - else - flash[:error] = t('flash.product_build_list.update_error') - flash[:warning] = @product_build_list.errors.full_messages.join('. ') - end - redirect_to platform_product_product_build_list_path(@platform, @product, @product_build_list) + @product_build_list.update_attributes(not_delete: (params[:product_build_list] || {})[:not_delete]) + render nothing: true end def cancel diff --git a/app/models/product_build_list.rb b/app/models/product_build_list.rb index e9bec061e..8f3e78951 100644 --- a/app/models/product_build_list.rb +++ b/app/models/product_build_list.rb @@ -122,8 +122,12 @@ class ProductBuildList < ActiveRecord::Base status == BUILD_CANCELING end + def can_destroy? + ![BUILD_STARTED, BUILD_PENDING, BUILD_CANCELING].include?(status) + end + def can_cancel? - [BUILD_STARTED, BUILD_PENDING].include? status + [BUILD_STARTED, BUILD_PENDING].include?(status) end def event_log_message diff --git a/app/views/platforms/product_build_lists/show.html.haml b/app/views/platforms/product_build_lists/show.html.haml index ba72e27d1..0c2a2e739 100644 --- a/app/views/platforms/product_build_lists/show.html.haml +++ b/app/views/platforms/product_build_lists/show.html.haml @@ -38,17 +38,18 @@ %br - if can?(:update, pbl) - = form_for pbl, - url: platform_product_product_build_list_path(pbl.product.platform,pbl.product, pbl), - html: {'ng-show' => "pbl.status == #{ProductBuildList::BUILD_COMPLETED}"} do |f| - - .leftlist= f.label :not_delete + %div{'ng-show' => "pbl.status == #{ProductBuildList::BUILD_COMPLETED}"} + .leftlist= t('activerecord.attributes.product_build_list.not_delete') .rightlist - = f.select :not_delete, [false, true].collect{|status| [t("layout.#{status}_"), status]}, {selected: pbl.not_delete} - .both - %br - = submit_tag t('layout.update'), data: {'disable-with' => t('layout.processing')} + = select_tag 'not_delete', options_for_select([false, true].map{|status| [t("layout.#{status}_"), status]}), {'ng-model' => "not_delete", 'ng-change' => 'updateStatus()', 'ng-init' => "not_delete = '#{pbl.not_delete}'"} .both + - if can?(:destroy, pbl) + = link_to t('layout.product_build_lists.delete'), + platform_product_product_build_list_path(pbl.product.platform, pbl.product, pbl), + method: :delete, data: { confirm: t("layout.confirm") }, class: 'button', + 'ng-show' => 'pbl.can_destroy' + .both + %br .flash_notify{'ng-hide' => 'pbl.not_delete'} .alert.alert-error - days = pbl.autostarted? ? ProductBuildList::LIVE_TIME : ProductBuildList::MAX_LIVE_TIME diff --git a/app/views/platforms/product_build_lists/show.json.jbuilder b/app/views/platforms/product_build_lists/show.json.jbuilder index 502a92275..767b642f1 100644 --- a/app/views/platforms/product_build_lists/show.json.jbuilder +++ b/app/views/platforms/product_build_lists/show.json.jbuilder @@ -2,7 +2,8 @@ json.product_build_list do json.(@product_build_list, :id, :status, :human_status, :not_delete) json.notified_at l(@product_build_list.updated_at, format: :long) - json.can_cancel @product_build_list.can_cancel? + json.can_cancel @product_build_list.can_cancel? + json.can_destroy @product_build_list.can_destroy? json.results @product_build_list.results do |result| json.file_name result['file_name'] diff --git a/config/routes.rb b/config/routes.rb index 52b31fc5b..c7ad7cf84 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -231,7 +231,7 @@ Rosa::Application.routes.draw do resources :maintainers, only: [:index] end - resources :product_build_lists, only: [:index, :show] + resources :product_build_lists, only: [:index, :show, :update] end resources :autocompletes, only: [] do diff --git a/spec/controllers/platforms/product_build_lists_controller_spec.rb b/spec/controllers/platforms/product_build_lists_controller_spec.rb index ef83e4eb0..85f317886 100644 --- a/spec/controllers/platforms/product_build_lists_controller_spec.rb +++ b/spec/controllers/platforms/product_build_lists_controller_spec.rb @@ -36,7 +36,7 @@ shared_examples_for 'product build list admin' do it 'should be able to perform update action' do put :update, valid_attributes_for_show.merge(product_build_list: {time_living: 100,not_delete: true}) - response.should redirect_to(platform_product_product_build_list_path(@product.platform, @product, @pbl)) + response.should be_success end it "ensures that only not_delete field of product build list has been updated" do @@ -73,6 +73,12 @@ shared_examples_for 'product build list user without admin rights' do put :cancel, valid_attributes_for_show response.should_not redirect_to(platform_product_product_build_list_path(@product.platform, @product, @pbl)) end + + it 'should not be able to perform update action' do + put :update, valid_attributes_for_show + response.should_not be_success + end + end shared_examples_for 'product build list user' do