Skip to content

Commit

Permalink
Create 'extension partials' helper, present with an example
Browse files Browse the repository at this point in the history
  • Loading branch information
mateus-po committed Feb 22, 2024
1 parent 808c4ac commit 4d1b565
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
27 changes: 27 additions & 0 deletions app/helpers/spree/admin/extension_partials_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Spree
module Admin
module ExtensionPartialsHelper
def render_matching(pattern:nil, locals: {})
old_path = Dir.pwd
rendered = ''
return rendered if pattern.nil?

view_paths.paths.each do |view_path|
Dir.chdir(view_path)
result = Dir['spree/admin/extension/**/*'].select do |path|
!File.directory?(path) && File.basename(path, '.html.erb') =~ /^_.+/
end

result.map! { |path| path.gsub(File.basename(path), File.basename(path, '.html.erb')[1..-1]) }
result.each do |path|
if path.match(/.+\/#{pattern}\/.+/)
rendered += render partial: path, locals: locals
end
end
end
Dir.chdir(old_path)
rendered
end
end
end
end
19 changes: 10 additions & 9 deletions app/views/spree/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
<% product_actions.items.each do |action| %>
<% next unless action.available?(current_ability) %>
<%= button_link_to(
Spree.t(action.label_translation_key),
action.url(@product),
class: action.classes,
icon: action.icon_key,
id: action.id,
target: action.target,
data: action.data_attributes
) %>
Spree.t(action.label_translation_key),
action.url(@product),
class: action.classes,
icon: action.icon_key,
id: action.id,
target: action.target,
data: action.data_attributes
) %>
<% end if product_actions&.items %>
<% end %>
<%= render partial: 'spree/admin/shared/product_tabs', locals: {current: :details} %>
<%= render partial: 'spree/admin/shared/product_tabs', locals: { current: :details } %>
<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @product } %>
<%= form_for [:admin, @product], method: :put, html: { multipart: true } do |f| %>
<fieldset>
<%= render partial: 'form', locals: { f: f } %>
<%= render_matching pattern: 'products/edit', locals: { f: f } %>
<%= render partial: 'spree/admin/shared/edit_resource_links' %>
</fieldset>
<% end %>

0 comments on commit 4d1b565

Please sign in to comment.