From 6106f1b46987bcd72a6f6709ee85e9f60ee8a0c6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 26 Nov 2012 09:45:06 +1100 Subject: [PATCH] Calculator type change works, enterprise fee tests pass --- .../javascripts/admin/enterprise_fees.js | 57 ++++++++++--------- .../admin/enterprise_fees/index.html.haml | 2 +- spec/requests/admin/enterprise_fees_spec.rb | 6 +- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/app/assets/javascripts/admin/enterprise_fees.js b/app/assets/javascripts/admin/enterprise_fees.js index a497c8cf96..e73ff0dfcf 100644 --- a/app/assets/javascripts/admin/enterprise_fees.js +++ b/app/assets/javascripts/admin/enterprise_fees.js @@ -1,9 +1,16 @@ function AdminEnterpriseFeesCtrl($scope, $http) { $http.get('/admin/enterprise_fees.json').success(function(data) { $scope.enterprise_fees = data; + + // 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) { + $scope.enterprise_fees[i].orig_calculator_type = $scope.enterprise_fees[i].calculator_type; + } }); } + angular.module('enterprise_fees', []) .directive('ngBindHtmlUnsafeCompiled', function($compile) { return function(scope, element, attrs) { @@ -20,36 +27,30 @@ angular.module('enterprise_fees', []) element.append(html); } } - }); + }) + .directive('spreeEnsureCalculatorPreferencesMatchType', function() { + // Hide calculator preference fields when calculator type changed + // Fixes 'Enterprise fee is not found' error when changing calculator type + // See spree/core/app/assets/javascripts/admin/calculator.js + // Note: For some reason, DOM --> model bindings aren't working here, so + // we use element.val() instead of querying the model itself. + return function(scope, element, attrs) { + scope.$watch(function(scope) { + //return scope.enterprise_fee.calculator_type; + return element.val(); + }, function(value) { + var settings = element.parent().parent().find("div.calculator-settings"); - -/* -// Hide calculator preference fields when calculator type changed -// Fixes 'Enterprise fee is not found' error when changing calculator type -// See spree/core/app/assets/javascripts/admin/calculator.js - -$(document).ready(function() { - // Store original value - $("select.calculator_type").each(function(i, ct) { - ct = $(ct); - ct.data('original-value', ct.attr('value')); - }); - - // Hide and disable calculator fields when calculator type is changed - $("select.calculator_type").change(function() { - var ct = $(this); - var cs = ct.parent().parent().find("div.calculator-settings"); - - if(ct.attr('value') == ct.data('original-value')) { - cs.show(); - cs.find("input").prop("disabled", false); - - } else { - cs.hide(); - cs.find("input").prop("disabled", true); + // scope.enterprise_fee.calculator_type == scope.enterprise_fee.orig_calculator_type + if(element.val() == scope.enterprise_fee.orig_calculator_type) { + settings.show(); + settings.find("input").prop("disabled", false); + } else { + settings.hide(); + settings.find("input").prop("disabled", true); + } + }); } }); -}); -*/ \ No newline at end of file diff --git a/app/views/admin/enterprise_fees/index.html.haml b/app/views/admin/enterprise_fees/index.html.haml index 13a1f6f25c..e2c5d4e854 100644 --- a/app/views/admin/enterprise_fees/index.html.haml +++ b/app/views/admin/enterprise_fees/index.html.haml @@ -24,7 +24,7 @@ = f.angular_collection_select :enterprise_id, Enterprise.all, :id, :name, 'enterprise_fee.enterprise_id', :include_blank => true %td= f.angular_select :fee_type, enterprise_fee_type_options, 'enterprise_fee.fee_type' %td= f.angular_text_field :name - %td= f.angular_collection_select :calculator_type, @calculators, :name, :description, 'enterprise_fee.calculator_type', {:class => 'calculator_type'} + %td= f.angular_collection_select :calculator_type, @calculators, :name, :description, 'enterprise_fee.calculator_type', {'class' => 'calculator_type', 'ng-model' => 'calculatorType', 'spree-ensure-calculator-preferences-match-type' => "1"} %td{'ng-bind-html-unsafe-compiled' => 'enterprise_fee.calculator_settings'} %td{'spree-delete-resource' => "1"} diff --git a/spec/requests/admin/enterprise_fees_spec.rb b/spec/requests/admin/enterprise_fees_spec.rb index 90f7f065d3..8af01b149f 100644 --- a/spec/requests/admin/enterprise_fees_spec.rb +++ b/spec/requests/admin/enterprise_fees_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' feature %q{ As an administrator I want to manage enterprise fees -} do +}, js: true do include AuthenticationWorkflow include WebHelper @@ -49,7 +49,7 @@ feature %q{ page.should have_selector "#enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent[value='12.34']" end - scenario "editing an enterprise fee", js: true do + scenario "editing an enterprise fee" do # Given an enterprise fee fee = create(:enterprise_fee) create(:enterprise, name: 'Foo') @@ -73,7 +73,7 @@ feature %q{ page.should have_selector "option[selected]", text: 'Flat Percent' end - scenario "deleting an enterprise fee", js: true do + scenario "deleting an enterprise fee" do # Given an enterprise fee fee = create(:enterprise_fee)