From 3932884dba98544e311d224498ca63b67eb10250 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 25 Aug 2014 14:59:10 +1000 Subject: [PATCH] Admin can create enterprise relationships with permissions --- .../enterprise_relationships_controller.js.coffee | 3 ++- .../admin/services/enterprise_relationships.js.coffee | 9 +++++++-- app/views/admin/enterprise_relationships/_form.html.haml | 6 +++++- spec/features/admin/enterprise_relationships_spec.rb | 9 +++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) 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 665753a522..88524fb330 100644 --- a/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee +++ b/app/assets/javascripts/admin/controllers/enterprise_relationships_controller.js.coffee @@ -1,9 +1,10 @@ angular.module("ofn.admin").controller "AdminEnterpriseRelationshipsCtrl", ($scope, EnterpriseRelationships, Enterprises) -> $scope.EnterpriseRelationships = EnterpriseRelationships $scope.Enterprises = Enterprises + $scope.permissions = {} $scope.create = -> - $scope.EnterpriseRelationships.create($scope.parent_id, $scope.child_id) + $scope.EnterpriseRelationships.create($scope.parent_id, $scope.child_id, $scope.permissions) $scope.delete = (enterprise_relationship) -> if confirm("Are you sure?") diff --git a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee index 3185f229dd..7a799f2f28 100644 --- a/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee +++ b/app/assets/javascripts/admin/services/enterprise_relationships.js.coffee @@ -1,12 +1,17 @@ angular.module("ofn.admin").factory 'EnterpriseRelationships', ($http, enterprise_relationships) -> new class EnterpriseRelationships create_errors: "" + all_permissions: [ + 'add_products_to_order_cycle' + 'manage_products' + ] constructor: -> @enterprise_relationships = enterprise_relationships - create: (parent_id, child_id) -> - $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: parent_id, child_id: child_id}}).success (data, status) => + create: (parent_id, child_id, permissions) -> + permissions = (name for name, enabled of permissions when enabled) + $http.post('/admin/enterprise_relationships', {enterprise_relationship: {parent_id: parent_id, child_id: child_id, permissions_list: permissions}}).success (data, status) => @enterprise_relationships.unshift(data) @create_errors = "" diff --git a/app/views/admin/enterprise_relationships/_form.html.haml b/app/views/admin/enterprise_relationships/_form.html.haml index 1a737ec3a5..abb96f3c8f 100644 --- a/app/views/admin/enterprise_relationships/_form.html.haml +++ b/app/views/admin/enterprise_relationships/_form.html.haml @@ -1,7 +1,11 @@ %tr %td %select{name: "enterprise_relationship_parent_id", "ng-model" => "parent_id", "ng-options" => "e.id as e.name for e in Enterprises.my_enterprises"} - %td permits + %td + %div{"ng-repeat" => "permission in EnterpriseRelationships.all_permissions"} + %label + %input{type: "checkbox", "ng-model" => "permissions[permission]"} + {{ EnterpriseRelationships.permission_presentation(permission) }} %td %select{name: "enterprise_relationship_child_id", "ng-model" => "child_id", "ng-options" => "e.id as e.name for e in Enterprises.all_enterprises"} %td.actions diff --git a/spec/features/admin/enterprise_relationships_spec.rb b/spec/features/admin/enterprise_relationships_spec.rb index 112529376a..7eadf37ffa 100644 --- a/spec/features/admin/enterprise_relationships_spec.rb +++ b/spec/features/admin/enterprise_relationships_spec.rb @@ -38,11 +38,16 @@ feature %q{ visit admin_enterprise_relationships_path select 'One', from: 'enterprise_relationship_parent_id' + check 'can add products to order cycle from' + check 'can manage the products of' + uncheck 'can manage the products of' select 'Two', from: 'enterprise_relationship_child_id' click_button 'Create' - page.should have_relationship e1, e2 - EnterpriseRelationship.where(parent_id: e1, child_id: e2).should be_present + page.should have_relationship e1, e2, ['can add products to order cycle from'] + er = EnterpriseRelationship.where(parent_id: e1, child_id: e2).first + er.should be_present + er.permissions.map(&:name).should == ['add_products_to_order_cycle'] end