From 38e7ebe952a0486df326eff3e8823fc781b616e0 Mon Sep 17 00:00:00 2001 From: Tomasz Donarski Date: Thu, 26 Oct 2023 17:43:12 +0200 Subject: [PATCH] Product_properties#index - use prebuilt action --- app/helpers/spree/admin/navigation_helper.rb | 4 ++ ...duct_properties_default_actions_builder.rb | 55 +++++++++++++++++++ .../admin/product_properties/index.html.erb | 26 ++++----- lib/spree/backend/engine.rb | 1 + ...properties_default_actions_builder_spec.rb | 21 +++++++ 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 app/models/spree/admin/actions/product_properties_default_actions_builder.rb create mode 100644 spec/models/spree/admin/actions/product_properties_default_actions_builder_spec.rb diff --git a/app/helpers/spree/admin/navigation_helper.rb b/app/helpers/spree/admin/navigation_helper.rb index de8c50cd7f..53451567ce 100644 --- a/app/helpers/spree/admin/navigation_helper.rb +++ b/app/helpers/spree/admin/navigation_helper.rb @@ -373,6 +373,10 @@ def stock_actions def variants_actions Rails.application.config.spree_backend.actions[:variants] end + + def product_properties_actions + Rails.application.config.spree_backend.actions[:product_properties] + end # rubocop:enable Metrics/ModuleLength end end diff --git a/app/models/spree/admin/actions/product_properties_default_actions_builder.rb b/app/models/spree/admin/actions/product_properties_default_actions_builder.rb new file mode 100644 index 0000000000..cd2a886f79 --- /dev/null +++ b/app/models/spree/admin/actions/product_properties_default_actions_builder.rb @@ -0,0 +1,55 @@ +module Spree + module Admin + module Actions + class ProductPropertiesDefaultActionsBuilder + include Spree::Core::Engine.routes.url_helpers + + def build + root = Root.new + add_select_from_prototype_action(root) + add_add_product_properties_action(root) + root + end + + private + + def add_select_from_prototype_action(root) + action = + ActionBuilder.new(select_from_prototype_config). + build + + root.add(action) + end + + def select_from_prototype_config + { + icon_name: 'list.svg', + key: :select_from_prototype, + url: available_admin_prototypes_path, + classes: 'btn-light js-new-ptype-link', + data: { update: 'prototypes', remote: true } + } + end + + def add_add_product_properties_action(root) + action = + ActionBuilder.new(add_product_properties_config). + with_create_ability_check(::Spree::ProductProperty). + build + + root.add(action) + end + + def add_product_properties_config + { + icon_name: 'add.svg', + key: :add_product_properties, + url: 'javascript:;', + classes: 'btn-success spree_add_fields', + data: { target: 'tbody#sortVert'} + } + end + end + end + end +end diff --git a/app/views/spree/admin/product_properties/index.html.erb b/app/views/spree/admin/product_properties/index.html.erb index 1540025832..03ab94f72e 100644 --- a/app/views/spree/admin/product_properties/index.html.erb +++ b/app/views/spree/admin/product_properties/index.html.erb @@ -2,19 +2,19 @@ <%= render 'spree/admin/shared/error_messages', target: @product %> <% content_for :page_actions do %> - <%= external_page_preview_link(@product) %> - - - <%= button_link_to Spree.t(:select_from_prototype), - available_admin_prototypes_url, - { icon: 'list.svg', remote: true, 'data-update' => 'prototypes', class: 'btn-light' } %> - - - <%= button_link_to( - Spree.t(:add_product_properties), - "javascript:;", - { icon: 'add.svg', :'data-target' => "tbody#sortVert", class: 'btn-success spree_add_fields' }) %> -<% end if can? :create, Spree::ProductProperty %> + <% product_properties_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 %> +<% end %> <%= form_for @product, url: spree.admin_product_url(@product), method: :put do |f| %>
diff --git a/lib/spree/backend/engine.rb b/lib/spree/backend/engine.rb index 29af248b92..0fb8e4aaf2 100644 --- a/lib/spree/backend/engine.rb +++ b/lib/spree/backend/engine.rb @@ -42,6 +42,7 @@ class Engine < ::Rails::Engine 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 + Rails.application.config.spree_backend.actions.include?(:product_properties) ? (Rails.application.config.spree_backend.actions[:product_properties].items << Spree::Admin::Actions::ProductPropertiesDefaultActionsBuilder.new.build.items).flatten! : Rails.application.config.spree_backend.actions[:product_properties] = Spree::Admin::Actions::ProductPropertiesDefaultActionsBuilder.new.build end end end diff --git a/spec/models/spree/admin/actions/product_properties_default_actions_builder_spec.rb b/spec/models/spree/admin/actions/product_properties_default_actions_builder_spec.rb new file mode 100644 index 0000000000..18466dbd79 --- /dev/null +++ b/spec/models/spree/admin/actions/product_properties_default_actions_builder_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +module Spree + module Admin + describe Actions::ProductPropertiesDefaultActionsBuilder, type: :model do + let(:builder) { described_class.new } + let(:default_actions) do + [:select_from_prototype + "javascript:;"] + 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