Class: Avo::BaseResource
Constant Summary
collapse
- VIEW_METHODS_MAPPING =
{
index: [:index_fields, :display_fields],
show: [:show_fields, :display_fields],
edit: [:edit_fields, :form_fields],
update: [:edit_fields, :form_fields],
new: [:new_fields, :form_fields],
create: [:new_fields, :form_fields]
}
Instance Attribute Summary collapse
#items_holder
Class Method Summary
collapse
-
.action(action_class, arguments: {}) ⇒ Object
-
.authorization ⇒ Object
-
.class_name ⇒ Object
-
.fetch_search(key, record: nil) ⇒ Object
-
.filter(filter_class, arguments: {}) ⇒ Object
-
.find_record(id, query: nil, params: nil) ⇒ Object
-
.find_scope ⇒ Object
This resolves the scope when finding records (not “where” queries).
-
.get_available_models ⇒ Object
-
.get_model_by_name(model_name) ⇒ Object
-
.get_record_associations(record) ⇒ Object
-
.model_class(record_class: nil) ⇒ Object
Returns the model class being used for this resource.
-
.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.
-
.name ⇒ Object
(also: singular_name)
-
.name_from_translation_key(count:, default:) ⇒ Object
Get the name from the translation_key and fallback to default It can raise I18n::InvalidPluralizationData when using only resource_translation without pluralization keys like: one, two or other key Example: — en: avo: resource_translations: product: save: “Save product”.
-
.navigation_label ⇒ Object
-
.plural_name ⇒ Object
-
.query_scope ⇒ Object
This resolves the scope when doing “where” queries (not find queries).
-
.route_key ⇒ Object
-
.scope(scope_class) ⇒ Object
-
.search_query ⇒ Object
-
.singular_route_key ⇒ Object
-
.translation_key ⇒ Object
-
.underscore_name ⇒ Object
-
.valid_association_name(record, association_name) ⇒ Object
-
.valid_attachment_name(record, association_name) ⇒ Object
Instance Method Summary
collapse
#apply_pagination, #pagination_type
#helpers
#description
#add_stimulus_attributes_for, #get_stimulus_controllers, #stimulus_data_attributes
#render_edit_controls, #render_index_controls, #render_row_controls, #render_show_controls
#with_new_items
#fields, #get_field, #get_field_definitions, #get_fields, #get_items, #get_preview_fields, #invalid_fields, #is_empty?, #items, #only_fields, #tab_groups, #visible_items
Constructor Details
#initialize(record: nil, view: nil, user: nil, params: nil) ⇒ BaseResource
Returns a new instance of BaseResource.
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/avo/base_resource.rb', line 243
def initialize(record: nil, view: nil, user: nil, params: nil)
@view = Avo::ViewInquirer.new(view) if view.present?
@user = user if user.present?
@params = params if params.present?
if record.present?
@record = record
hydrate_model_with_default_values if @view&.new?
end
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
#record ⇒ Object
Returns the value of attribute record.
43
44
45
|
# File 'lib/avo/base_resource.rb', line 43
def record
@record
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
41
42
43
|
# File 'lib/avo/base_resource.rb', line 41
def reflection
@reflection
end
|
#user ⇒ Object
Returns the value of attribute user.
42
43
44
|
# File 'lib/avo/base_resource.rb', line 42
def user
@user
end
|
#view ⇒ Object
Returns the value of attribute view.
40
41
42
|
# File 'lib/avo/base_resource.rb', line 40
def view
@view
end
|
Class Method Details
.action(action_class, arguments: {}) ⇒ Object
80
81
82
|
# File 'lib/avo/base_resource.rb', line 80
def action(action_class, arguments: {})
deprecated_dsl_api __method__, "actions"
end
|
.class_name ⇒ Object
164
165
166
|
# File 'lib/avo/base_resource.rb', line 164
def class_name
to_s.demodulize
end
|
.fetch_search(key, record: nil) ⇒ Object
227
228
229
230
|
# File 'lib/avo/base_resource.rb', line 227
def fetch_search(key, record: nil)
Avo::ExecutionContext.new(target: search[key], resource: self, record: record).handle
end
|
.filter(filter_class, arguments: {}) ⇒ Object
84
85
86
|
# File 'lib/avo/base_resource.rb', line 84
def filter(filter_class, arguments: {})
deprecated_dsl_api __method__, "filters"
end
|
.find_record(id, query: nil, params: nil) ⇒ Object
214
215
216
217
218
219
220
221
|
# File 'lib/avo/base_resource.rb', line 214
def find_record(id, query: nil, params: nil)
Avo::ExecutionContext.new(
target: find_record_method,
query: query || find_scope, id: id,
params: params
).handle
end
|
.find_scope ⇒ Object
This resolves the scope when finding records (not “where” queries)
It’s used to apply the authorization feature.
105
106
107
|
# File 'lib/avo/base_resource.rb', line 105
def find_scope
authorization.apply_policy model_class
end
|
.get_available_models ⇒ Object
131
132
133
|
# File 'lib/avo/base_resource.rb', line 131
def get_available_models
ApplicationRecord.descendants
end
|
.get_model_by_name(model_name) ⇒ Object
135
136
137
138
139
|
# File 'lib/avo/base_resource.rb', line 135
def get_model_by_name(model_name)
get_available_models.find do |m|
m.to_s == model_name.to_s
end
end
|
.get_record_associations(record) ⇒ Object
113
114
115
|
# File 'lib/avo/base_resource.rb', line 113
def get_record_associations(record)
record._reflections
end
|
.model_class(record_class: nil) ⇒ Object
Returns the model class being used for this resource.
The Resource instance has a model_class method too so it can support the STI use cases where we figure out the model class from the record
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/avo/base_resource.rb', line 145
def model_class(record_class: nil)
return @model_class if @model_class.present?
return record_class if record_class.present?
class_name.safe_constantize
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
160
161
162
|
# File 'lib/avo/base_resource.rb', line 160
def model_key
model_class.model_name.plural
end
|
.name ⇒ Object
Also known as:
singular_name
180
181
182
|
# File 'lib/avo/base_resource.rb', line 180
def name
name_from_translation_key(count: 1, default: class_name.underscore.humanize)
end
|
.name_from_translation_key(count:, default:) ⇒ Object
Get the name from the translation_key and fallback to default It can raise I18n::InvalidPluralizationData when using only resource_translation without pluralization keys like: one, two or other key Example:
en:
avo:
resource_translations:
product:
save: "Save product"
198
199
200
201
202
|
# File 'lib/avo/base_resource.rb', line 198
def name_from_translation_key(count:, default:)
t(translation_key, count:, default:).humanize
rescue I18n::InvalidPluralizationData
default
end
|
.navigation_label ⇒ Object
210
211
212
|
# File 'lib/avo/base_resource.rb', line 210
def navigation_label
plural_name.humanize
end
|
.plural_name ⇒ Object
185
186
187
|
# File 'lib/avo/base_resource.rb', line 185
def plural_name
name_from_translation_key(count: 2, default: name.pluralize)
end
|
.query_scope ⇒ Object
This resolves the scope when doing “where” queries (not find queries)
It’s used to apply the authorization feature.
95
96
97
98
99
100
|
# File 'lib/avo/base_resource.rb', line 95
def query_scope
authorization.apply_policy Avo::ExecutionContext.new(
target: index_query,
query: model_class
).handle
end
|
.route_key ⇒ Object
168
169
170
|
# File 'lib/avo/base_resource.rb', line 168
def route_key
class_name.underscore.pluralize
end
|
.scope(scope_class) ⇒ Object
88
89
90
|
# File 'lib/avo/base_resource.rb', line 88
def scope(scope_class)
deprecated_dsl_api __method__, "scopes"
end
|
.search_query ⇒ Object
223
224
225
|
# File 'lib/avo/base_resource.rb', line 223
def search_query
search.dig(:query)
end
|
.singular_route_key ⇒ Object
172
173
174
|
# File 'lib/avo/base_resource.rb', line 172
def singular_route_key
route_key.singularize
end
|
.translation_key ⇒ Object
176
177
178
|
# File 'lib/avo/base_resource.rb', line 176
def translation_key
@translation_key || "avo.resource_translations.#{class_name.underscore}"
end
|
.underscore_name ⇒ Object
204
205
206
207
208
|
# File 'lib/avo/base_resource.rb', line 204
def underscore_name
return @name if @name.present?
name.demodulize.underscore
end
|
.valid_association_name(record, association_name) ⇒ Object
117
118
119
120
121
|
# File 'lib/avo/base_resource.rb', line 117
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
123
124
125
126
127
128
129
|
# File 'lib/avo/base_resource.rb', line 123
def valid_attachment_name(record, association_name)
association_exists = get_record_associations(record).keys.any? do |name|
name == "#{association_name}_attachment" || name == "#{association_name}_attachments"
end
association_name if association_exists
end
|
Instance Method Details
#attachment_fields ⇒ Object
414
415
416
417
418
|
# File 'lib/avo/base_resource.rb', line 414
def attachment_fields
get_field_definitions.select do |field|
[Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
end
end
|
#authorization(user: nil) ⇒ Object
451
452
453
454
|
# File 'lib/avo/base_resource.rb', line 451
def authorization(user: nil)
current_user = user || Avo::Current.user
Avo::Services::AuthorizationService.new(current_user, record || model_class, policy_class: authorization_policy)
end
|
#available_view_types ⇒ Object
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
# File 'lib/avo/base_resource.rb', line 395
def available_view_types
if self.class.view_types.present?
return Array(
Avo::ExecutionContext.new(
target: self.class.view_types,
resource: self,
record: record
).handle
)
end
view_types = [:table]
view_types << :grid if self.class.grid_view.present?
view_types << :map if map_view.present?
view_types
end
|
#avatar ⇒ Object
546
547
548
549
550
551
552
553
554
|
# File 'lib/avo/base_resource.rb', line 546
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
538
539
540
541
542
543
544
|
# File 'lib/avo/base_resource.rb', line 538
def avatar_field
get_field_definitions.find do |field|
field.as_avatar.present?
end
rescue
nil
end
|
#avatar_type ⇒ Object
556
557
558
559
560
|
# File 'lib/avo/base_resource.rb', line 556
def avatar_type
avatar_field.as_avatar
rescue
nil
end
|
#cache_hash(parent_record) ⇒ Object
474
475
476
477
478
479
480
481
482
|
# File 'lib/avo/base_resource.rb', line 474
def cache_hash(parent_record)
result = [record, file_hash]
if parent_record.present?
result << parent_record
end
result
end
|
#current_user ⇒ Object
18
19
20
|
# File 'lib/avo/base_resource.rb', line 18
def current_user
Avo::Current.user
end
|
#default_panel_name ⇒ Object
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/avo/base_resource.rb', line 357
def default_panel_name
return @params[:related_name].capitalize if @params.present? && @params[:related_name].present?
case @view.to_sym
when :show
record_title
when :edit
record_title
when :new
t("avo.create_new_item", item: name.humanize(capitalize: false)).upcase_first
end
end
|
#description_attributes ⇒ Object
578
579
580
581
582
583
584
|
# File 'lib/avo/base_resource.rb', line 578
def description_attributes
{
view: view,
resource: self,
record: record
}
end
|
#detect_fields ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/avo/base_resource.rb', line 261
def detect_fields
self.items_holder = Avo::Resources::Items::Holder.new(parent: self)
if temporary_items.present?
instance_eval(&temporary_items)
else
fetch_fields
end
self
end
|
#divider ⇒ Object
298
299
300
|
# File 'lib/avo/base_resource.rb', line 298
def divider
action DividerComponent
end
|
#entity_loader(entity) ⇒ Object
586
587
588
|
# File 'lib/avo/base_resource.rb', line 586
def entity_loader(entity)
instance_variable_get("@#{entity.to_s.pluralize}_loader")
end
|
#fetch_cards ⇒ Object
294
295
296
|
# File 'lib/avo/base_resource.rb', line 294
def fetch_cards
cards
end
|
#fetch_fields ⇒ Object
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/avo/base_resource.rb', line 283
def fetch_fields
possible_methods_for_view = VIEW_METHODS_MAPPING[view.to_sym]
possible_methods_for_view&.each do |method_for_view|
return send(method_for_view) if respond_to?(method_for_view)
end
fields
end
|
#fields_by_database_id ⇒ Object
Map the received params to their actual fields
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/avo/base_resource.rb', line 421
def 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
end
|
#file_hash ⇒ Object
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
# File 'lib/avo/base_resource.rb', line 456
def file_hash
content_to_be_hashed = ""
file_name = self.class.underscore_name.tr(" ", "_")
resource_path = Rails.root.join("app", "avo", "resources", "#{file_name}.rb").to_s
if File.file? resource_path
content_to_be_hashed += File.read(resource_path)
end
policy_path = Rails.root.join("app", "policies", "#{file_name.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_record(record, params, extra_params: []) ⇒ Object
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/avo/base_resource.rb', line 432
def fill_record(record, params, extra_params: [])
params.each do |key, value|
field = fields_by_database_id[key]
next unless field.present?
record = field.fill_field record, key, value, params
end
if .present?
record.assign_attributes params.permit()
end
record
end
|
562
563
564
|
# File 'lib/avo/base_resource.rb', line 562
def form_scope
model_class.base_class.to_s.underscore.downcase
end
|
#has_record_id? ⇒ Boolean
566
567
568
|
# File 'lib/avo/base_resource.rb', line 566
def has_record_id?
record.present? && record_id.present?
end
|
#hydrate ⇒ Object
347
348
349
350
351
352
353
354
355
|
# File 'lib/avo/base_resource.rb', line 347
def hydrate(...)
super(...)
if @record.present?
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 record.
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
# File 'lib/avo/base_resource.rb', line 485
def hydrate_model_with_default_values
default_values = get_fields
.select do |field|
!field.computed && !field.is_a?(Avo::Fields::HeadingField)
end
.map do |field|
value = field.value
if field.type == "belongs_to"
reflection = @record._reflections[@params[:via_relation]]
if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
via_resource = Avo.resource_manager.get_resource_by_model_class(@params[:via_relation_class])
value = via_resource.find_record(@params[:via_record_id])
elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s
resource = Avo.resource_manager.get_resource_by_model_class params[:via_relation_class]
record = resource.find_record @params[:via_record_id], params: params
id_param = reflection.options[:primary_key] || :id
value = record.send(id_param)
end
end
[field, value]
end
.to_h
.select do |_, value|
value.present?
end
default_values.each do |field, value|
field.assign_value record: @record, value: value
end
end
|
#id_attribute ⇒ Object
570
571
572
|
# File 'lib/avo/base_resource.rb', line 570
def id_attribute
:id
end
|
#model_class ⇒ Object
Returns the model class being used for this resource.
We use the class method as a fallback but we pass it the record too so it can support the STI use cases where we figure out the model class from that record.
374
375
376
377
378
|
# File 'lib/avo/base_resource.rb', line 374
def model_class
record_class = @record&.class
self.class.model_class record_class: record_class
end
|
#model_name ⇒ Object
522
523
524
|
# File 'lib/avo/base_resource.rb', line 522
def model_name
model_class.model_name
end
|
#record_id ⇒ Object
574
575
576
|
# File 'lib/avo/base_resource.rb', line 574
def record_id
record.send(id_attribute)
end
|
#record_path ⇒ Object
530
531
532
|
# File 'lib/avo/base_resource.rb', line 530
def record_path
resource_path(record: record, resource: self)
end
|
#record_title ⇒ Object
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/avo/base_resource.rb', line 380
def record_title
return name if @record.nil?
return @record.try(:name) || @record.try(:title) || @record.try(:label) || @record.id if title.nil?
case title
when Symbol
@record.send title
when Proc
Avo::ExecutionContext.new(target: title, resource: self, record: @record).handle
end
end
|
#records_path ⇒ Object
534
535
536
|
# File 'lib/avo/base_resource.rb', line 534
def records_path
resources_path(resource: self)
end
|
#singular_model_key ⇒ Object
526
527
528
|
# File 'lib/avo/base_resource.rb', line 526
def singular_model_key
model_class.model_name.singular
end
|