Skip to content

Commit

Permalink
Variants#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 cab307a commit d41645f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 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 @@ -369,6 +369,10 @@ def payments_actions
def stock_actions
Rails.application.config.spree_backend.actions[:stock]
end

def variants_actions
Rails.application.config.spree_backend.actions[:variants]
end
# rubocop:enable Metrics/ModuleLength
end
end
Expand Down
37 changes: 37 additions & 0 deletions app/models/spree/admin/actions/variants_default_actions_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Spree
module Admin
module Actions
class VariantsDefaultActionsBuilder
include Spree::Core::Engine.routes.url_helpers

def build
root = Root.new
add_new_variant_action(root)
root
end

private

def add_new_variant_action(root)
action =
ActionBuilder.new(new_variant_config).
with_create_ability_check(::Spree::Variant).
build

root.add(action)
end

def new_variant_config
{
icon_name: 'add.svg',
key: :new_variant,
url: ->(resource) { new_admin_product_variant_path(resource) },
classes: 'btn-success',
id: 'new_var_link',
data: { update: 'new_variant' }
}
end
end
end
end
end
14 changes: 12 additions & 2 deletions app/views/spree/admin/variants/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,18 @@
<% end %>
<% else %>
<% content_for :page_actions do %>
<%= external_page_preview_link(@product) %>
<%= button_link_to(Spree.t(:new_variant), spree.new_admin_product_variant_url(@product), { icon: 'add.svg', :'data-update' => 'new_variant', class: 'btn-success', id: 'new_var_link' }) if can? :create, Spree::Variant %>
<% variants_actions.items.each do |action| %>
<% next unless action.available?(current_ability) %>
<%= button_link_to(
action.text,
action.url(@product),
class: action.classes,
icon: action.icon_name,
id: action.id,
target: action.target,
data: action.data
) %>
<% end %>
<%= button_link_to (@deleted == '1' ? Spree.t(:show_active) : Spree.t(:show_deleted)), spree.admin_product_variants_url(@product, { q: { deleted_at_null: @deleted == '1' ? "0" : "1" } }), { class: 'btn-light', icon: 'filter.svg' } %>
<% 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 @@ -41,6 +41,7 @@ class Engine < ::Rails::Engine
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
Rails.application.config.spree_backend.actions.include?(:variants) ? (Rails.application.config.spree_backend.actions[:variants].items << Spree::Admin::Actions::VariantsDefaultActionsBuilder.new.build.items).flatten! : Rails.application.config.spree_backend.actions[:variants] = Spree::Admin::Actions::VariantsDefaultActionsBuilder.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::VariantsDefaultActionsBuilder, type: :model do
let(:builder) { described_class.new }
let(:default_actions) do
[:new_variant]
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 d41645f

Please sign in to comment.