Skip to content

Commit

Permalink
Payments#index - use prebuilt action
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdonarski committed Oct 26, 2023
1 parent 5d16081 commit 4b89f74
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/helpers/spree/admin/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions app/models/spree/admin/actions/payments_default_actions_builder.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 9 additions & 4 deletions app/views/spree/admin/payments/index.html.erb
Original file line number Diff line number Diff line change
@@ -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) %>
<span id="new_payment_section">
<%= button_link_to Spree.t(:new_payment), new_admin_order_payment_url(@order), class: "btn-success", icon: 'add.svg' %>
</span>
<% 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 %>

Expand Down
1 change: 1 addition & 0 deletions lib/spree/backend/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4b89f74

Please sign in to comment.