Class: Avo::Resources::Base

Direct Known Subclasses

BaseResource, ArrayResource

Constant Summary collapse

VIEW_METHODS_MAPPING =
{
  index: [:index, :display],
  show: [:show, :display],
  edit: [:edit, :form],
  update: [:edit, :form],
  new: [:new, :form],
  create: [:new, :form]
}

Constants included from Concerns::HasFieldDiscovery

Concerns::HasFieldDiscovery::COLUMN_NAMES_TO_IGNORE

Instance Attribute Summary collapse

Attributes included from Concerns::HasItems

#items_holder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::SafeCall

#safe_call

Methods included from Concerns::RowControlsConfiguration

#controls_placement, #render_row_controls_on_the_left?, #render_row_controls_on_the_right?, #row_controls_classes, #row_controls_configurations

Methods included from Concerns::HasDiscreetInformation

#discreet_information

Methods included from Concerns::Pagination

#apply_pagination, #pagination_type

Methods included from Concerns::HasHelpers

#helpers

Methods included from Concerns::HasAvatar

#avatar, #initials

Methods included from Concerns::HasCover

#cover

Methods included from Concerns::HasDescription

#description, #discreet_description

Methods included from Concerns::HasResourceStimulusControllers

#add_stimulus_attributes_for, #get_stimulus_controllers, #stimulus_data_attributes

Methods included from Concerns::HasControls

#render_edit_controls, #render_index_controls, #render_row_controls, #render_show_controls

Methods included from Concerns::CanReplaceItems

#with_new_items

Methods included from Concerns::HasItems

#fields, #get_field, #get_field_definitions, #get_fields, #get_preview_fields, #invalid_fields, #is_empty?, #items, #items_with_standalone_fields_wrapped_in_cards, #only_fields, #tab_groups, #visible_items

Methods included from Concerns::HasFieldDiscovery

#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.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/avo/resources/base.rb', line 314

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

#recordObject

Returns the value of attribute record.



54
55
56
# File 'lib/avo/resources/base.rb', line 54

def record
  @record
end

#reflectionObject

Returns the value of attribute reflection.



52
53
54
# File 'lib/avo/resources/base.rb', line 52

def reflection
  @reflection
end

#userObject

Returns the value of attribute user.



53
54
55
# File 'lib/avo/resources/base.rb', line 53

def user
  @user
end

#viewObject

Returns the value of attribute view.



51
52
53
# File 'lib/avo/resources/base.rb', line 51

def view
  @view
end

Class Method Details

.authorizationObject



137
138
139
# File 'lib/avo/resources/base.rb', line 137

def authorization
  Avo::Services::AuthorizationService.new Avo::Current.user, model_class, policy_class: authorization_policy
end

.class_nameObject



182
183
184
# File 'lib/avo/resources/base.rb', line 182

def class_name
  @class_name ||= to_s.delete_prefix("Avo::Resources::")
end

.controller_pathObject

Same as route_path — resolves to the correct namespaced controller inside isolate_namespace



209
210
211
# File 'lib/avo/resources/base.rb', line 209

def controller_path
  route_path
end

.demodulized_class_nameObject

Last segment only — used for display, translation keys, and initials



187
188
189
# File 'lib/avo/resources/base.rb', line 187

def demodulized_class_name
  class_name.demodulize
end

.fetch_search(key, record: nil) ⇒ Object



289
290
291
292
# File 'lib/avo/resources/base.rb', line 289

def fetch_search(key, record: nil)
  # self.class.fetch_search
  Avo::ExecutionContext.new(target: search[key], resource: self, record: record).handle
end

.find_record(id, query: nil, params: nil) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/avo/resources/base.rb', line 263

def find_record(id, query: nil, params: nil)
  query ||= find_scope # If no record is given we'll use the default

  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_scopeObject

This resolves the scope when finding records (not "where" queries)

It's used to apply the authorization feature.



133
134
135
# File 'lib/avo/resources/base.rb', line 133

def find_scope
  authorization.apply_policy model_class
end

.get_available_modelsObject



149
150
151
# File 'lib/avo/resources/base.rb', line 149

def get_available_models
  ApplicationRecord.descendants
end

.get_model_by_name(model_name) ⇒ Object



153
154
155
156
157
# File 'lib/avo/resources/base.rb', line 153

def get_model_by_name(model_name)
  get_available_models.find do |m|
    m.to_s == model_name.to_s
  end
end

.initialsObject



223
224
225
# File 'lib/avo/resources/base.rb', line 223

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



163
164
165
166
167
168
169
170
171
172
# File 'lib/avo/resources/base.rb', line 163

def model_class(record_class: nil)
  # get the model class off of the static property
  return constantized_model_class if @model_class.present?

  # get the model class off of the record for STI models
  return record_class if record_class.present?

  # generate a model class
  class_name.safe_constantize
