Skip to content

Commit ec72503

Browse files
committed
Admin product properties: Load modal with turbo frame
1 parent 18b7af5 commit ec72503

File tree

7 files changed

+26
-145
lines changed

7 files changed

+26
-145
lines changed

admin/app/components/solidus_admin/properties/edit/component.html.erb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<%= turbo_frame_tag :edit_property_modal do %>
1+
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
22
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3-
<%= form_for @property, url: solidus_admin.property_path(@property), html: { id: form_id } do |f| %>
3+
<%= form_for @property, url: form_url, html: { id: form_id } do |f| %>
44
<div class="flex flex-col gap-6 pb-4">
55
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
66
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
@@ -14,4 +14,3 @@
1414
<% end %>
1515
<% end %>
1616
<% end %>
17-
<%= render component("properties/index").new(page: @page) %>
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
# frozen_string_literal: true
22

3-
class SolidusAdmin::Properties::Edit::Component < SolidusAdmin::BaseComponent
4-
def initialize(page:, property:)
5-
@page = page
6-
@property = property
7-
end
8-
9-
def form_id
10-
dom_id(@property, "#{stimulus_id}_edit_property_form")
11-
end
3+
class SolidusAdmin::Properties::Edit::Component < SolidusAdmin::Resources::Edit::Component
124
end

admin/app/components/solidus_admin/properties/index/component.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,20 @@ def search_url
1717
solidus_admin.properties_path
1818
end
1919

20-
def row_url(property)
21-
solidus_admin.edit_property_path(property, _turbo_frame: :edit_property_modal)
20+
def edit_path(property)
21+
solidus_admin.edit_property_path(property, **search_filter_params)
2222
end
2323

2424
def turbo_frames
25-
%w[
26-
new_property_modal
27-
edit_property_modal
28-
]
25+
%w[resource_modal]
2926
end
3027

