Class: Avo::Resources::ArrayResource
- 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
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
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_records(array_of_records = nil) ⇒ Object
- #find_record(id, query: nil, params: nil) ⇒ Object
- #is_active_record_relation?(array_of_records = records) ⇒ Boolean
- #is_array_of_active_records?(array_of_records = records) ⇒ Boolean
- #is_array_of_store_model?(array_of_records = records) ⇒ Boolean
- #records ⇒ Object
- #resource_type_array? ⇒ Boolean
- #sort_by_param ⇒ Object
- #sorting_supported? ⇒ Boolean
Methods included from Concerns::FindAssociationField
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::SafeCall
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
Methods included from Concerns::Pagination
#apply_pagination, #pagination_type
Methods included from Concerns::Hydration
Methods included from Concerns::HasHelpers
Methods included from Concerns::HasProfilePhoto
Methods included from Concerns::HasCoverPhoto
Methods included from Concerns::HasDescription
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
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_class ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/avo/resources/array_resource.rb', line 17 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
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 93 94 95 96 97 98 99 |
# File 'lib/avo/resources/array_resource.rb', line 43 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 array_of_records.empty? array_of_records elsif 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 elsif is_array_of_store_model?(array_of_records) @@model_class = array_of_records.first.class return(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
35 36 37 38 39 40 41 |
# File 'lib/avo/resources/array_resource.rb', line 35 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
105 106 107 |
# File 'lib/avo/resources/array_resource.rb', line 105 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
101 102 103 |
# File 'lib/avo/resources/array_resource.rb', line 101 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 |
#is_array_of_store_model?(array_of_records = records) ⇒ Boolean
109 110 111 |
# File 'lib/avo/resources/array_resource.rb', line 109 def is_array_of_store_model?(array_of_records = records) @is_array_of_store_model ||= defined?(StoreModel::Model) && array_of_records.all? { |element| element.is_a?(StoreModel::Model) } end |
#records ⇒ Object
33 |
# File 'lib/avo/resources/array_resource.rb', line 33 def records = [] |
#resource_type_array? ⇒ Boolean
113 |
# File 'lib/avo/resources/array_resource.rb', line 113 def resource_type_array? = true |
#sort_by_param ⇒ Object
115 |
# File 'lib/avo/resources/array_resource.rb', line 115 def sort_by_param = nil |
#sorting_supported? ⇒ Boolean
117 |
# File 'lib/avo/resources/array_resource.rb', line 117 def sorting_supported? = false |