end

.model_keyObject

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



178
179
180
# File 'lib/avo/resources/base.rb', line 178

def model_key
  @model_key ||= model_class.model_name.plural
end

.nameObject Also known as: singular_name



218
219
220
# File 'lib/avo/resources/base.rb', line 218

def name
  name_from_translation_key(count: 1, default: demodulized_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"


240
241
242
243
244
# File 'lib/avo/resources/base.rb', line 240

def name_from_translation_key(count:, default:)
  t(translation_key, count:, default:)
rescue I18n::InvalidPluralizationData
  default
end


259
260
261
# File 'lib/avo/resources/base.rb', line 259

def navigation_label
  plural_name
end

.plural_nameObject



227
228
229
# File 'lib/avo/resources/base.rb', line 227

def plural_name
  name_from_translation_key(count: 2, default: name.pluralize)
end

.query_scopeObject

This resolves the scope when doing "where" queries (not find queries)

It's used to apply the authorization feature.



123
124
125
126
127
128
# File 'lib/avo/resources/base.rb', line 123

def query_scope
  authorization.apply_policy Avo::ExecutionContext.new(
    target: index_query,
    query: model_class
  ).handle
end

.route_keyObject

Underscore-joined Rails resource identifier: "accounts_invoices", "users"



200
201
202
# File 'lib/avo/resources/base.rb', line 200

def route_key
  route_path.tr("/", "_")
end

.route_pathObject

Slash-separated URL slug: "accounts/invoices", "users"



192
193
194
195
196
197
# File 'lib/avo/resources/base.rb', line 192

def route_path
  parts = class_name.split("::")
  parts.map!(&:underscore)
  parts[-1] = parts[-1].pluralize
  parts.join("/")
end

.search_queryObject



285
286
287
# File 'lib/avo/resources/base.rb', line 285

def search_query
  search.dig(:query)
end

.sentence_nameObject

The name as it should read inside a sentence, e.g. "Create new payment method". A resolved translation is used verbatim; only the generated fallback is lowercased.



248
249
250
251
252
253
# File 'lib/avo/resources/base.rb', line 248

def sentence_name
  name_from_translation_key(
    count: 1,
    default: demodulized_class_name.underscore.humanize(capitalize: false)
  )
end

.singular_route_keyObject



204
205
206
# File 'lib/avo/resources/base.rb', line 204

def singular_route_key
  route_key.singularize
end

.translation_keyObject



213
214
215
# File 'lib/avo/resources/base.rb', line 213

def translation_key
  custom_translation_key || "avo.resource_translations.#{class_name.underscore}"
end

.underscore_nameObject



255
256
257
# File 'lib/avo/resources/base.rb', line 255

def underscore_name
  name.demodulize.underscore
end

.valid_association_name(record, association_name) ⇒ Object



141
142
143
# File 'lib/avo/resources/base.rb', line 141

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



145
146
147
# File 'lib/avo/resources/base.rb', line 145

def valid_attachment_name(record, association_name)
  association_name if record.class.reflect_on_attachment(association_name).present?
end

Instance Method Details

#attachment_fieldsObject



571
572
573
574
575
# File 'lib/avo/resources/base.rb', line 571

def attachment_fields
  get_field_definitions.select do |field|
    [Avo::Fields::FileField, Avo::Fields::FilesField].include? field.class
  end
end

#authorization(user: nil) ⇒ Object



617
618
619
620
# File 'lib/avo/resources/base.rb', line 617

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_typesObject



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/avo/resources/base.rb', line 550

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



644
645
646
647
648
649
650
651
652
# File 'lib/avo/resources/base.rb', line 644

def cache_hash(parent_record)
  result = [record, file_hash]

  if parent_record.present?
    result << parent_record
  end

  result
end

#current_userObject



28
29
30
# File 'lib/avo/resources/base.rb', line 28

def current_user
  Avo::Current.user
end

#custom_componentsObject



740
741
742
743
744
745
746
747
# File 'lib/avo/resources/base.rb', line 740

def custom_components
  @custom_components ||= Avo::ExecutionContext.new(
    target: components,
    resource: self,
    record: @record,
    view: @view
  ).handle.with_indifferent_access
end

#default_panel_nameObject



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/avo/resources/base.rb', line 486

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
    # upcase_first, not humanize: locales like de put the item first
    # ("%{item} hinzufügen"), so the sentence still needs its initial cased
    # while the interpolated name stays exactly as translated.
    t("avo.create_new_item", item: self.class.sentence_name).upcase_first
  end
end

#description_attributesObject



724
725
726
727
728
729
730
# File 'lib/avo/resources/base.rb', line 724

def description_attributes
  {
    view: view,
    resource: self,
    record: record
  }
end

#detect_fieldsObject



332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/avo/resources/base.rb', line 332

def detect_fields
  self.items_holder = Avo::Resources::Items::Holder.new(parent: self)

  # Used in testing to replace items
  if temporary_items.present?
    instance_eval(&temporary_items)
  else
    fetch_fields
  end

  self
end

#divider(**kwargs) ⇒ Object



419
420
421
# File 'lib/avo/resources/base.rb', line 419

def divider(**kwargs)
  entity_loader(:action).use({class: Divider, **kwargs}.compact)
end

#entity_loader(entity) ⇒ Object



732
733
734
# File 'lib/avo/resources/base.rb', line 732

def entity_loader(entity)
  instance_variable_get(:"@#{entity.to_s.pluralize}_loader")
end

#fetch_cardsObject



409
410
411
412
413
414
415
416
417
# File 'lib/avo/resources/base.rb', line 409

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_fieldsObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/avo/resources/base.rb', line 390

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]

  # Safe navigation operator is used because the view can be "destroy"
  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_iconObject



