forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources_controller.rb
More file actions
155 lines (125 loc) · 4.04 KB
/
resources_controller.rb
File metadata and controls
155 lines (125 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true
module SolidusAdmin
class ResourcesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search
helper_method :search_filter_params
before_action :set_paginated_resources, only: %i[index]
before_action :set_resource, only: %i[edit update]
# GET /index
#
# Uses {set_paginated_resources} to set @resources
# and a instance variable with the plural name of the resource.
#
# Uses the geared_pagination gem to set @page for pagination.
#
# @see set_paginated_resources
# @see resources_collection
def index
respond_to do |format|
format.html { render index_component.new(page: @page) }
format.json { render json: blueprint.render(@page.records, view: blueprint_view) }
end
end
def new
@resource = resource_class.new
render new_component.new(@resource)
end
def create
@resource = resource_class.new(permitted_resource_params)
if @resource.save
flash[:notice] = t('.success')
redirect_to after_create_path, status: :see_other
else
page_component = new_component.new(@resource)
render_resource_form_with_errors(page_component)
end
end
def edit
render edit_component.new(@resource)
end
def update
if @resource.update(permitted_resource_params)
flash[:notice] = t('.success')
redirect_to after_update_path, status: :see_other
else
page_component = edit_component.new(@resource)
render_resource_form_with_errors(page_component)
end
end
def destroy
@resource = resource_class.where(id: params[:id])
resource_class.transaction { @resource.destroy_all }
flash[:notice] = t('.success')
redirect_back_or_to after_destroy_path, status: :see_other
end
private
def search_filter_params
request.params.slice(:q, :page)
end
def set_paginated_resources
@resources ||= apply_search_to(
resources_collection,
param: :q,
).tap do |resources|
instance_variable_set("@#{plural_resource_name}", resources)
# sets @page instance variable in geared_pagination gem
set_page_and_extract_portion_from(resources, ordered_by: resources_sorting_options, per_page:)
end
end
def resources_sorting_options
{ id: :desc }
end
def resources_collection
resource_class.all
end
def per_page; end
def set_resource
@resource ||= resource_class.find(params[:id]).tap do |resource|
instance_variable_set("@#{resource_name}", resource)
end
end
def resource_class
raise NotImplementedError,
"You must implement the resource_class method in #{self.class}"
end
def resource_name
resource_class.model_name.singular_route_key
end
def plural_resource_name
resource_class.model_name.route_key
end
def index_component
component("#{plural_resource_name}/index")
end
def new_component
component("#{plural_resource_name}/new")
end
def edit_component
component("#{plural_resource_name}/edit")
end
def render_resource_form_with_errors(page_component)
respond_to do |format|
format.html do
render page_component, status: :unprocessable_entity
end
format.turbo_stream do
render turbo_stream: turbo_stream.replace(:resource_modal, page_component),
status: :unprocessable_entity
end
end
end
def permitted_resource_params
raise NotImplementedError,
"You must implement the permitted_resource_params method in #{self.class}"
end
def after_create_path
solidus_admin.send("#{plural_resource_name}_path", **search_filter_params)
end
def after_update_path
solidus_admin.send("#{plural_resource_name}_path", **search_filter_params)
end
def after_destroy_path
solidus_admin.send("#{plural_resource_name}_path", **search_filter_params)
end
end
end