diff --git a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee index 02001bc48f..51e2dd9ee0 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -4,8 +4,12 @@ Admin.controller "AdminEnterpriseRelationshipsCtrl", ($scope, $http, EnterpriseR $scope.create = -> $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: $scope.parent_id, child_id: $scope.child_id}}).success (data, status) => - $scope.EnterpriseRelationships.enterprise_relationships.unshift({parent_name: data.parent_name, child_name: data.child_name}) + $scope.EnterpriseRelationships.enterprise_relationships.unshift(data) $scope.errors = "" .error (response, status) => $scope.errors = response.errors + + $scope.delete = (enterprise_relationship) -> + if confirm("Are you sure?") + $scope.EnterpriseRelationships.delete enterprise_relationship diff --git a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee index 0201a9793e..8af5fef666 100644 --- a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee +++ b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee @@ -1,4 +1,9 @@ -Admin.factory 'EnterpriseRelationships', (enterprise_relationships) -> +Admin.factory 'EnterpriseRelationships', ($http, enterprise_relationships) -> new class EnterpriseRelationships constructor: -> @enterprise_relationships = enterprise_relationships + + delete: (er) -> + ers = @enterprise_relationships + $http.delete('/admin/enterprise_relationships/' + er.id).success (data) -> + ers.splice ers.indexOf(er), 1 diff --git a/app/controllers/admin/enterprise_relationships_controller.rb b/app/controllers/admin/enterprise_relationships_controller.rb index 9661470574..46ac45a3e5 100644 --- a/app/controllers/admin/enterprise_relationships_controller.rb +++ b/app/controllers/admin/enterprise_relationships_controller.rb @@ -14,5 +14,11 @@ module Admin render status: 400, json: {errors: @enterprise_relationship.errors.full_messages.join(', ')} end end + + def destroy + @enterprise_relationship = EnterpriseRelationship.find params[:id] + @enterprise_relationship.destroy + render nothing: true + end end end diff --git a/app/views/admin/enterprise_relationships/index.html.haml b/app/views/admin/enterprise_relationships/index.html.haml index 050c57cc96..1992f883d4 100644 --- a/app/views/admin/enterprise_relationships/index.html.haml +++ b/app/views/admin/enterprise_relationships/index.html.haml @@ -27,3 +27,5 @@ %tr{"ng-repeat" => "enterprise_relationship in EnterpriseRelationships.enterprise_relationships"} %td {{ enterprise_relationship.parent_name }} %td {{ enterprise_relationship.child_name }} + %td.actions + %a.delete-enterprise-relationship.icon-trash.no-text{'ng-click' => 'delete(enterprise_relationship)'} diff --git a/app/views/admin/json/_enterprise_relationship.rabl b/app/views/admin/json/_enterprise_relationship.rabl index 23d08d4c87..9be152ec5c 100644 --- a/app/views/admin/json/_enterprise_relationship.rabl +++ b/app/views/admin/json/_enterprise_relationship.rabl @@ -1,6 +1,6 @@ object @enterprise_relationship -attributes :parent_id, :child_id +attributes :id, :parent_id, :child_id node :parent_name do |enterprise_relationship| enterprise_relationship.parent.name diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb index ae54f1112c..0161677afe 100644 --- a/spec/features/admin/enterprise_relationships_spec.rb +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -22,8 +22,8 @@ feature %q{ # Then I should see the relationships within('table#enterprise-relationships') do - page.should have_table_row [e1.name, e2.name] - page.should have_table_row [e3.name, e4.name] + page.should have_table_row [e1.name, e2.name, ''] + page.should have_table_row [e3.name, e4.name, ''] end end @@ -37,7 +37,7 @@ feature %q{ select 'Two', from: 'enterprise_relationship_child_name' click_button 'Create' - page.should have_table_row [e1.name, e2.name] + page.should have_table_row [e1.name, e2.name, ''] EnterpriseRelationship.where(parent_id: e1, child_id: e2).should be_present end @@ -60,5 +60,17 @@ feature %q{ end - scenario "deleting a relationship" + scenario "deleting a relationship" do + e1 = create(:enterprise, name: 'One') + e2 = create(:enterprise, name: 'Two') + er = create(:enterprise_relationship, parent: e1, child: e2) + + visit admin_enterprise_relationships_path + page.should have_table_row [e1.name, e2.name, ''] + + first("a.delete-enterprise-relationship").click + + page.should_not have_table_row [e1.name, e2.name, ''] + EnterpriseRelationship.where(id: er.id).should be_empty + end end