diff --git a/app/assets/javascripts/admin/enterprise_fees.js b/app/assets/javascripts/admin/enterprise_fees.js index 15a80b3e02..b815cd4266 100644 --- a/app/assets/javascripts/admin/enterprise_fees.js +++ b/app/assets/javascripts/admin/enterprise_fees.js @@ -14,10 +14,6 @@ angular.module('enterprise_fees', []) $http.get($scope.enterpriseFeesUrl()).success(function(data) { $scope.enterprise_fees = data; - for(i=0; i<3; i++) { - $scope.enterprise_fees.push({}); - } - // TODO: Angular 1.1.0 will have a means to reset a form to its pristine state, which // would avoid the need to save off original calculator types for comparison. for(i in $scope.enterprise_fees) { diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index 75035a2d64..829f950b1f 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -8,6 +8,10 @@ module Admin def index @include_calculators = params[:include_calculators].present? + blank_enterprise_fee = EnterpriseFee.new + blank_enterprise_fee.enterprise = current_enterprise + 3.times { @collection << blank_enterprise_fee } + respond_to do |format| format.html format.json { @presented_collection = @collection.each_with_index.map { |ef, i| EnterpriseFeePresenter.new(self, ef, i) } } @@ -52,13 +56,13 @@ module Admin def collection collection = EnterpriseFee.managed_by(spree_current_user).order('enterprise_id', 'fee_type', 'name') - - if params.key? :enterprise_id - enterprise = Enterprise.find params[:enterprise_id] - collection = collection.for_enterprise(enterprise) - end - + collection = collection.for_enterprise(current_enterprise) if current_enterprise collection end + + def current_enterprise + Enterprise.find params[:enterprise_id] if params.key? :enterprise_id + end + end end diff --git a/app/models/enterprise_fee_set.rb b/app/models/enterprise_fee_set.rb index 4ae02f51d8..fdefeec277 100644 --- a/app/models/enterprise_fee_set.rb +++ b/app/models/enterprise_fee_set.rb @@ -1,7 +1,7 @@ class EnterpriseFeeSet < ModelSet def initialize(attributes={}) super(EnterpriseFee, EnterpriseFee.all, - proc { |attrs| attrs[:enterprise_id].blank? }, + proc { |attrs| attrs[:name].blank? }, attributes) end end diff --git a/app/views/admin/enterprise_fees/index.html.haml b/app/views/admin/enterprise_fees/index.html.haml index 8bf3135b8a..789ee19935 100644 --- a/app/views/admin/enterprise_fees/index.html.haml +++ b/app/views/admin/enterprise_fees/index.html.haml @@ -23,7 +23,7 @@ %tr{'ng-repeat' => 'enterprise_fee in enterprise_fees | filter:query'} %td = f.ng_hidden_field :id - = f.ng_collection_select :enterprise_id, Enterprise.all, :id, :name, 'enterprise_fee.enterprise_id', :include_blank => true + = f.ng_collection_select :enterprise_id, Enterprise.managed_by(spree_current_user), :id, :name, 'enterprise_fee.enterprise_id', :include_blank => true %td= f.ng_select :fee_type, enterprise_fee_type_options, 'enterprise_fee.fee_type' %td= f.ng_text_field :name %td= f.ng_collection_select :calculator_type, @calculators, :name, :description, 'enterprise_fee.calculator_type', {'class' => 'calculator_type', 'ng-model' => 'calculatorType', 'spree-ensure-calculator-preferences-match-type' => "1"} diff --git a/spec/features/admin/enterprise_fees_spec.rb b/spec/features/admin/enterprise_fees_spec.rb index 0d700fab13..3c5526d6a1 100644 --- a/spec/features/admin/enterprise_fees_spec.rb +++ b/spec/features/admin/enterprise_fees_spec.rb @@ -128,6 +128,7 @@ feature %q{ let(:enterprise_user) { create_enterprise_user } let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') } let(:distributor2) { create(:distributor_enterprise, name: 'Second Distributor') } + let(:distributor3) { create(:distributor_enterprise, name: 'Third Distributor') } let(:ef1) { create(:enterprise_fee, name: 'One', enterprise: distributor1) } let(:ef2) { create(:enterprise_fee, name: 'Two', enterprise: distributor2) } @@ -166,5 +167,17 @@ feature %q{ page.should_not have_field 'enterprise_fee_set_collection_attributes_0_name', with: 'One' page.should have_field 'enterprise_fee_set_collection_attributes_0_name', with: 'Two' end + + it "only allows me to select enterprises I have access to" do + ef1 + ef2 + distributor3 + + click_link 'Enterprises' + within(".enterprise-#{distributor2.id}") { click_link 'Enterprise Fees' } + page.should have_select('enterprise_fee_set_collection_attributes_1_enterprise_id', + selected: 'Second Distributor', + options: ['', 'First Distributor', 'Second Distributor']) + end end end