3128
def page_actions
3229
render component("ui/button").new(
3330
tag: :a,
3431
text: t('.add'),
35-
href: solidus_admin.new_property_path, data: { turbo_frame: :new_property_modal },
32+
href: solidus_admin.new_property_path(**search_filter_params),
33+
data: { turbo_frame: :resource_modal },
3634
icon: "add-line",
3735
)
3836
end
@@ -41,7 +39,7 @@ def batch_actions
4139
[
4240
{
4341
label: t('.batch_actions.delete'),
44-
action: solidus_admin.properties_path,
42+
action: solidus_admin.properties_path(**search_filter_params),
4543
method: :delete,
4644
icon: 'delete-bin-7-line',
4745
},
@@ -59,7 +57,9 @@ def name_column
5957
{
6058
header: :name,
6159
data: ->(property) do
62-
content_tag :div, property.name
60+
link_to property.name, edit_path(property),
61+
data: { turbo_frame: :resource_modal },
62+
class: 'body-link'
6363
end
6464
}
6565
end
@@ -68,7 +68,9 @@ def presentation_column
6868
{
6969
header: :presentation,
7070
data: ->(property) do
71-
content_tag :div, property.presentation
71+
link_to property.presentation, edit_path(property),
72+
data: { turbo_frame: :resource_modal },
73+
class: 'body-link'
7274
end
7375
}
7476
end

admin/app/components/solidus_admin/properties/new/component.html.erb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<%= turbo_frame_tag :new_property_modal do %>
1+
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
22
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3-
<%= form_for @property, url: solidus_admin.properties_path, html: { id: form_id } do |f| %>
3+
<%= form_for @property, url: form_url, html: { id: form_id } do |f| %>
44
<div class="flex flex-col gap-6 pb-4">
55
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
66
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
@@ -14,5 +14,3 @@
1414
<% end %>
1515
<% end %>
1616
<% end %>
17-
18-
<%= render component("properties/index").new(page: @page) %>
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
# frozen_string_literal: true
22

3-
class SolidusAdmin::Properties::New::Component < SolidusAdmin::BaseComponent
4-
def initialize(page:, property:)
5-
@page = page
6-
@property = property
7-
end
8-
9-
def form_id
10-
dom_id(@property, "#{stimulus_id}_new_property_form")
11-
end
3+
class SolidusAdmin::Properties::New::Component < SolidusAdmin::Resources::New::Component
124
end
Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,15 @@
11
# frozen_string_literal: true
22

33
module SolidusAdmin
4-
class PropertiesController < SolidusAdmin::BaseController
5-
include SolidusAdmin::ControllerHelpers::Search
6-
7-
before_action :set_property, only: %i[edit update]
8-
9-
def index
10-
set_index_page
11-
12-
respond_to do |format|
13-
format.html { render component('properties/index').new(page: @page) }
14-
end
15-
end
16-
17-
def new
18-
@property = Spree::Property.new
19-
20-
set_index_page
21-
22-
respond_to do |format|
23-
format.html { render component('properties/new').new(page: @page, property: @property) }
24-
end
25-
end
26-
27-
def create
28-
@property = Spree::Property.new(property_params)
29-
30-
if @property.save
31-
respond_to do |format|
32-
flash[:notice] = t('.success')
33-
34-
format.html do
35-
redirect_to solidus_admin.properties_path, status: :see_other
36-
end
37-
38-
format.turbo_stream do
39-
render turbo_stream: '<turbo-stream action="refresh" />'
40-
end
41-
end
42-
else
43-
set_index_page
44-
45-
respond_to do |format|
46-
format.html do
47-
page_component = component('properties/new').new(page: @page, property: @property)
48-
render page_component, status: :unprocessable_entity
49-
end
50-
end
51-
end
52-
end
53-
54-
def edit
55-
set_index_page
56-
57-
respond_to do |format|
58-
format.html { render component('properties/edit').new(page: @page, property: @property) }
59-
end
60-
end
61-
62-
def update
63-
if @property.update(property_params)
64-
respond_to do |format|
65-
flash[:notice] = t('.success')
66-
67-
format.html do
68-
redirect_to solidus_admin.properties_path, status: :see_other
69-
end
70-
71-
format.turbo_stream do
72-
render turbo_stream: '<turbo-stream action="refresh" />'
73-
end
74-
end
75-
else
76-
set_index_page
77-
78-
respond_to do |format|
79-
format.html do
80-
page_component = component('properties/edit').new(page: @page, property: @property)
81-
render page_component, status: :unprocessable_entity
82-
end
83-
end
84-
end
85-
end
86-
87-
def destroy
88-
@properties = Spree::Property.where(id: params[:id])
89-
90-
Spree::Property.transaction do
91-
@properties.destroy_all
92-
end
93-
94-
flash[:notice] = t('.success')
95-
redirect_to properties_path, status: :see_other
96-
end
97-
4+
class PropertiesController < SolidusAdmin::ResourcesController
985
private
996

100-
def set_property
101-
@property = Spree::Property.find(params[:id])
102-
end
7+
def resource_class = Spree::Property
1038

104-
def property_params
9+
def permitted_resource_params
10510
params.require(:property).permit(:name, :presentation)
10611
end
10712

108-
def set_index_page
109-
properties = apply_search_to(
110-
Spree::Property.unscoped.order(id: :desc),
111-
param: :q,
112-
)
113-
114-
set_page_and_extract_portion_from(properties)
115-
end
13+
def resources_collection = Spree::Property.unscoped
11614
end
11715
end

admin/spec/features/properties_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
it "updates the property" do
5656
visit "/admin/properties"
57-
find_row("Color").click
57+
click_on "Color"
5858

5959
fill_in "Name", with: "Size"
6060
fill_in "Presentation", with: "Cool Size"
@@ -68,7 +68,7 @@
6868

6969
it "shows validation errors" do
7070
visit "/admin/properties"
71-
find_row("Color").click
71+
click_on "Color"
7272

7373
fill_in "Name", with: ""
7474
click_on "Update Property"
@@ -83,7 +83,7 @@
8383

8484
it "updates the property" do
8585
visit "/admin/properties"
86-
find_row("Color").click
86+
click_on "Color"
8787

8888
fill_in "Name", with: "Size"
8989
fill_in "Presentation", with: "Cool Size"
@@ -97,7 +97,7 @@
9797

9898
it "shows validation errors" do
9999
visit "/admin/properties"
100-
find_row("Color").click
100+
click_on "Color"
101101
expect(page).to have_field("Name", with: "Color")
102102
fill_in "Name", with: ""
103103
click_on "Update Property"

0 commit comments

Comments
 (0)