Skip to content

Commit ca8dbab

Browse files
authored
enhancement: StoreModel::Model support for array resources (avo-hq#3753)
* enhancement: StoreModel::Model support for array resources * fix test
1 parent a583e4c commit ca8dbab

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

Diff for: app/components/avo/index/table_row_component.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<%# hover:z-[21] removed from tr class to solve flickering actions component on row controls and z-20 changed to z-21%>
22

33
<%= content_tag :tr,
4-
id: "#{self.class.to_s.underscore}_#{@resource.record.to_param}",
4+
id: "#{self.class.to_s.underscore}_#{@resource.record_param}",
55
class: class_names("group bg-white hover:bg-gray-50 z-21 border-b", {"cursor-pointer": click_row_to_view_record}),
66
data: {
77
index: @index,
88
component_name: self.class.to_s.underscore,
99
resource_name: @resource.class.to_s,
10-
record_id: @resource.record.id,
10+
record_id: @resource.record_param,
1111
**(click_row_to_view_record ? {
1212
visit_path: helpers.resource_show_path(resource: @resource, parent_resource: @parent_resource, parent_record: @parent_record, parent_or_child_resource:),
1313
action: "click->table-row#visitRecord keydown.enter->table-row#visitRecord keydown.space->table-row#visitRecord"

Diff for: app/controllers/avo/actions_controller.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,15 @@ def get_messages
184184
end
185185

186186
def decrypted_index_query
187-
@decrypted_index_query ||= if (encrypted_query = action_params[:fields]&.dig(:avo_index_query)).present?
187+
@decrypted_index_query ||= if encrypted_query.present? && encrypted_query != "select_all_disabled"
188188
Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal)
189189
end
190190
end
191191

192+
def encrypted_query
193+
@encrypted_query ||= action_params[:fields]&.dig(:avo_index_query)
194+
end
195+
192196
def flash_messages
193197
get_messages.each do |message|
194198
flash[message[:type]] = message[:body]

Diff for: app/controllers/avo/base_application_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def set_related_resource
135135
end
136136

137137
def set_record
138-
id = if @resource.model_class.primary_key.is_a?(Array) && params.respond_to?(:extract_value)
138+
id = if @resource.model_class.try(:primary_key).is_a?(Array) && params.respond_to?(:extract_value)
139139
params.extract_value(:id)
140140
else
141141
params[:id]

Diff for: lib/avo/resources/array_resource.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ def fetch_records(array_of_records = nil)
5858
array_of_records = records_from_field_or_record || array_of_records
5959
end
6060

61-
@fetched_records ||= if is_array_of_active_records?(array_of_records)
61+
@fetched_records ||= if array_of_records.empty?
62+
array_of_records
63+
elsif is_array_of_active_records?(array_of_records)
6264
@@model_class = array_of_records.first.class
6365
@@model_class.where(id: array_of_records.map(&:id))
6466
elsif is_active_record_relation?(array_of_records)
6567
@@model_class = array_of_records.try(:model)
6668
array_of_records
69+
elsif is_array_of_store_model?(array_of_records)
70+
@@model_class = array_of_records.first.class
71+
return(array_of_records)
6772
else
6873
# Dynamically create a class with accessors for all unique keys from the records
6974
keys = array_of_records.flat_map(&:keys).uniq
@@ -99,6 +104,10 @@ def is_active_record_relation?(array_of_records = records)
99104
@is_active_record_relation ||= array_of_records.is_a?(ActiveRecord::Relation)
100105
end
101106

107+
def is_array_of_store_model?(array_of_records = records)
108+
@is_array_of_store_model ||= defined?(StoreModel::Model) && array_of_records.all? { |element| element.is_a?(StoreModel::Model) }
109+
end
110+
102111
def resource_type_array? = true
103112

104113
def sort_by_param = nil

Diff for: spec/system/avo/has_many_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def destroy
117117
expect(page).to have_css('div[data-field-id="patrons"] div[data-target="tag-component"]', text: user.name)
118118

119119
# Find user name on has many field
120-
within("tr[data-record-id='#{user.id}']") do
120+
within("tr[data-record-id='#{user.to_param}']") do
121121
expect(page).to have_text(user.first_name)
122122
expect(page).to have_text(user.last_name)
123123
end

0 commit comments

Comments
 (0)