-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathresource_index_component.rb
263 lines (212 loc) · 6.77 KB
/
resource_index_component.rb
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# frozen_string_literal: true
class Avo::Views::ResourceIndexComponent < Avo::ResourceComponent
include Avo::ResourcesHelper
include Avo::ApplicationHelper
include Avo::Concerns::ChecksShowAuthorization
prop :resource
prop :resources
prop :records, default: [].freeze
prop :pagy
prop :index_params, default: {}.freeze
prop :filters, default: [].freeze
prop :actions, default: [].freeze
prop :reflection
prop :turbo_frame, default: ""
prop :parent_record
prop :parent_resource
prop :applied_filters, default: {}.freeze
prop :query, reader: :public
prop :scopes, reader: :public
def title
if @reflection.present?
return name if field.present?
reflection_resource.plural_name
else
@resource.plural_name
end
end
def view_type
@index_params[:view_type]
end
def bulk_edit_path
# Add the `view` param to let Avo know where to redirect back when the user clicks the `Cancel` button.
args = {via_view: "index"}
if @parent_record.present?
args = {
via_resource_class: parent_resource.class.to_s,
via_record_id: @parent_record.to_param
}
end
helpers.edit_bulk_update_path(resource: @resource, **args)
end
def available_view_types
@index_params[:available_view_types]
end
# The Create button is dependent on the new? policy method.
# The create? should be called only when the user clicks the Save button so the developers gets access to the params from the form.
def can_see_the_create_button?
# Disable creation for ArrayResources
return false if @resource.resource_type_array?
return authorize_association_for(:create) if @reflection.present?
@resource.authorization.authorize_action(:new, raise_exception: false) && !has_reflection_and_is_read_only
end
def can_attach?
return false if has_reflection_and_is_read_only
reflection_class = if @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
@reflection.through_reflection.class
else
@reflection.class
end
return false unless reflection_class.in? [
ActiveRecord::Reflection::HasManyReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection
]
authorize_association_for(:attach)
end
def create_path
args = {}
if @reflection.present?
args = {
via_resource_class: @parent_resource.class,
via_relation_class: reflection_model_class,
via_record_id: @parent_record.to_param
}
if @reflection.class.in? [
ActiveRecord::Reflection::ThroughReflection,
ActiveRecord::Reflection::HasAndBelongsToManyReflection
]
args[:via_relation] = params[:resource_name]
end
if @reflection.is_a? ActiveRecord::Reflection::HasManyReflection
args[:via_relation] = @reflection.name
end
if @reflection.inverse_of.present?
args[:via_relation] = @reflection.inverse_of.name
end
end
helpers.new_resource_path(resource: @resource, **args)
end
def attach_path
current_path = CGI.unescape(request.env["PATH_INFO"]).split("/").select(&:present?)
Avo.root_path(
paths: [*current_path, "new"],
query: {
view: @parent_resource&.view&.to_s,
turbo_frame: params[:turbo_frame],
for_attribute: field&.try(:for_attribute)
}.compact
)
end
def singular_resource_name
if @reflection.present?
return name.singularize if field.present?
reflection_resource.name
else
@resource.singular_name || @resource.model_class.model_name.name.downcase
end
end
def description
# If this is a has many association, the user can pass a description to be shown just for this association.
return field&.description(query: @query) if @reflection.present?
@resource.description
end
def show_search_input
return false unless authorized_to_search?
return false unless @resource.class.search_query.present?
return false if field&.hide_search_input
true
end
def authorized_to_search?
# Hide the search if the authorization prevents it
@resource.authorization.authorize_action("search", raise_exception: false)
end
def render_dynamic_filters_button
return unless Avo.avo_dynamic_filters_installed?
return unless @resource.has_filters?
return if Avo::DynamicFilters.configuration.always_expanded
a_button size: :sm,
color: :primary,
icon: "avo/filter",
data: {
controller: "avo-filters",
action: "click->avo-filters#toggleFiltersArea",
avo_filters_dynamic_filters_component_id_value: dynamic_filters_component_id
} do
Avo::DynamicFilters.configuration.button_label
end
end
def render_bulk_update_button
a_link helpers.edit_bulk_update_path(resource_name: @resource.name, id: 4),
style: :primary,
color: :primary,
icon: "avo/edit",
form_class: "flex flex-col sm:flex-row sm:inline-flex" do
"Bulk update"
end
end
def scopes_list
Avo::Advanced::Scopes::ListComponent.new(
scopes: @scopes,
resource: @resource,
turbo_frame: @turbo_frame,
parent_record: @parent_record,
query: @query,
loader: @resource.entity_loader(:scope)
)
end
def can_render_scopes?
defined?(Avo::Advanced)
end
def back_path
# The `return_to` param takes precedence over anything else.
return params[:return_to] if params[:return_to].present?
# Show Go Back link only when association page is opened
# as a standalone page
if @reflection.present? && !helpers.turbo_frame_request?
helpers.resource_path(record: @parent_record, resource: @parent_resource)
end
end
private
def reflection_model_class
@reflection.active_record.to_s
end
def name
field.custom_name? ? field.name : field.plural_name
end
def via_reflection
return unless @reflection.present?
{
association: "has_many",
association_id: @reflection.name,
class: reflection_model_class,
id: @parent_record.to_param,
view: @parent_resource.view
}
end
def header_visible?
search_query_present? || filters_present? || has_many_view_types? || has_dynamic_filters?
end
def has_dynamic_filters?
Avo.avo_dynamic_filters_installed? && @resource.has_filters?
end
def search_query_present?
@resource.class.search_query.present?
end
def filters_present?
@filters.present?
end
def has_many_view_types?
available_view_types.count > 1
end
# Generate a unique component id for the current component.
# This is used to identify the component in the DOM.
def dynamic_filters_component_id
@dynamic_filters_component_id ||= "dynamic_filters_component_id_#{SecureRandom.hex(3)}"
end
def reloadable
field&.reloadable?
end
def linkable?
field&.linkable?
end
end