Class: Avo::BaseResource
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#get_stimulus_controllers, #stimulus_data_attributes
#has_show_controls?, #render_show_controls
#with_new_items
#fields, #get_field, #get_field_definitions, #get_fields, #get_items, #get_tabs, #hydrate_fields, #tab_groups, #tools
Constructor Details
Returns a new instance of BaseResource.
134
135
136
137
138
139
140
|
# File 'lib/avo/base_resource.rb', line 134
def initialize
unless self.class.model_class.present?
if model_class.present? && model_class.respond_to?(:base_class)
self.class.model_class = model_class.base_class
end
end
end
|
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
26
27
28
|
# File 'lib/avo/base_resource.rb', line 26
def params
@params
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
24
25
26
|
# File 'lib/avo/base_resource.rb', line 24
def reflection
@reflection
end
|
#user ⇒ Object
Returns the value of attribute user.
25
26
27
|
# File 'lib/avo/base_resource.rb', line 25
def user
@user
end
|
#view ⇒ Object
Returns the value of attribute view.
23
24
25
|
# File 'lib/avo/base_resource.rb', line 23
def view
@view
end
|
Class Method Details
.action(action_class) ⇒ Object
65
66
67
68
69
|
# File 'lib/avo/base_resource.rb', line 65
def action(action_class)
self.actions_loader ||= Avo::Loaders::Loader.new
self.actions_loader.use action_class
end
|
.filter(filter_class) ⇒ Object
71
72
73
74
75
|
# File 'lib/avo/base_resource.rb', line 71
def filter(filter_class)
self.filters_loader ||= Avo::Loaders::Loader.new
self.filters_loader.use filter_class
end
|
.find_scope ⇒ Object
This resolves the scope when finding records (not “where” queries)
91
92
93
94
95
|
# File 'lib/avo/base_resource.rb', line 91
def find_scope
final_scope = resolve_find_scope.present? ? resolve_find_scope.call(model_class: model_class) : model_class
authorization.apply_policy final_scope
end
|
.get_available_models ⇒ Object
123
124
125
|
# File 'lib/avo/base_resource.rb', line 123
def get_available_models
ApplicationRecord.descendants
end
|
.get_record_associations(record) ⇒ Object
107
108
109
|
# File 'lib/avo/base_resource.rb', line 107
def get_record_associations(record)
record._reflections
end
|
.grid(&block) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/avo/base_resource.rb', line 58
def grid(&block)
grid_collector = GridCollector.new
grid_collector.instance_eval(&block)
self.grid_loader = grid_collector
end
|
.order_actions ⇒ Object
101
102
103
104
105
|
# File 'lib/avo/base_resource.rb', line 101
def order_actions
return {} if ordering.blank?
ordering.dig(:actions) || {}
end
|
.query_scope ⇒ Object
This resolves the scope when doing “where” queries (not find queries)
84
85
86
87
88
|
# File 'lib/avo/base_resource.rb', line 84
def query_scope
final_scope = resolve_query_scope.present? ? resolve_query_scope.call(model_class: model_class) : model_class
authorization.apply_policy final_scope
end
|
.scope ⇒ Object
This is the search_query scope This should be removed and passed to the search block
79
80
81
|
# File 'lib/avo/base_resource.rb', line 79
def scope
query_scope
end
|
.valid_association_name(record, association_name) ⇒ Object
111
112
113
114
115
|
# File 'lib/avo/base_resource.rb', line 111
def valid_association_name(record, association_name)
get_record_associations(record).keys.find do |name|
name == association_name
end
end
|
.valid_attachment_name(record, association_name) ⇒ Object
117
118
119
120
121
|
# File 'lib/avo/base_resource.rb', line 117
def valid_attachment_name(record, association_name)
get_record_associations(record).keys.each do |name|
return association_name if name == "#{association_name}_attachment" || name == "#{association_name}_attachments"
end
end
|
.valid_model_class(model_class) ⇒ Object
127
128
129
130
131
|
# File 'lib/avo/base_resource.rb', line 127
def valid_model_class(model_class)
get_available_models.find do |m|
m.to_s == model_class.to_s
end
end
|
Instance Method Details
#attached_file_fields ⇒ Object
282
283
284
285
286
|
# File 'lib/avo/base_resource.rb', line 282
def attached_file_fields
get_field_definitions.select do |field|
[Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
end
end
|
#authorization(user: nil) ⇒ Object
317
318
319
320
|
# File 'lib/avo/base_resource.rb', line 317
def authorization(user: nil)
current_user = user || Avo::App.current_user
Avo::Services::AuthorizationService.new(current_user, model || model_class, policy_class: authorization_policy)
end
|
#available_view_types ⇒ Object
274
275
276
277
278
279
280
|
# File 'lib/avo/base_resource.rb', line 274
def available_view_types
view_types = [:table]
view_types << :grid if get_grid_fields.present?
view_types
end
|
#avatar ⇒ Object
437
438
439
440
441
442
443
444
445
|
# File 'lib/avo/base_resource.rb', line 437
def avatar
return avatar_field.to_image if avatar_field.respond_to? :to_image
return avatar_field.value.variant(resize_to_limit: [480, 480]) if avatar_field.type == "file"
avatar_field.value
rescue
nil
end
|
#avatar_field ⇒ Object
429
430
431
432
433
434
435
|
# File 'lib/avo/base_resource.rb', line 429
def avatar_field
get_field_definitions.find do |field|
field.as_avatar.present?
end
rescue
nil
end
|
#avatar_type ⇒ Object
447
448
449
450
451
|
# File 'lib/avo/base_resource.rb', line 447
def avatar_type
avatar_field.as_avatar
rescue
nil
end
|
#cache_hash(parent_model) ⇒ Object
340
341
342
343
344
345
346
|
# File 'lib/avo/base_resource.rb', line 340
def cache_hash(parent_model)
if parent_model.present?
[model, file_hash, parent_model]
else
[model, file_hash]
end
end
|
#class_name_without_resource ⇒ Object
192
193
194
|
# File 'lib/avo/base_resource.rb', line 192
def class_name_without_resource
self.class.name.demodulize.delete_suffix("Resource")
end
|
#default_panel_name ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/avo/base_resource.rb', line 179
def default_panel_name
return @params[:related_name].capitalize if @params.present? && @params[:related_name].present?
case @view
when :show
model_title
when :edit
model_title
when :new
t("avo.create_new_item", item: name.downcase).upcase_first
end
end
|
#description ⇒ Object
461
462
463
|
# File 'lib/avo/base_resource.rb', line 461
def description
description_field&.value
end
|
#description_field ⇒ Object
453
454
455
456
457
458
459
|
# File 'lib/avo/base_resource.rb', line 453
def description_field
get_field_definitions.find do |field|
field.as_description.present?
end
rescue
nil
end
|
#file_hash ⇒ Object
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/avo/base_resource.rb', line 322
def file_hash
content_to_be_hashed = ""
resource_path = Rails.root.join("app", "avo", "resources", "#{self.class.name.underscore}.rb").to_s
if File.file? resource_path
content_to_be_hashed += File.read(resource_path)
end
policy_path = Rails.root.join("app", "policies", "#{self.class.name.underscore.gsub("_resource", "")}_policy.rb").to_s
if File.file? policy_path
content_to_be_hashed += File.read(policy_path)
end
Digest::MD5.hexdigest(content_to_be_hashed)
end
|
#fill_model(model, params, extra_params: []) ⇒ Object
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/avo/base_resource.rb', line 288
def fill_model(model, params, extra_params: [])
fields_by_database_id = get_field_definitions
.reject do |field|
field.computed
end
.map do |field|
[field.database_id.to_s, field]
end
.to_h
params.each do |key, value|
field = fields_by_database_id[key]
next unless field.present?
model = field.fill_field model, key, value, params
end
if .present?
model.assign_attributes params.permit()
end
model
end
|
465
466
467
|
# File 'lib/avo/base_resource.rb', line 465
def form_scope
model_class.base_class.to_s.underscore.downcase
end
|
#get_actions ⇒ Object
173
174
175
176
177
|
# File 'lib/avo/base_resource.rb', line 173
def get_actions
return [] if self.class.actions_loader.blank?
self.class.actions_loader.bag
end
|
#get_filters ⇒ Object
167
168
169
170
171
|
# File 'lib/avo/base_resource.rb', line 167
def get_filters
return [] if self.class.filters_loader.blank?
self.class.filters_loader.bag
end
|
#get_grid_fields ⇒ Object
161
162
163
164
165
|
# File 'lib/avo/base_resource.rb', line 161
def get_grid_fields
return if self.class.grid_loader.blank?
self.class.grid_loader.hydrate(model: @model, view: @view, resource: self)
end
|
#has_model_id? ⇒ Boolean
473
474
475
|
# File 'lib/avo/base_resource.rb', line 473
def has_model_id?
model.present? && model.id.present?
end
|
#hydrate(model: nil, view: nil, user: nil, params: nil) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/avo/base_resource.rb', line 147
def hydrate(model: nil, view: nil, user: nil, params: nil)
@view = view if view.present?
@user = user if user.present?
@params = params if params.present?
if model.present?
@model = model
hydrate_model_with_default_values if @view == :new
end
self
end
|
#hydrate_model_with_default_values ⇒ Object
We will not overwrite any attributes that come pre-filled in the model.
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# File 'lib/avo/base_resource.rb', line 349
def hydrate_model_with_default_values
default_values = get_fields
.select do |field|
!field.computed
end
.map do |field|
id = field.id
value = field.value
if field.type == "belongs_to"
id = field.foreign_key.to_sym
reflection = @model._reflections[@params[:via_relation]]
if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
value = @params[:via_relation_class].safe_constantize.find(@params[:via_resource_id])
elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
value = @params[:via_resource_id]
end
end
[id, value]
end
.to_h
.select do |id, value|
value.present?
end
default_values.each do |id, value|
if @model.send(id).nil?
@model.send("#{id}=", value)
end
end
end
|
#label ⇒ Object
425
426
427
|
# File 'lib/avo/base_resource.rb', line 425
def label
label_field&.value || model_title
end
|
#label_field ⇒ Object
417
418
419
420
421
422
423
|
# File 'lib/avo/base_resource.rb', line 417
def label_field
get_field_definitions.find do |field|
field.as_label.present?
end
rescue
nil
end
|
#model_class ⇒ Object
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/avo/base_resource.rb', line 196
def model_class
return self.class.model_class if self.class.model_class.present?
return @model.base_class if @model.present?
class_name_without_resource.safe_constantize
end
|
#model_id ⇒ Object
207
208
209
|
# File 'lib/avo/base_resource.rb', line 207
def model_id
@model.send id
end
|
#model_key ⇒ Object
This is used as the model class ID We use this instead of the route_key to maintain compatibility with uncountable models With uncountable models route key appends an _index suffix (Fish->fish_index) Example: User->users, MediaItem->media_items, Fish->fish
397
398
399
|
# File 'lib/avo/base_resource.rb', line 397
def model_key
model_class.model_name.plural
end
|
#model_name ⇒ Object
401
402
403
|
# File 'lib/avo/base_resource.rb', line 401
def model_name
model_class.model_name
end
|
#model_title ⇒ Object
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/avo/base_resource.rb', line 211
def model_title
return name if @model.nil?
the_title = @model.send title
return the_title if the_title.present?
model_id
rescue
name
end
|
#name ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/avo/base_resource.rb', line 238
def name
default = class_name_without_resource.to_s.gsub('::', ' ').underscore.humanize
return @name if @name.present?
if translation_key && ::Avo::App.translation_enabled
t(translation_key, count: 1, default: default).capitalize
else
default
end
end
|
#navigation_label ⇒ Object
270
271
272
|
# File 'lib/avo/base_resource.rb', line 270
def navigation_label
plural_name.humanize
end
|
#ordering_host(**args) ⇒ Object
469
470
471
|
# File 'lib/avo/base_resource.rb', line 469
def ordering_host(**args)
Avo::Hosts::Ordering.new resource: self, options: self.class.ordering, **args
end
|
#plural_name ⇒ Object
254
255
256
257
258
259
260
261
262
|
# File 'lib/avo/base_resource.rb', line 254
def plural_name
default = name.pluralize
if translation_key && ::Avo::App.translation_enabled
t(translation_key, count: 2, default: default).capitalize
else
default
end
end
|
#record ⇒ Object
Also known as:
model
142
143
144
|
# File 'lib/avo/base_resource.rb', line 142
def record
@model
end
|
#record_path ⇒ Object
409
410
411
|
# File 'lib/avo/base_resource.rb', line 409
def record_path
resource_path(model: model, resource: self)
end
|
#records_path ⇒ Object
413
414
415
|
# File 'lib/avo/base_resource.rb', line 413
def records_path
resources_path(resource: self)
end
|
#resource_description ⇒ Object
222
223
224
225
226
227
228
229
230
|
# File 'lib/avo/base_resource.rb', line 222
def resource_description
return instance_exec(&self.class.description) if self.class.description.respond_to? :call
if view == :index
return self.class.description if self.class.description.is_a? String
end
end
|
#route_key ⇒ Object
385
386
387
|
# File 'lib/avo/base_resource.rb', line 385
def route_key
class_name_without_resource.underscore.pluralize
end
|
#singular_model_key ⇒ Object
405
406
407
|
# File 'lib/avo/base_resource.rb', line 405
def singular_model_key
model_class.model_name.singular
end
|
#singular_name ⇒ Object
250
251
252
|
# File 'lib/avo/base_resource.rb', line 250
def singular_name
name
end
|
#singular_route_key ⇒ Object
389
390
391
|
# File 'lib/avo/base_resource.rb', line 389
def singular_route_key
route_key.singularize
end
|
#translation_key ⇒ Object
232
233
234
235
236
|
# File 'lib/avo/base_resource.rb', line 232
def translation_key
return "avo.resource_translations.#{class_name_without_resource.underscore}" if ::Avo::App.translation_enabled
self.class.translation_key
end
|
#underscore_name ⇒ Object
264
265
266
267
268
|
# File 'lib/avo/base_resource.rb', line 264
def underscore_name
return @name if @name.present?
self.class.name.demodulize.underscore
end
|