diff --git a/app/helpers/spree/admin/navigation_helper.rb b/app/helpers/spree/admin/navigation_helper.rb index 8a78781982..337946edd0 100644 --- a/app/helpers/spree/admin/navigation_helper.rb +++ b/app/helpers/spree/admin/navigation_helper.rb @@ -361,6 +361,10 @@ def store_credits_actions def adjustments_actions Rails.application.config.spree_backend.actions[:adjustments] end + + def payments_actions + Rails.application.config.spree_backend.actions[:payments] + end # rubocop:enable Metrics/ModuleLength end end diff --git a/app/models/spree/admin/actions/payments_default_actions_builder.rb b/app/models/spree/admin/actions/payments_default_actions_builder.rb new file mode 100644 index 0000000000..77e27b0f48 --- /dev/null +++ b/app/models/spree/admin/actions/payments_default_actions_builder.rb @@ -0,0 +1,40 @@ +module Spree + module Admin + module Actions + class PaymentsDefaultActionsBuilder + include Spree::Core::Engine.routes.url_helpers + + def build + root = Root.new + add_new_payment_action(root) + root + end + + private + + def add_new_payment_action(root) + action = + ActionBuilder.new(new_payment_config). + with_availability_check( + lambda do |ability, resource| + ability.can?(:create, ::Spree::Payment) && resource.outstanding_balance? + end + ). + build + + root.add(action) + end + + def new_payment_config + { + icon_name: 'add.svg', + key: :new_payment, + url: ->(resource) { new_admin_order_payment_path(resource) }, + classes: 'btn-success', + id: 'new_payment_section' + } + end + end + end + end +end diff --git a/app/views/spree/admin/payments/index.html.erb b/app/views/spree/admin/payments/index.html.erb index c9dbe3cd49..72e9340023 100644 --- a/app/views/spree/admin/payments/index.html.erb +++ b/app/views/spree/admin/payments/index.html.erb @@ -1,10 +1,15 @@ <%= render partial: 'spree/admin/shared/order_tabs', locals: { current: :payments } %> <% content_for :page_actions do %> - <% if @order.outstanding_balance? && can?(:create, Spree::Payment) %> - - <%= button_link_to Spree.t(:new_payment), new_admin_order_payment_url(@order), class: "btn-success", icon: 'add.svg' %> - + <% payments_actions.items.each do |action| %> + <% next unless action.available?(current_ability, @order) %> + <%= button_link_to( + action.text, + action.url(@order), + class: action.classes, + icon: action.icon_name, + id: action.id + ) %> <% end %> <% end %> diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 16f23b63f3..38afcecb9a 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -40,6 +40,7 @@ class Engine < ::Rails::Engine Rails.application.config.spree_backend.actions.include?(:images) ? (Rails.application.config.spree_backend.actions[:images].items << Spree::Admin::Actions::ImagesDefaultActionsBuilder.new.build.items).flatten! : Rails.application.config.spree_backend.actions[:images] = Spree::Admin::Actions::ImagesDefaultActionsBuilder.new.build Rails.application.config.spree_backend.actions[:store_credits] = Spree::Admin::Actions::StoreCreditsDefaultActionsBuilder.new.build Rails.application.config.spree_backend.actions[:adjustments] = Spree::Admin::Actions::AdjustmentsDefaultActionsBuilder.new.build + Rails.application.config.spree_backend.actions[:payments] = Spree::Admin::Actions::PaymentsDefaultActionsBuilder.new.build end end end diff --git a/spec/models/spree/admin/actions/payments_default_actions_builder_spec.rb b/spec/models/spree/admin/actions/payments_default_actions_builder_spec.rb new file mode 100644 index 0000000000..099b1c875b --- /dev/null +++ b/spec/models/spree/admin/actions/payments_default_actions_builder_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +module Spree + module Admin + describe Actions::PaymentsDefaultActionsBuilder, type: :model do + let(:builder) { described_class.new } + let(:default_actions) do + [:new_payment] + end + + describe '#build' do + subject { builder.build } + + it 'builds default tabs' do + expect(subject.items.map(&:key)).to match(default_actions) + end + end + end + end +end