Skip to content

Commit

Permalink
fix: array resource authorization (#3646)
Browse files Browse the repository at this point in the history
* fix: array resource authorization

* lint

* syntax
  • Loading branch information
Paul-Bob authored Feb 7, 2025
1 parent 88c7e8d commit 7655644
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions lib/avo/resources/array_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ class ArrayResource < Base

class << self
def model_class
@@model_class ||= ActiveSupport::OrderedOptions.new.tap do |obj|
obj.model_name = ActiveSupport::OrderedOptions.new.tap do |thing|
thing.plural = route_key
@@model_class ||= Object.const_set(
name,
Class.new do
include ActiveModel::Model

class << self
def primary_key = nil

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

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

custom_class = Class.new do
include ActiveModel::Model
Object.const_set(
name,
Class.new do
include ActiveModel::Model

# Dynamically define accessors
attr_accessor(*keys)
# Dynamically define accessors
attr_accessor(*keys)

define_method(:to_param) do
id
define_method(:to_param) do
id
end
end
end
)

custom_class = name.constantize

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

0 comments on commit 7655644

Please sign in to comment.