535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/avo/resources/base.rb', line 535

def fetch_record_icon
  return icon if @record.nil?

  # Get the icon from the record if icon is not set
  return @record.try(:icon) if icon.nil?

  # If the icon is a symbol, get the value from the record else execute the block/string
  case icon
  when Symbol
    @record.send icon
  when Proc
    Avo::ExecutionContext.new(target: icon, resource: self, record: @record).handle
  end
end

#fetch_record_titleObject



516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/avo/resources/base.rb', line 516

def fetch_record_title
  return name if @record.nil?

  # Get the title from the record if title is not set, try to get the name, title or label, or fallback to the to_param
  return @record.try(:name) || @record.try(:title) || @record.try(:label) || @record.to_param if title.nil?

  # If the title is a symbol, get the value from the record else execute the block/string
  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



579
580
581
582
583
584
585
586
587
588
# File 'lib/avo/resources/base.rb', line 579

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_hashObject



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'lib/avo/resources/base.rb', line 622

def file_hash
  content_to_be_hashed = ""

  file_base = self.class.class_name.underscore
  resource_path = Rails.root.join("app", "avo", "resources", "#{file_base}.rb").to_s
  if File.file? resource_path
    content_to_be_hashed += File.read(resource_path)
  end

  # policy file hash
  policy_path = Rails.root.join("app", "policies", "#{file_base.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_nameObject



640
641
642
# File 'lib/avo/resources/base.rb', line 640

def file_name
  @file_name ||= self.class.underscore_name.tr(" ", "_")
end

#fill_record(record, permitted_params, extra_params: [], fields: nil) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/avo/resources/base.rb', line 590

def fill_record(record, permitted_params, extra_params: [], fields: nil)
  # Write the field values
  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

  # Write the user configured extra params to the record
  if extra_params.present?
    # Pick only the extra params
    # params at this point are already permitted, only need the keys to access them
    extra_attributes = permitted_params.slice(*flatten_keys(extra_params))

    # Let Rails fill in the rest of the params
    record.assign_attributes extra_attributes
  end

  safe_call(:fill_nested_records, record:, permitted_params:) || record
end

#find_action(action_id) ⇒ Object



468
469
470
471
472
473
474
# File 'lib/avo/resources/base.rb', line 468

def find_action(action_id)
  actions = get_actions + Array(safe_call(:get_actions_from_custom_controls))

  actions
    .uniq { |action| action[:class].to_s }
    .find { |action| action[:class].to_s == action_id.to_s }
end

#form_scopeObject



708
709
710
# File 'lib/avo/resources/base.rb', line 708

def form_scope
  model_class.base_class.to_s.underscore.downcase
end


753
754
755
756
757
758
759
760
761
762
# File 'lib/avo/resources/base.rb', line 753

def get_external_link
  return if record.nil?
  # Skip only on the "create" form views where there's no saved record to
  # link to yet. Avoid `record.persisted?` here: non-ActiveRecord records
  # (e.g. API-backed resources) report `persisted? => false` and would
  # never render an external link.
  return if view&.new? || view&.create?

  Avo::ExecutionContext.new(target: external_link, resource: self, record: record).handle
end

#get_itemsObject

Renderable items for the resource view. Injects a header if none is defined and, when the user hasn't taken control by declaring at least one panel, wraps standalone field groups in a Panel + Card so they render with the expected chrome at the top level.



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
# File 'lib/avo/resources/base.rb', line 349

def get_items
  grouped_items = visible_items.slice_when do |prev, curr|
    is_standalone?(prev) != is_standalone?(curr)
  end.to_a.map do |group|
    {elements: group, is_standalone: is_standalone?(group.first)}
  end

  if items.none? { |item| item.is_header? }
    header = Avo::Resources::Items::Header.new
    hydrate_item header
    grouped_items.unshift({elements: [header], is_standalone: false})
  end

  if items.none? { |item| item.is_panel? }
    grouped_items.select { |group| group[:is_standalone] }.each do |group|
      calculated_panel = Avo::Resources::Items::Panel.new
      hydrate_item calculated_panel

      card = Avo::Resources::Items::Card.new
      hydrate_item card
      card.items_holder.items = group[:elements]
      calculated_panel.items_holder.items = [card]

      group[:elements] = calculated_panel
    end
  end

  grouped_items.flat_map { |group| group[:elements] }
end

#has_record_id?Boolean

Returns:

  • (Boolean)


712
713
714
# File 'lib/avo/resources/base.rb', line 712

def has_record_id?
  record.present? && record_id.present?
end

#hydrateObject



476
477
478
479
480
481
482
483
484
# File 'lib/avo/resources/base.rb', line 476

def hydrate(...)
  super

  if @record.present?
    hydrate_model_with_default_values if @view&.new?
  end

  self
end

#hydrate_model_with_default_valuesObject

We will not overwrite any attributes that come pre-filled in the record.



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/avo/resources/base.rb', line 655

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]) && @params[:via_record_id].present?
          # set the value to the actual record
          via_resource = Avo.resource_manager.get_resource_by_model_class(@params[:via_relation_class])
          value = via_resource.find_record(@params[:via_record_id]) if via_resource.present?
        elsif reflection.present? && reflection.foreign_key.present? && field.id.to_s == @params[:via_relation].to_s && @params[:via_record_id].present?
          resource = Avo.resource_manager.get_resource_by_model_class params[:via_relation_class]
          record = resource.find_record @params[:via_record_id], params: params if resource.present?
          id_param = reflection.options[:primary_key] || :id

          value = record.send(id_param) if record.present?
        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_attributeObject



