Skip to content

Commit

Permalink
Product_properties#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 d41645f commit 38e7ebe
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 13 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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
26 changes: 13 additions & 13 deletions app/views/spree/admin/product_properties/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<%= render 'spree/admin/shared/error_messages', target: @product %>

<% content_for :page_actions do %>
<%= external_page_preview_link(@product) %>

<span class="js-new-ptype-link">
<%= button_link_to Spree.t(:select_from_prototype),
available_admin_prototypes_url,
{ icon: 'list.svg', remote: true, 'data-update' => 'prototypes', class: 'btn-light' } %>
</span>

<%= 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| %>
<fieldset>
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 38e7ebe

Please sign in to comment.