Class: Avo::Resources::Base
- Inherits:
-
Object
- Object
- Avo::Resources::Base
show all
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- ActionView::Helpers::UrlHelper, Concerns::AbstractResource, Concerns::CanReplaceItems, Concerns::HasAvatar, Concerns::HasControls, Concerns::HasCover, Concerns::HasDescription, Concerns::HasDiscreetInformation, Concerns::HasFieldDiscovery, Concerns::HasHelpers, Concerns::HasItems, Concerns::HasResourceStimulusControllers, Concerns::Hydration, Concerns::ModelClassConstantized, Concerns::Pagination, Concerns::RowControlsConfiguration, Concerns::SafeCall
- Defined in:
- lib/avo/resources/base.rb
Constant Summary
collapse
- VIEW_METHODS_MAPPING =
{
index: [:index, :display],
show: [:show, :display],
edit: [:edit, :form],
update: [:edit, :form],
new: [:new, :form],
create: [:new, :form]
}
Concerns::HasFieldDiscovery::COLUMN_NAMES_TO_IGNORE
Instance Attribute Summary collapse
#items_holder
Class Method Summary
collapse
Instance Method Summary
collapse
#safe_call
#controls_placement, #render_row_controls_on_the_left?, #render_row_controls_on_the_right?, #row_controls_classes, #row_controls_configurations
#discreet_information
#apply_pagination, #pagination_type
#helpers
#avatar, #initials
#cover
#description, #discreet_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
#discover_associations, #discover_columns, #model_db_columns
Constructor Details
#initialize(record: nil, view: nil, user: nil, params: nil) ⇒ Base
Returns a new instance of Base.
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'lib/avo/resources/base.rb', line 275
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.presence || {}
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.
53
54
55
|
# File 'lib/avo/resources/base.rb', line 53
def record
@record
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
51
52
53
|
# File 'lib/avo/resources/base.rb', line 51
def reflection
@reflection
end
|
#user ⇒ Object
Returns the value of attribute user.
52
53
54
|
# File 'lib/avo/resources/base.rb', line 52
def user
@user
end
|
#view ⇒ Object
Returns the value of attribute view.
50
51
52
|
# File 'lib/avo/resources/base.rb', line 50
def view
@view
end
|
Class Method Details
.class_name ⇒ Object
175
176
177
|
# File 'lib/avo/resources/base.rb', line 175
def class_name
@class_name ||= to_s.demodulize
end
|
.fetch_search(key, record: nil) ⇒ Object
258
259
260
261
|
# File 'lib/avo/resources/base.rb', line 258
def fetch_search(key, record: nil)
Avo::ExecutionContext.new(target: search[key], resource: self, record: record).handle
end
|
.find_record(id, query: nil, params: nil) ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/avo/resources/base.rb', line 228
def find_record(id, query: nil, params: nil)
query ||= find_scope
if single_includes.present?
query = query.includes(*single_includes)
end
if single_attachments.present?
single_attachments.each do |attachment|
query = query.send(:"with_attached_#{attachment}")
end
end
Avo::ExecutionContext.new(
target: find_record_method,
query: query,
id: id,
params: params,
model_class:
).handle
end
|
.find_scope ⇒ Object
This resolves the scope when finding records (not “where” queries)
It’s used to apply the authorization feature.
126
127
128
|
# File 'lib/avo/resources/base.rb', line 126
def find_scope
authorization.apply_policy model_class
end
|
.get_available_models ⇒ Object
142
143
144
|
# File 'lib/avo/resources/base.rb', line 142
def get_available_models
ApplicationRecord.descendants
end
|
.get_model_by_name(model_name) ⇒ Object
146
147
148
149
150
|
# File 'lib/avo/resources/base.rb', line 146
def get_model_by_name(model_name)
get_available_models.find do |m|
m.to_s == model_name.to_s
end
end
|
.initials ⇒ Object
197
198
199
|
# File 'lib/avo/resources/base.rb', line 197
def initials
name.to_s.split(" ").map(&:first).join("").first(2).upcase
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
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/avo/resources/base.rb', line 156
def model_class(record_class: nil)
return constantized_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
171
172
173
|
# File 'lib/avo/resources/base.rb', line 171
def model_key
@model_key ||= model_class.model_name.plural
end
|
.name ⇒ Object
Also known as:
singular_name
192
193
194
|
# File 'lib/avo/resources/base.rb', line 192
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"
214
215
216
217
218
|
# File 'lib/avo/resources/base.rb', line 214
def name_from_translation_key(count:, default:)
t(translation_key, count:, default:).humanize
rescue I18n::InvalidPluralizationData
default
end
|
.navigation_label ⇒ Object
224
225
226
|
# File 'lib/avo/resources/base.rb', line 224
def navigation_label
plural_name.humanize
end
|
.plural_name ⇒ Object
201
202
203
|
# File 'lib/avo/resources/base.rb', line 201
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.
116
117
118
119
120
121
|
# File 'lib/avo/resources/base.rb', line 116
def query_scope
authorization.apply_policy Avo::ExecutionContext.new(
target: index_query,
query: model_class
).handle
end
|
.route_key ⇒ Object
179
180
181
|
# File 'lib/avo/resources/base.rb', line 179
def route_key
class_name.underscore.pluralize
end
|
.search_query ⇒ Object
250
251
252
|
# File 'lib/avo/resources/base.rb', line 250
def search_query
search.dig(:query)
end
|
.search_results_count ⇒ Object
254
255
256
|
# File 'lib/avo/resources/base.rb', line 254
def search_results_count
search.dig(:results_count)
end
|
.singular_route_key ⇒ Object
183
184
185
|
# File 'lib/avo/resources/base.rb', line 183
def singular_route_key
route_key.singularize
end
|
.translation_key ⇒ Object
187
188
189
|
# File 'lib/avo/resources/base.rb', line 187
def translation_key
custom_translation_key || "avo.resource_translations.#{class_name.underscore}"
end
|
.underscore_name ⇒ Object
220
221
222
|
# File 'lib/avo/resources/base.rb', line 220
def underscore_name
name.demodulize.underscore
end
|
.valid_association_name(record, association_name) ⇒ Object
134
135
136
|
# File 'lib/avo/resources/base.rb', line 134
def valid_association_name(record, association_name)
association_name if record.class.reflect_on_association(association_name).present?
end
|
.valid_attachment_name(record, association_name) ⇒ Object
138
139
140
|
# File 'lib/avo/resources/base.rb', line 138
def valid_attachment_name(record, association_name)
association_name if record.class.reflect_on_attachment(association_name).present?
end
|
Instance Method Details
#attachment_fields ⇒ Object
487
488
489
490
491
|
# File 'lib/avo/resources/base.rb', line 487
def attachment_fields
get_field_definitions.select do |field|
[Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
end
end
|
#authorization(user: nil) ⇒ Object
533
534
535
536
|
# File 'lib/avo/resources/base.rb', line 533
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
# File 'lib/avo/resources/base.rb', line 466
def available_view_types
@available_view_types ||= begin
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
end
|
#cache_hash(parent_record) ⇒ Object
559
560
561
562
563
564
565
566
567
|
# File 'lib/avo/resources/base.rb', line 559
def cache_hash(parent_record)
result = [record, file_hash]
if parent_record.present?
result << parent_record
end
result
end
|
#current_user ⇒ Object
28
29
30
|
# File 'lib/avo/resources/base.rb', line 28
def current_user
Avo::Current.user
end
|
#custom_components ⇒ Object
655
656
657
658
659
660
661
662
|
# File 'lib/avo/resources/base.rb', line 655
def custom_components
@custom_components ||= Avo::ExecutionContext.new(
target: components,
resource: self,
record: @record,
view: @view
).handle.with_indifferent_access
end
|
#default_panel_name ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
|
# File 'lib/avo/resources/base.rb', line 405
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
639
640
641
642
643
644
645
|
# File 'lib/avo/resources/base.rb', line 639
def description_attributes
{
view: view,
resource: self,
record: record
}
end
|
#detect_fields ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/avo/resources/base.rb', line 293
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(**kwargs) ⇒ Object
346
347
348
|
# File 'lib/avo/resources/base.rb', line 346
def divider(**kwargs)
entity_loader(:action).use({class: Divider, **kwargs}.compact)
end
|
#entity_loader(entity) ⇒ Object
647
648
649
|
# File 'lib/avo/resources/base.rb', line 647
def entity_loader(entity)
instance_variable_get(:"@#{entity.to_s.pluralize}_loader")
end
|
#fetch_cards ⇒ Object
336
337
338
339
340
341
342
343
344
|
# File 'lib/avo/resources/base.rb', line 336
def fetch_cards
possible_methods_for_view = VIEW_METHODS_MAPPING[view.to_sym]
possible_methods_for_view&.each do |method_for_view|
return send(:"#{method_for_view}_cards") if respond_to?(:"#{method_for_view}_cards")
end
cards
end
|
#fetch_fields ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/avo/resources/base.rb', line 317
def fetch_fields
if view.preview?
[:fields, :index_fields, :show_fields, :display_fields].each do |fields_method|
send(fields_method) if respond_to?(fields_method)
end
return
end
possible_methods_for_view = VIEW_METHODS_MAPPING[view.to_sym]
possible_methods_for_view&.each do |method_for_view|
return send(:"#{method_for_view}_fields") if respond_to?(:"#{method_for_view}_fields")
end
fields
end
|
#fetch_record_icon ⇒ Object
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
# File 'lib/avo/resources/base.rb', line 451
def fetch_record_icon
return icon if @record.nil?
return @record.try(:icon) if icon.nil?
case icon
when Symbol
@record.send icon
when Proc
Avo::ExecutionContext.new(target: icon, resource: self, record: @record).handle
end
end
|
#fetch_record_title ⇒ Object
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
# File 'lib/avo/resources/base.rb', line 432
def fetch_record_title
return name if @record.nil?
return @record.try(:name) || @record.try(:title) || @record.try(:label) || @record.to_param if title.nil?
case title
when Symbol
@record.send title
when Proc
Avo::ExecutionContext.new(target: title, resource: self, record: @record).handle
end
end
|
#fields_by_database_id(resource: self) ⇒ Object
Map the received params to their actual fields ‘resource’ argument is used on avo-advanced, don’t remove
495
496
497
498
499
500
501
502
503
504
|
# File 'lib/avo/resources/base.rb', line 495
def fields_by_database_id(resource: self)
resource.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
# File 'lib/avo/resources/base.rb', line 538
def file_hash
content_to_be_hashed = ""
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
|
#file_name ⇒ Object
555
556
557
|
# File 'lib/avo/resources/base.rb', line 555
def file_name
@file_name ||= self.class.underscore_name.tr(" ", "_")
end
|
#fill_record(record, permitted_params, extra_params: [], fields: nil) ⇒ Object
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
# File 'lib/avo/resources/base.rb', line 506
def fill_record(record, permitted_params, extra_params: [], fields: nil)
permitted_params.each do |key, value|
field = if fields.present?
fields.find { |f| f.id == key.to_sym }
else
fields_by_database_id[key]
end
next unless field.present?
record = field.fill_field record, key, value, permitted_params
end
if .present?
= permitted_params.slice(*flatten_keys())
record.assign_attributes
end
safe_call(:fill_nested_records, record:, permitted_params:) || record
end
|
623
624
625
|
# File 'lib/avo/resources/base.rb', line 623
def form_scope
model_class.base_class.to_s.underscore.downcase
end
|
#get_external_link ⇒ Object
668
669
670
671
672
|
# File 'lib/avo/resources/base.rb', line 668
def get_external_link
return unless record&.persisted?
Avo::ExecutionContext.new(target: external_link, resource: self, record: record).handle
end
|
#has_record_id? ⇒ Boolean
627
628
629
|
# File 'lib/avo/resources/base.rb', line 627
def has_record_id?
record.present? && record_id.present?
end
|
#hydrate ⇒ Object
395
396
397
398
399
400
401
402
403
|
# File 'lib/avo/resources/base.rb', line 395
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.
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
# File 'lib/avo/resources/base.rb', line 570
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.class.reflect_on_association(@params[:via_relation]) if @params[:via_relation].present?
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
631
632
633
|
# File 'lib/avo/resources/base.rb', line 631
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.
422
423
424
425
426
|
# File 'lib/avo/resources/base.rb', line 422
def model_class
record_class = @record&.class
self.class.model_class record_class: record_class
end
|
#model_name ⇒ Object
607
608
609
|
# File 'lib/avo/resources/base.rb', line 607
def model_name
model_class.model_name
end
|
#record_icon ⇒ Object
447
448
449
|
# File 'lib/avo/resources/base.rb', line 447
def record_icon
fetch_record_icon.to_s
end
|
#record_id ⇒ Object
635
636
637
|
# File 'lib/avo/resources/base.rb', line 635
def record_id
record.send(id_attribute)
end
|
#record_param ⇒ Object
651
652
653
|
# File 'lib/avo/resources/base.rb', line 651
def record_param
@record_param ||= @record.persisted? ? @record.to_param : nil
end
|
#record_path ⇒ Object
615
616
617
|
# File 'lib/avo/resources/base.rb', line 615
def record_path
resource_path(record: record, resource: self)
end
|
#record_title ⇒ Object
428
429
430
|
# File 'lib/avo/resources/base.rb', line 428
def record_title
fetch_record_title.to_s
end
|
#records_path ⇒ Object
619
620
621
|
# File 'lib/avo/resources/base.rb', line 619
def records_path
resources_path(resource: self)
end
|
#resolve_component(original_component) ⇒ Object
664
665
666
|
# File 'lib/avo/resources/base.rb', line 664
def resolve_component(original_component)
custom_components.dig(original_component.to_s)&.to_s&.safe_constantize || original_component
end
|
#resource_type_array? ⇒ Boolean
674
|
# File 'lib/avo/resources/base.rb', line 674
def resource_type_array? = false
|
#singular_model_key ⇒ Object
611
612
613
|
# File 'lib/avo/resources/base.rb', line 611
def singular_model_key
model_class.model_name.singular
end
|
#sort_by_param ⇒ Object
676
677
678
679
680
681
682
683
684
|
# File 'lib/avo/resources/base.rb', line 676
def sort_by_param
available_columns = model_class.column_names
if available_columns.include?(default_sort_column.to_s)
default_sort_column
elsif available_columns.include?("created_at")
:created_at
end
end
|
#sorting_supported? ⇒ Boolean
686
|
# File 'lib/avo/resources/base.rb', line 686
def sorting_supported? = true
|