716
717
718
# File 'lib/avo/resources/base.rb', line 716

def id_attribute
  :id
end

#index_view_frameObject



310
311
312
# File 'lib/avo/resources/base.rb', line 310

def index_view_frame
  "#{model_key}_index"
end

#index_view_lazy_loading?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/avo/resources/base.rb', line 306

def index_view_lazy_loading?
  self.class.index_view_loading.to_s == "lazy"
end

#model_classObject

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.



506
507
508
509
510
# File 'lib/avo/resources/base.rb', line 506

def model_class
  record_class = @record&.class

  self.class.model_class record_class: record_class
end

#model_nameObject



692
693
694
# File 'lib/avo/resources/base.rb', line 692

def model_name
  model_class.model_name
end

#record_iconObject



531
532
533
# File 'lib/avo/resources/base.rb', line 531

def record_icon
  fetch_record_icon.to_s
end

#record_idObject



720
721
722
# File 'lib/avo/resources/base.rb', line 720

def record_id
  record.send(id_attribute)
end

#record_paramObject



736
737
738
# File 'lib/avo/resources/base.rb', line 736

def record_param
  @record_param ||= @record.persisted? ? @record.to_param : nil
end

#record_pathObject



700
701
702
# File 'lib/avo/resources/base.rb', line 700

def record_path
  resource_path(record: record, resource: self)
end

#record_titleObject



512
513
514
# File 'lib/avo/resources/base.rb', line 512

def record_title
  fetch_record_title.to_s
end

#records_pathObject



704
705
706
# File 'lib/avo/resources/base.rb', line 704

def records_path
  resources_path(resource: self)
end

#resolve_component(original_component) ⇒ Object



749
750
751
# File 'lib/avo/resources/base.rb', line 749

def resolve_component(original_component)
  custom_components.dig(original_component.to_s)&.to_s&.safe_constantize || original_component
end

#resource_type_array?Boolean

Returns:

  • (Boolean)


764
# File 'lib/avo/resources/base.rb', line 764

def resource_type_array? = false

#singular_model_keyObject



696
697
698
# File 'lib/avo/resources/base.rb', line 696

def singular_model_key
  model_class.model_name.singular
end

#sort_by_paramObject



766
767
768
769
770
771
772
773
774
# File 'lib/avo/resources/base.rb', line 766

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

Returns:

  • (Boolean)


776
# File 'lib/avo/resources/base.rb', line 776

def sorting_supported? = true

#view_typeObject



778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/avo/resources/base.rb', line 778

def view_type
  @view_type ||= if @params[:view_type].present?
    Avo::ViewInquirer.new(@params[:view_type])
  elsif available_view_types.size == 1
    Avo::ViewInquirer.new(available_view_types.first)
  else
    Avo::ViewInquirer.new(Avo::ExecutionContext.new(
      target: default_view_type || Avo.configuration.default_view_type,
      resource: self,
      view: @view
    ).handle)
  end
end