Class: Avo::Resources::ArrayResource

Inherits:
Base
  • Object
show all
Extended by:
ActiveSupport::DescendantsTracker
Includes:
Concerns::FindAssociationField
Defined in:
lib/avo/resources/array_resource.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Constants inherited from Base

Base::VIEW_METHODS_MAPPING

Constants included from Concerns::HasFieldDiscovery

Concerns::HasFieldDiscovery::COLUMN_NAMES_TO_IGNORE

Instance Attribute Summary

Attributes inherited from Base

#record, #reflection, #user, #view

Attributes included from Concerns::HasItems

#items_holder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::FindAssociationField

#find_association_field

Methods inherited from Base

action, #attachment_fields, authorization, #authorization, #available_view_types, #avatar, #avatar_field, #avatar_type, #cache_hash, class_name, #current_user, #custom_components, #default_panel_name, #description_attributes, #detect_fields, #divider, #entity_loader, #fetch_cards, #fetch_fields, #fetch_record_title, fetch_search, #fields_by_database_id, #file_hash, #file_name, #fill_record, filter, find_record, find_scope, #form_scope, get_available_models, #get_external_link, get_model_by_name, #has_record_id?, #hydrate, #hydrate_model_with_default_values, #id_attribute, #initialize, #model_class, model_key, #model_name, name, name_from_translation_key, navigation_label, plural_name, query_scope, #record_id, #record_param, #record_path, #record_title, #records_path, #resolve_component, route_key, scope, search_query, search_results_count, #singular_model_key, singular_route_key, translation_key, underscore_name, valid_association_name, valid_attachment_name

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::Hydration

#hydrate

Methods included from Concerns::HasHelpers

#helpers

Methods included from Concerns::HasProfilePhoto

#profile_photo

Methods included from Concerns::HasCoverPhoto

#cover_photo

Methods included from Concerns::HasDescription

#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_items, #get_preview_fields, #invalid_fields, #is_empty?, #items, #only_fields, #tab_groups, #visible_items

Methods included from Concerns::HasFieldDiscovery

#discover_associations, #discover_columns, #model_db_columns

Constructor Details

This class inherits a constructor from Avo::Resources::Base

Class Method Details

.model_classObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/avo/resources/array_resource.rb', line 15

def model_class
  @@model_class ||= Avo.const_set(
    class_name,
    Class.new do
      include ActiveModel::Model

      class << self
        def primary_key = nil

        def all = "Avo::Resources::#{class_name}".constantize.new.fetch_records
      end
    end
  )
end

Instance Method Details

#fetch_records(array_of_records = nil) ⇒ Object



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

def fetch_records(array_of_records = nil)
  array_of_records ||= records
  raise "Unable to fetch any #{class_name}" if array_of_records.nil?

  # When the array of records is declared in a field's block, we need to get that block from the parent resource
  # If there is no block try to pick those from the parent_record
  # Fallback to resource's def records method
  if params[:via_resource_class].present?
    via_resource = Avo.resource_manager.get_resource(params[:via_resource_class])
    via_record = via_resource.find_record params[:via_record_id], params: params
    via_resource = via_resource.new record: via_record, view: :show
    via_resource.detect_fields

    association_field = find_association_field(resource: via_resource, association: route_key)

    records_from_field_or_record = Avo::ExecutionContext.new(target: association_field.block).handle || via_record.try(route_key)

    array_of_records = records_from_field_or_record || array_of_records
  end

  @fetched_records ||= if is_array_of_active_records?(array_of_records)
    @@model_class = array_of_records.first.class
    @@model_class.where(id: array_of_records.map(&:id))
  elsif is_active_record_relation?(array_of_records)
    @@model_class = array_of_records.try(:model)
    array_of_records
  else
    # Dynamically create a class with accessors for all unique keys from the records
    keys = array_of_records.flat_map(&:keys).uniq

    Avo.const_set(
      class_name,
      Class.new do
        include ActiveModel::Model

        # Dynamically define accessors
        attr_accessor(*keys)

        define_method(:to_param) do
          id
        end
      end
    )

    custom_class = "Avo::#{class_name}".constantize

    # Map the records to instances of the dynamically created class
    array_of_records.map do |item|
      custom_class.new(item)
    end
  end
end

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



33
34
35
36
37
38
39
# File 'lib/avo/resources/array_resource.rb', line 33

def find_record(id, query: nil, params: nil)
  fetched_records = fetch_records

  return super(id, query: fetched_records, params:) if is_active_record_relation?(fetched_records)

  fetched_records.find { |i| i.id.to_s == id.to_s }
end

#is_active_record_relation?(array_of_records = records) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/avo/resources/array_resource.rb', line 98

def is_active_record_relation?(array_of_records = records)
  @is_active_record_relation ||= array_of_records.is_a?(ActiveRecord::Relation)
end

#is_array_of_active_records?(array_of_records = records) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/avo/resources/array_resource.rb', line 94

def is_array_of_active_records?(array_of_records = records)
  @is_array_of_active_records ||= array_of_records.all? { |element| element.is_a?(ActiveRecord::Base) }
end

#recordsObject



31
# File 'lib/avo/resources/array_resource.rb', line 31

def records = []

#resource_type_array?Boolean

Returns:

  • (Boolean)


102
# File 'lib/avo/resources/array_resource.rb', line 102

def resource_type_array? = true

#sort_by_paramObject



104
# File 'lib/avo/resources/array_resource.rb', line 104

def sort_by_param = nil

#sorting_supported?Boolean

Returns:

  • (Boolean)


106
# File 'lib/avo/resources/array_resource.rb', line 106

def sorting_supported? = false