Skip to content

Commit 7655644

Browse files
authored
fix: array resource authorization (#3646)
* fix: array resource authorization * lint * syntax
1 parent 88c7e8d commit 7655644

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

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

+23-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ class ArrayResource < Base
1313

1414
class << self
1515
def model_class
16-
@@model_class ||= ActiveSupport::OrderedOptions.new.tap do |obj|
17-
obj.model_name = ActiveSupport::OrderedOptions.new.tap do |thing|
18-
thing.plural = route_key
16+
@@model_class ||= Object.const_set(
17+
name,
18+
Class.new do
19+
include ActiveModel::Model
20+
21+
class << self
22+
def primary_key = nil
23+
24+
def all = "Avo::Resources::#{name}".constantize.new.fetch_records
25+
end
1926
end
20-
end
27+
)
2128
end
2229
end
2330

@@ -61,16 +68,21 @@ def fetch_records(array_of_records = nil)
6168
# Dynamically create a class with accessors for all unique keys from the records
6269
keys = array_of_records.flat_map(&:keys).uniq
6370

64-
custom_class = Class.new do
65-
include ActiveModel::Model
71+
Object.const_set(
72+
name,
73+
Class.new do
74+
include ActiveModel::Model
6675

67-
# Dynamically define accessors
68-
attr_accessor(*keys)
76+
# Dynamically define accessors
77+
attr_accessor(*keys)
6978

70-
define_method(:to_param) do
71-
id
79+
define_method(:to_param) do
80+
id
81+
end
7282
end
73-
end
83+
)
84+
85+
custom_class = name.constantize
7486

7587
# Map the records to instances of the dynamically created class
7688
array_of_records.map do |item|

0 commit comments

Comments
 (0)