Skip to content

Commit f228f54

Browse files
authored
enhancement: access record on array block (#3787)
* enhancement: access_record_on_array_block * fix
1 parent 662b49c commit f228f54

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

app/controllers/avo/associations_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def index
2929
# When other field type, like has_many the @query is directly fetched from the parent record
3030
# Don't apply policy on array type since it can return an array of hashes where `.all` and other methods used on policy will fail.
3131
@query = if @field.type == "array"
32-
@resource.fetch_records(Avo::ExecutionContext.new(target: @field.block).handle || @parent_record.try(@field.id))
32+
@resource.fetch_records(Avo::ExecutionContext.new(target: @field.block, record: @parent_record).handle || @parent_record.try(@field.id))
3333
else
3434
@related_authorization.apply_policy(
3535
@parent_record.send(

lib/avo/resources/array_resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def fetch_records(array_of_records = nil)
5555

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

58-
records_from_field_or_record = Avo::ExecutionContext.new(target: association_field.block).handle || via_record.try(route_key)
58+
records_from_field_or_record = Avo::ExecutionContext.new(target: association_field.block, record: via_record).handle || via_record.try(route_key)
5959

6060
array_of_records = records_from_field_or_record || array_of_records
6161
end

spec/dummy/app/avo/resources/event.rb

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,8 @@ def fields
4949

5050
# this field demonstrated how one can use the array field to display an arbitrary array of objects as a "has_many field"
5151
field :attendees, as: :array do
52-
[
53-
{id: 1, name: "John Doe", role: "Software Developer", organization: "TechCorp"},
54-
{id: 2, name: "Jane Smith", role: "Data Scientist", organization: "DataPros"},
55-
{id: 3, name: "Emily Davis", role: "Product Manager", organization: "Startup Inc."},
56-
{id: 4, name: "Kevin Roberts", role: "CTO", organization: "FutureTech"},
57-
{id: 5, name: "Sarah Johnson", role: "UI/UX Designer", organization: "DesignWorks"},
58-
{id: 6, name: "Michael Lee", role: "Backend Engineer", organization: "CodeBase"},
59-
{id: 7, name: "Olivia Brown", role: "Project Coordinator", organization: "BuildIt"},
60-
{id: 8, name: "Ethan Williams", role: "AI Specialist", organization: "InnoBots"},
61-
{id: 9, name: "Sophia Martinez", role: "Marketing Strategist", organization: "Brandify"},
62-
{id: 10, name: "Jacob Wilson", role: "DevOps Engineer", organization: "OpsWorld"},
63-
{id: 11, name: "Ava Taylor", role: "Business Analyst", organization: "AnalyzeNow"},
64-
{id: 12, name: "William Hernandez", role: "Full Stack Developer", organization: "Webify"},
65-
{id: 13, name: "Mia Moore", role: "HR Manager", organization: "PeopleFirst"},
66-
{id: 14, name: "James Anderson", role: "Blockchain Developer", organization: "ChainWorks"},
67-
{id: 15, name: "Charlotte White", role: "Product Designer", organization: "Cre8tive"},
68-
{id: 16, name: "Benjamin Green", role: "Cybersecurity Analyst", organization: "SecureNet"},
69-
{id: 17, name: "Amelia Clark", role: "Data Engineer", organization: "BigData Solutions"},
70-
{id: 18, name: "Lucas Carter", role: "Scrum Master", organization: "AgileHub"},
71-
{id: 19, name: "Ella Thompson", role: "Software Architect", organization: "CodeVision"},
72-
{id: 20, name: "Alexander Scott", role: "Solutions Consultant", organization: "Innovate Consulting"}
73-
]
52+
# This is testing that array block can access the record.something
53+
record.attendees_from_block
7454
end
7555
end
7656
end

spec/dummy/app/models/event.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,30 @@ def first_user
2525
def attendees
2626
raise "Test array resource, this method should not be called"
2727
end
28+
29+
# This is testing that array block can access the record.something
30+
def attendees_from_block
31+
[
32+
{id: 1, name: "John Doe", role: "Software Developer", organization: "TechCorp"},
33+
{id: 2, name: "Jane Smith", role: "Data Scientist", organization: "DataPros"},
34+
{id: 3, name: "Emily Davis", role: "Product Manager", organization: "Startup Inc."},
35+
{id: 4, name: "Kevin Roberts", role: "CTO", organization: "FutureTech"},
36+
{id: 5, name: "Sarah Johnson", role: "UI/UX Designer", organization: "DesignWorks"},
37+
{id: 6, name: "Michael Lee", role: "Backend Engineer", organization: "CodeBase"},
38+
{id: 7, name: "Olivia Brown", role: "Project Coordinator", organization: "BuildIt"},
39+
{id: 8, name: "Ethan Williams", role: "AI Specialist", organization: "InnoBots"},
40+
{id: 9, name: "Sophia Martinez", role: "Marketing Strategist", organization: "Brandify"},
41+
{id: 10, name: "Jacob Wilson", role: "DevOps Engineer", organization: "OpsWorld"},
42+
{id: 11, name: "Ava Taylor", role: "Business Analyst", organization: "AnalyzeNow"},
43+
{id: 12, name: "William Hernandez", role: "Full Stack Developer", organization: "Webify"},
44+
{id: 13, name: "Mia Moore", role: "HR Manager", organization: "PeopleFirst"},
45+
{id: 14, name: "James Anderson", role: "Blockchain Developer", organization: "ChainWorks"},
46+
{id: 15, name: "Charlotte White", role: "Product Designer", organization: "Cre8tive"},
47+
{id: 16, name: "Benjamin Green", role: "Cybersecurity Analyst", organization: "SecureNet"},
48+
{id: 17, name: "Amelia Clark", role: "Data Engineer", organization: "BigData Solutions"},
49+
{id: 18, name: "Lucas Carter", role: "Scrum Master", organization: "AgileHub"},
50+
{id: 19, name: "Ella Thompson", role: "Software Architect", organization: "CodeVision"},
51+
{id: 20, name: "Alexander Scott", role: "Solutions Consultant", organization: "Innovate Consulting"}
52+
]
53+
end
2854
end

0 commit comments

Comments
 (0)