diff --git a/app/assets/javascripts/admin/controllers/providers_controller.js.coffee b/app/assets/javascripts/admin/controllers/providers_controller.js.coffee new file mode 100644 index 0000000000..b78ca47f4c --- /dev/null +++ b/app/assets/javascripts/admin/controllers/providers_controller.js.coffee @@ -0,0 +1,2 @@ +angular.module("ofn.admin").controller "ProvidersCtrl", ($scope, paymentMethod) -> + $scope.include_html = "/admin/payment_methods/#{paymentMethod.id}/show_provider_preferences?provider_type=#{paymentMethod.type}" \ No newline at end of file diff --git a/app/assets/javascripts/admin/directives/fetch_provider_prefs.js.coffee b/app/assets/javascripts/admin/directives/fetch_provider_prefs.js.coffee new file mode 100644 index 0000000000..dad8e4b8a1 --- /dev/null +++ b/app/assets/javascripts/admin/directives/fetch_provider_prefs.js.coffee @@ -0,0 +1,5 @@ +angular.module("ofn.admin").directive "ofnFetchProviderPrefs", ($http) -> + link: (scope, element, attrs) -> + element.on "change blur", -> + scope.$apply -> + scope.include_html = "/admin/payment_methods/#{attrs.ofnFetchProviderPrefs}/show_provider_preferences?provider_type=#{element.val()}" \ No newline at end of file diff --git a/app/controllers/spree/admin/payment_methods_controller_decorator.rb b/app/controllers/spree/admin/payment_methods_controller_decorator.rb index 90f85d2bc2..fdb2c0953a 100644 --- a/app/controllers/spree/admin/payment_methods_controller_decorator.rb +++ b/app/controllers/spree/admin/payment_methods_controller_decorator.rb @@ -1,32 +1,46 @@ -Spree::Admin::PaymentMethodsController.class_eval do - before_filter :load_hubs, only: [:new, :edit, :create, :update] +module Spree + module Admin + PaymentMethodsController.class_eval do + before_filter :load_hubs, only: [:new, :edit, :create, :update] - # Only show payment methods that user has access to and sort by distributor name - # ! Redundant code copied from Spree::Admin::ResourceController with modifications marked - def collection - return parent.send(controller_name) if parent_data.present? - collection = if model_class.respond_to?(:accessible_by) && - !current_ability.has_block?(params[:action], model_class) + # Only show payment methods that user has access to and sort by distributor name + # ! Redundant code copied from Spree::Admin::ResourceController with modifications marked + def collection + return parent.send(controller_name) if parent_data.present? + collection = if model_class.respond_to?(:accessible_by) && + !current_ability.has_block?(params[:action], model_class) - model_class.accessible_by(current_ability, action) + model_class.accessible_by(current_ability, action) - else - model_class.scoped - end + else + model_class.scoped + end - collection = collection.managed_by(spree_current_user).by_name # This line added + collection = collection.managed_by(spree_current_user).by_name # This line added - # This block added - if params.key? :enterprise_id - distributor = Enterprise.find params[:enterprise_id] - collection = collection.for_distributor(distributor) + # This block added + if params.key? :enterprise_id + distributor = Enterprise.find params[:enterprise_id] + collection = collection.for_distributor(distributor) + end + + collection + end + + def show_provider_preferences + @payment_method = PaymentMethod.find(params[:id]) + payment_method_type = params[:provider_type] + if @payment_method['type'].to_s != payment_method_type + @payment_method.update_column(:type, payment_method_type) + @payment_method = PaymentMethod.find(params[:id]) + end + render partial: 'provider_settings' + end + + private + def load_hubs + @hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by!{ |d| [(@payment_method.has_distributor? d) ? 0 : 1, d.name] } + end end - - collection - end - - private - def load_hubs - @hubs = Enterprise.managed_by(spree_current_user).is_distributor.sort_by!{ |d| [(@payment_method.has_distributor? d) ? 0 : 1, d.name] } end end diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index e6d93f25aa..a04d966ee7 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -44,7 +44,7 @@ class AbilityDecorator # Enterprise User can only access payment methods for their distributors can [:index, :create], Spree::PaymentMethod - can [:admin, :read, :update, :fire, :resend, :destroy], Spree::PaymentMethod do |payment_method| + can [:admin, :read, :update, :fire, :resend, :destroy, :show_provider_preferences], Spree::PaymentMethod do |payment_method| (user.enterprises & payment_method.distributors).any? end 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 0ae2b81a9e..3b588a98c1 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 @@ -33,17 +33,4 @@ = radio_button :payment_method, :active, false   = label_tag nil, t(:say_no) - .row - .alpha.three.columns - = f.label :type, t(:provider) - .omega.eight.columns - = collection_select(:payment_method, :type, @providers, :to_s, :clean_name, {}, {:id => 'gtwy-type', :class => 'select2 fullwidth'}) - - %fieldset.alpha.eleven.columns.no-border-bottom#gateway_fields - %legend{ align="center"} - = t(:provider_settings) - #preference-settings - - unless @object.new_record? - = preference_fields(@object, f) - - if @object.respond_to?(:preferences) - #gateway-settings-warning.info.warning= t(:provider_settings_warning) \ No newline at end of file + = render 'providers' \ No newline at end of file diff --git a/app/views/spree/admin/payment_methods/_provider_settings.html.haml b/app/views/spree/admin/payment_methods/_provider_settings.html.haml new file mode 100644 index 0000000000..e227180fc3 --- /dev/null +++ b/app/views/spree/admin/payment_methods/_provider_settings.html.haml @@ -0,0 +1,7 @@ +- if @payment_method.preferences.present? + %fieldset.alpha.eleven.columns.no-border-bottom#gateway_fields + %legend{ align: "center"} + = t(:provider_settings) + .preference-settings + = fields_for :payment_method, @payment_method do |payment_method_form| + = preference_fields(@payment_method, payment_method_form) \ No newline at end of file diff --git a/app/views/spree/admin/payment_methods/_providers.html.haml b/app/views/spree/admin/payment_methods/_providers.html.haml new file mode 100644 index 0000000000..825e277a95 --- /dev/null +++ b/app/views/spree/admin/payment_methods/_providers.html.haml @@ -0,0 +1,10 @@ +:javascript + angular.module('ofn.admin').value('paymentMethod', #{ { id: @payment_method.id, type: @payment_method.type }.to_json }) +#provider-settings{ ng: { app: "ofn.admin", controller: "ProvidersCtrl" } } + .row + .alpha.three.columns + = label :payment_method, :type, t(:provider) + .omega.eight.columns + = collection_select(:payment_method, :type, @providers, :to_s, :clean_name, {}, { class: 'select2 fullwidth', 'ofn-fetch-provider-prefs' => "#{@object.id}"}) + + %div{"ng-include" => "include_html" } \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index af70141639..53f7b2a92e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -135,6 +135,10 @@ Spree::Core::Engine.routes.prepend do post :bulk_update, :on => :collection, :as => :bulk_update end + + resources :payment_methods do + get :show_provider_preferences, on: :member + end end resources :orders do