-
-
Notifications
You must be signed in to change notification settings - Fork 280
feature: array adapter #3469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feature: array adapter #3469
Changes from 37 commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
eb9ad52
feature: array adapter
adrianthedev 91d8b41
update ovei list
adrianthedev 5d2973a
wip: hide unsupported actions, implement show view
Paul-Bob e1ed8cd
some lint
Paul-Bob e639cb8
revert indentation
Paul-Bob c789b97
Merge branch 'main' into feature/array-adapter
Paul-Bob 1fa0f72
lint
Paul-Bob b6868ae
Merge branch 'feature/array-adapter' of github.com:avo-hq/avo into fe…
Paul-Bob 6355d8b
lint
Paul-Bob 921330f
rm `ActiveRecordResource` for now
Paul-Bob 3572078
wip
Paul-Bob 30e4fb5
rm ostruct requirement
Paul-Bob 16dd5f9
make it work with array of active records
Paul-Bob 14cc44d
Merge branch 'main' into feature/array-adapter
Paul-Bob dd1c32b
has_many field array option
Paul-Bob da0b783
lint
Paul-Bob 9fd7808
fix
Paul-Bob b4c6e01
Merge branch 'main' into feature/array-adapter
Paul-Bob 0482080
fix flaky test
Paul-Bob bcb3e91
fix flaky test
Paul-Bob 1eca2c8
prepare dummy for tests
Paul-Bob 24e266a
lint
Paul-Bob 6fe69cb
index tests
Paul-Bob 0a74f15
lint & flaky test
Paul-Bob 21dbd3c
lint
Paul-Bob cee07ca
more tests
Paul-Bob 26a62de
lint
Paul-Bob 2f5f5f3
wait_for_loaded on associations
Paul-Bob ea17356
rm copyable
Paul-Bob 4955d60
rm comments
Paul-Bob 61408b5
array resource check
Paul-Bob 228579d
using_wait_time capybara
Paul-Bob feda5d5
.
Paul-Bob cd740df
fix test
Paul-Bob 50de07f
.
Paul-Bob c42f23e
fix test
Paul-Bob 79058a8
fix test
Paul-Bob 163096b
wip
Paul-Bob 0972488
wip
Paul-Bob af256db
wip
Paul-Bob 70743a6
wip
Paul-Bob da96679
wip
Paul-Bob c94d9fd
fix test
Paul-Bob 846e3dd
refactor wip
Paul-Bob 68a0727
Merge branch 'main' into feature/array-adapter
Paul-Bob a8b5c5a
refactor still wip
Paul-Bob 15424d9
fix test
Paul-Bob 431435d
wip refactor
Paul-Bob 95367b4
lint
Paul-Bob c4884bc
Revert "wip refactor"
Paul-Bob be1dc23
re-try
Paul-Bob 815c2fa
Revert "Revert "wip refactor""
Paul-Bob adc614b
re-add display
Paul-Bob 504bebb
rm display
Paul-Bob 224a2a3
Merge branch 'main' into feature/array-adapter
Paul-Bob 8ee548f
add description
Paul-Bob 5bcede9
Merge branch 'feature/array-adapter' of github.com:avo-hq/avo into fe…
Paul-Bob 8d7f82d
fix habtm
Paul-Bob 3a9c32a
fix attach_fields on has one
Paul-Bob 432ff04
is_searchable?
Paul-Bob 4b4b9c1
add attach_scope to has one field
Paul-Bob 0a58c58
rbac changes
Paul-Bob 5dc8f26
lint
Paul-Bob 3641325
lint
Paul-Bob 91de7f5
Merge branch 'main' into feature/array-adapter
Paul-Bob 22093f1
Merge branch 'main' into feature/array-adapter
Paul-Bob 3b70313
minor tweaks
adrianthedev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Avo | ||
class ArrayController < BaseController | ||
def set_query | ||
@query = @resource.fetch_records | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
module Avo | ||
module Resources | ||
class ArrayResource < Base | ||
extend ActiveSupport::DescendantsTracker | ||
|
||
include Avo::Concerns::FindAssociationField | ||
|
||
delegate :model_class, to: :class | ||
|
||
class_attribute :pagination, default: { | ||
type: :array | ||
} | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end | ||
|
||
Paul-Bob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def records = [] | ||
|
||
Paul-Bob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
||
def fetch_records(array_of_records = records) | ||
raise "Unable to fetch any #{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 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 | ||
else | ||
# 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 | ||
|
||
# Dynamically define accessors | ||
attr_accessor(*keys) | ||
|
||
define_method(:to_param) do | ||
id | ||
end | ||
end | ||
|
||
# Map the records to instances of the dynamically created class | ||
array_of_records.map do |item| | ||
custom_class.new(item) | ||
end | ||
end | ||
end | ||
|
||
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 | ||
|
||
def is_active_record_relation?(array_of_records = records) | ||
Paul-Bob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@is_active_record_relation ||= array_of_records.is_a?(ActiveRecord::Relation) | ||
end | ||
|
||
def is_array_resource? = true | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
class Avo::Resources::<%= resource_class %> < Avo::BaseResource | ||
class Avo::Resources::<%= resource_class %> < <%= parent_resource %><% if options["array"] %> | ||
def records | ||
[ | ||
{ | ||
id: 1, | ||
title: "Example 1" | ||
}, | ||
{ | ||
id: 2, | ||
title: "Example 2" | ||
} | ||
] | ||
end | ||
<% else %> | ||
# self.includes = [] | ||
# self.attachments = []<%= model_class_from_args %> | ||
# self.search = { | ||
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) } | ||
# } | ||
|
||
<% end %><% if options["array"] %> | ||
def fields | ||
field :id, as: :id | ||
field :title | ||
end<% else %> | ||
def fields | ||
field :id, as: :id<%= generate_fields %> | ||
end | ||
end<% end %> | ||
end | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Avo::Resources::Attendee < Avo::Resources::ArrayResource | ||
self.description = -> { | ||
return "Attendees from field block" if params["resource_name"] == "events" | ||
return "First 6 users" if params["resource_name"] == "courses" | ||
|
||
"#{@record&.name || "All the users"} rendered as array resource" | ||
} | ||
|
||
# Test array resource, this method should be called only on attendees index | ||
def records = User.all | ||
|
||
def fields | ||
field :id, as: :id | ||
field :name | ||
|
||
with_options visible: -> { !resource.record.is_a?(User) } do | ||
field :role | ||
field :organization | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.