From 4d83bf2135653a2b6c5069285096dbcdbd665f60 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 27 Apr 2016 13:36:49 +1000 Subject: [PATCH] Payment methods can be tagged --- .../payment_methods/payment_methods.js.coffee | 2 +- app/helpers/admin/injection_helper.rb | 4 ++ .../gateway/pay_pal_express_decorator.rb | 7 ++ app/models/spree/gateway_decorator.rb | 3 + app/models/spree/payment_method_decorator.rb | 4 +- .../replace_form_fields.html.haml.deface | 10 ++- .../api/admin/payment_method_serializer.rb | 11 +++ .../admin/payment_methods/_data.html.haml | 2 - spec/features/admin/payment_method_spec.rb | 71 +++++++++++-------- 9 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 app/models/spree/gateway/pay_pal_express_decorator.rb create mode 100644 app/serializers/api/admin/payment_method_serializer.rb delete mode 100644 app/views/spree/admin/payment_methods/_data.html.haml diff --git a/app/assets/javascripts/admin/payment_methods/payment_methods.js.coffee b/app/assets/javascripts/admin/payment_methods/payment_methods.js.coffee index 01553647d4..c74a1c7054 100644 --- a/app/assets/javascripts/admin/payment_methods/payment_methods.js.coffee +++ b/app/assets/javascripts/admin/payment_methods/payment_methods.js.coffee @@ -1 +1 @@ -angular.module("admin.paymentMethods", []) +angular.module("admin.paymentMethods", ['ngTagsInput', 'admin.utils']) diff --git a/app/helpers/admin/injection_helper.rb b/app/helpers/admin/injection_helper.rb index 079f93d41f..67105b6b34 100644 --- a/app/helpers/admin/injection_helper.rb +++ b/app/helpers/admin/injection_helper.rb @@ -23,6 +23,10 @@ module Admin admin_inject_json_ams_array "admin.paymentMethods", "paymentMethods", @payment_methods, Api::Admin::IdNameSerializer end + def admin_inject_payment_method + admin_inject_json_ams "admin.paymentMethods", "paymentMethod", @payment_method, Api::Admin::PaymentMethodSerializer + end + def admin_inject_shipping_methods admin_inject_json_ams_array "admin.shippingMethods", "shippingMethods", @shipping_methods, Api::Admin::IdNameSerializer end diff --git a/app/models/spree/gateway/pay_pal_express_decorator.rb b/app/models/spree/gateway/pay_pal_express_decorator.rb new file mode 100644 index 0000000000..dc8239a107 --- /dev/null +++ b/app/models/spree/gateway/pay_pal_express_decorator.rb @@ -0,0 +1,7 @@ +module Spree + class Gateway::PayPalExpress < Gateway + # Something odd is happening with class inheritance here, this class (defined in spree_paypal_express gem) + # doesn't seem to pick up attr_accessible from the Gateway class, so we redefine the attrs we need here + attr_accessible :tag_list + end +end diff --git a/app/models/spree/gateway_decorator.rb b/app/models/spree/gateway_decorator.rb index 3c6bb2638d..9125ef1d2d 100644 --- a/app/models/spree/gateway_decorator.rb +++ b/app/models/spree/gateway_decorator.rb @@ -1,4 +1,5 @@ Spree::Gateway.class_eval do + acts_as_taggable # Due to class load order, when config.cache_classes is enabled (ie. staging and production # environments), this association isn't inherited from PaymentMethod. As a result, creating @@ -12,4 +13,6 @@ Spree::Gateway.class_eval do # Default to live preference :server, :string, :default => 'live' preference :test_mode, :boolean, :default => false + + attr_accessible :tag_list end diff --git a/app/models/spree/payment_method_decorator.rb b/app/models/spree/payment_method_decorator.rb index 97ae3c6bc7..77e22c591f 100644 --- a/app/models/spree/payment_method_decorator.rb +++ b/app/models/spree/payment_method_decorator.rb @@ -1,8 +1,10 @@ Spree::PaymentMethod.class_eval do + acts_as_taggable + # See gateway_decorator.rb when modifying this association has_and_belongs_to_many :distributors, join_table: 'distributors_payment_methods', :class_name => 'Enterprise', association_foreign_key: 'distributor_id' - attr_accessible :distributor_ids + attr_accessible :distributor_ids, :tag_list calculated_adjustments diff --git a/app/overrides/spree/admin/payment_methods/_form/replace_form_fields.html.haml.deface b/app/overrides/spree/admin/payment_methods/_form/replace_form_fields.html.haml.deface index 21fbf95723..aa9ead7a0f 100644 --- a/app/overrides/spree/admin/payment_methods/_form/replace_form_fields.html.haml.deface +++ b/app/overrides/spree/admin/payment_methods/_form/replace_form_fields.html.haml.deface @@ -1,6 +1,6 @@ / replace "div[data-hook='admin_payment_method_form_fields']" -= render 'data' += admin_inject_payment_method %div.alpha.eleven.columns{ "ng-app" => "admin.paymentMethods", "ng-controller" => "paymentMethodCtrl" } .row .alpha.three.columns @@ -34,6 +34,14 @@ = radio_button :payment_method, :active, false   = label_tag nil, t(:say_no) + + .row + .alpha.three.columns + = label(:payment_method, :tags, t(:tags)) + .omega.eight.columns + = hidden_field(:payment_method, :tag_list, "ng-value" => "paymentMethod.tag_list") + %tags-with-translation#something{ object: "paymentMethod" } + = render 'providers' .row diff --git a/app/serializers/api/admin/payment_method_serializer.rb b/app/serializers/api/admin/payment_method_serializer.rb new file mode 100644 index 0000000000..42c11a6953 --- /dev/null +++ b/app/serializers/api/admin/payment_method_serializer.rb @@ -0,0 +1,11 @@ +class Api::Admin::PaymentMethodSerializer < ActiveModel::Serializer + attributes :id, :name, :type, :tag_list, :tags + + def tag_list + object.tag_list.join(",") + end + + def tags + object.tag_list.map{ |t| { text: t } } + end +end diff --git a/app/views/spree/admin/payment_methods/_data.html.haml b/app/views/spree/admin/payment_methods/_data.html.haml deleted file mode 100644 index 62a36498a4..0000000000 --- a/app/views/spree/admin/payment_methods/_data.html.haml +++ /dev/null @@ -1,2 +0,0 @@ -:javascript - angular.module('admin.paymentMethods').value('paymentMethod', #{ { id: @payment_method.id, type: @payment_method.type }.to_json }) diff --git a/spec/features/admin/payment_method_spec.rb b/spec/features/admin/payment_method_spec.rb index b736155372..87f22d969f 100644 --- a/spec/features/admin/payment_method_spec.rb +++ b/spec/features/admin/payment_method_spec.rb @@ -29,47 +29,53 @@ feature %q{ payment_method = Spree::PaymentMethod.find_by_name('Cheque payment method') payment_method.distributors.should == [@distributors[0]] end + end - scenario "updating a payment method" do - pm = create(:payment_method, distributors: [@distributors[0]]) - login_to_admin_section + scenario "updating a payment method", js: true do + pm = create(:payment_method, distributors: [@distributors[0]]) + login_to_admin_section - visit spree.edit_admin_payment_method_path pm + visit spree.edit_admin_payment_method_path pm - fill_in 'payment_method_name', :with => 'New PM Name' + fill_in 'payment_method_name', :with => 'New PM Name' + find(:css, "tags-input .tags input").set "member\n" - uncheck "payment_method_distributor_ids_#{@distributors[0].id}" - check "payment_method_distributor_ids_#{@distributors[1].id}" - check "payment_method_distributor_ids_#{@distributors[2].id}" - select2_select "PayPal Express", from: "payment_method_type" - expect(page).to have_field 'Login' - fill_in 'payment_method_preferred_login', with: 'testlogin' - fill_in 'payment_method_preferred_password', with: 'secret' - fill_in 'payment_method_preferred_signature', with: 'sig' + uncheck "payment_method_distributor_ids_#{@distributors[0].id}" + check "payment_method_distributor_ids_#{@distributors[1].id}" + check "payment_method_distributor_ids_#{@distributors[2].id}" + select2_select "PayPal Express", from: "payment_method_type" + expect(page).to have_field 'Login' + fill_in 'payment_method_preferred_login', with: 'testlogin' + fill_in 'payment_method_preferred_password', with: 'secret' + fill_in 'payment_method_preferred_signature', with: 'sig' - click_button 'Update' + click_button 'Update' - expect(flash_message).to eq 'Payment Method has been successfully updated!' + expect(flash_message).to eq 'Payment Method has been successfully updated!' + save_screenshot '/Users/rob/Desktop/ss.png' - payment_method = Spree::PaymentMethod.find_by_name('New PM Name') - expect(payment_method.distributors).to include @distributors[1], @distributors[2] - expect(payment_method.distributors).not_to include @distributors[0] - expect(payment_method.type).to eq "Spree::Gateway::PayPalExpress" - expect(payment_method.preferences[:login]).to eq 'testlogin' - expect(payment_method.preferences[:password]).to eq 'secret' - expect(payment_method.preferences[:signature]).to eq 'sig' + expect(first('tags-input .tag-list ti-tag-item')).to have_content "member" - fill_in 'payment_method_preferred_login', with: 'otherlogin' - click_button 'Update' + payment_method = Spree::PaymentMethod.find_by_name('New PM Name') + expect(payment_method.distributors).to include @distributors[1], @distributors[2] + expect(payment_method.distributors).not_to include @distributors[0] + expect(payment_method.type).to eq "Spree::Gateway::PayPalExpress" + expect(payment_method.preferences[:login]).to eq 'testlogin' + expect(payment_method.preferences[:password]).to eq 'secret' + expect(payment_method.preferences[:signature]).to eq 'sig' - expect(flash_message).to eq 'Payment Method has been successfully updated!' - expect(page).to have_field 'Password', with: '' + fill_in 'payment_method_preferred_login', with: 'otherlogin' + click_button 'Update' - payment_method = Spree::PaymentMethod.find_by_name('New PM Name') - expect(payment_method.preferences[:login]).to eq 'otherlogin' - expect(payment_method.preferences[:password]).to eq 'secret' - expect(payment_method.preferences[:signature]).to eq 'sig' - end + expect(flash_message).to eq 'Payment Method has been successfully updated!' + expect(page).to have_field 'Password', with: '' + expect(first('tags-input .tag-list ti-tag-item')).to have_content "member" + + payment_method = Spree::PaymentMethod.find_by_name('New PM Name') + expect(payment_method.tag_list).to eq ["member"] + expect(payment_method.preferences[:login]).to eq 'otherlogin' + expect(payment_method.preferences[:password]).to eq 'secret' + expect(payment_method.preferences[:signature]).to eq 'sig' end context "as an enterprise user", js: true do @@ -102,12 +108,15 @@ feature %q{ fill_in 'payment_method_name', :with => 'Cheque payment method' check "payment_method_distributor_ids_#{distributor1.id}" + find(:css, "tags-input .tags input").set "local\n" click_button 'Create' flash_message.should == 'Payment Method has been successfully created!' + expect(first('tags-input .tag-list ti-tag-item')).to have_content "local" payment_method = Spree::PaymentMethod.find_by_name('Cheque payment method') payment_method.distributors.should == [distributor1] + payment_method.tag_list.should == ["local"] end it "shows me only payment methods I have access to" do