Skip to content

Commit 06ac254

Browse files
committed
Update CRUD for option values
Adds modals for option value add/edit
1 parent 5fba2b1 commit 06ac254

File tree

12 files changed

+106
-9
lines changed

12 files changed

+106
-9
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@
2525
<%= render_option_values %>
2626
<% end %>
2727

28-
<% panel.with_action(name: t(".panels.option_values.add"), href: "#") %>
28+
<% panel.with_action(
29+
name: t(".panels.option_values.add"),
30+
href: solidus_admin.new_option_type_option_value_path(@option_type),
31+
data: { turbo_frame: :option_value_modal }
32+
) %>
2933
<% end %>
3034

3135
<%= page_footer do %>
3236
<%= render component("ui/button").new(text: t(".save"), form: form_id) %>
3337
<% end %>
3438
</div>
39+
40+
<%= turbo_frame_tag :option_value_modal, target: "_top" %>
3541
<% end %>
3642
<% end %>

admin/app/components/solidus_admin/option_types/edit/component.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ def option_value_columns
3434
{
3535
header: :name,
3636
data: ->(option_value) do
37-
link_to option_value.name, "#",
38-
class: 'body-link'
37+
link_to option_value.name, solidus_admin.edit_option_value_path(option_value),
38+
class: 'body-link',
39+
data: { turbo_frame: :option_value_modal }
3940
end
4041
},
4142
{
4243
header: :presentation,
4344
data: ->(option_value) do
44-
link_to option_value.presentation, "#",
45-
class: 'body-link'
45+
link_to option_value.presentation, solidus_admin.edit_option_value_path(option_value),
46+
class: 'body-link',
47+
data: { turbo_frame: :option_value_modal }
4648
end
4749
},
4850
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= turbo_frame_tag :option_value_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @option_value, url: form_url, html: { id: form_id } do |f| %>
4+
<div class="flex flex-col gap-6 pb-4">
5+
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
6+
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
7+
</div>
8+
<% modal.with_actions do %>
9+
<form method="dialog">
10+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
11+
</form>
12+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<% end %>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::OptionValues::Edit::Component < SolidusAdmin::Resources::Edit::Component
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: "Edit Option Value"
3+
cancel: "Cancel"
4+
submit: "Update Option Value"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%= turbo_frame_tag :option_value_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @option_value, url: form_url, html: { id: form_id } do |f| %>
4+
<div class="flex flex-col gap-6 pb-4">
5+
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
6+
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
7+
</div>
8+
<% modal.with_actions do %>
9+
<form method="dialog">
10+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
11+
</form>
12+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<% end %>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::OptionValues::New::Component < SolidusAdmin::Resources::New::Component
4+
def form_url
5+
solidus_admin.option_type_option_values_path(@option_value.option_type)
6+
end
7+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: "New Option Value"
3+
cancel: "Cancel"
4+
submit: "Add Option Value"

admin/app/controllers/solidus_admin/option_values_controller.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,40 @@ module SolidusAdmin
44
class OptionValuesController < SolidusAdmin::ResourcesController
55
include SolidusAdmin::Moveable
66

7+
before_action :set_option_type, only: [:new, :create]
8+
9+
def new
10+
@resource = @option_type.option_values.build
11+
super
12+
end
13+
14+
def create
15+
@resource = @option_type.option_values.build(permitted_resource_params)
16+
super
17+
end
18+
719
private
820

921
def resource_class = Spree::OptionValue
22+
23+
def permitted_resource_params
24+
params.require(:option_value).permit(:name, :presentation)
25+
end
26+
27+
def resource_form_frame
28+
:option_value_modal
29+
end
30+
31+
def after_create_path
32+
solidus_admin.edit_option_type_path(@option_type)
33+
end
34+
35+
def after_update_path
36+
solidus_admin.edit_option_type_path(@option_value.option_type)
37+
end
38+
39+
def set_option_type
40+
@option_type = Spree::OptionType.find(params[:option_type_id])
41+
end
1042
end
1143
end

admin/app/controllers/solidus_admin/resources_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def index
2525
end
2626

2727
def new
28-
@resource = resource_class.new
28+
@resource ||= resource_class.new
2929
render new_component.new(@resource)
3030
end
3131

3232
def create
33-
@resource = resource_class.new(permitted_resource_params)
33+
@resource ||= resource_class.new(permitted_resource_params)
3434

3535
if @resource.save
3636
flash[:notice] = t('.success')

0 commit comments

Comments
 (0)