Skip to content
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

feature: array adapter #3469

Open
wants to merge 65 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
eb9ad52
feature: array adapter
adrianthedev Nov 28, 2024
91d8b41
update ovei list
adrianthedev Dec 7, 2024
5d2973a
wip: hide unsupported actions, implement show view
Paul-Bob Jan 9, 2025
e1ed8cd
some lint
Paul-Bob Jan 9, 2025
e639cb8
revert indentation
Paul-Bob Jan 9, 2025
c789b97
Merge branch 'main' into feature/array-adapter
Paul-Bob Jan 9, 2025
1fa0f72
lint
Paul-Bob Jan 9, 2025
b6868ae
Merge branch 'feature/array-adapter' of github.com:avo-hq/avo into fe…
Paul-Bob Jan 9, 2025
6355d8b
lint
Paul-Bob Jan 9, 2025
921330f
rm `ActiveRecordResource` for now
Paul-Bob Jan 9, 2025
3572078
wip
Paul-Bob Jan 10, 2025
30e4fb5
rm ostruct requirement
Paul-Bob Jan 10, 2025
16dd5f9
make it work with array of active records
Paul-Bob Jan 10, 2025
14cc44d
Merge branch 'main' into feature/array-adapter
Paul-Bob Jan 10, 2025
dd1c32b
has_many field array option
Paul-Bob Jan 10, 2025
da0b783
lint
Paul-Bob Jan 10, 2025
9fd7808
fix
Paul-Bob Jan 10, 2025
b4c6e01
Merge branch 'main' into feature/array-adapter
Paul-Bob Jan 13, 2025
0482080
fix flaky test
Paul-Bob Jan 13, 2025
bcb3e91
fix flaky test
Paul-Bob Jan 13, 2025
1eca2c8
prepare dummy for tests
Paul-Bob Jan 13, 2025
24e266a
lint
Paul-Bob Jan 13, 2025
6fe69cb
index tests
Paul-Bob Jan 13, 2025
0a74f15
lint & flaky test
Paul-Bob Jan 13, 2025
21dbd3c
lint
Paul-Bob Jan 13, 2025
cee07ca
more tests
Paul-Bob Jan 13, 2025
26a62de
lint
Paul-Bob Jan 13, 2025
2f5f5f3
wait_for_loaded on associations
Paul-Bob Jan 13, 2025
ea17356
rm copyable
Paul-Bob Jan 13, 2025
4955d60
rm comments
Paul-Bob Jan 13, 2025
61408b5
array resource check
Paul-Bob Jan 13, 2025
228579d
using_wait_time capybara
Paul-Bob Jan 13, 2025
feda5d5
.
Paul-Bob Jan 13, 2025
cd740df
fix test
Paul-Bob Jan 13, 2025
50de07f
.
Paul-Bob Jan 13, 2025
c42f23e
fix test
Paul-Bob Jan 13, 2025
79058a8
fix test
Paul-Bob Jan 13, 2025
163096b
wip
Paul-Bob Jan 14, 2025
0972488
wip
Paul-Bob Jan 14, 2025
af256db
wip
Paul-Bob Jan 15, 2025
70743a6
wip
Paul-Bob Jan 15, 2025
da96679
wip
Paul-Bob Jan 15, 2025
c94d9fd
fix test
Paul-Bob Jan 15, 2025
846e3dd
refactor wip
Paul-Bob Jan 15, 2025
68a0727
Merge branch 'main' into feature/array-adapter
Paul-Bob Jan 16, 2025
a8b5c5a
refactor still wip
Paul-Bob Jan 16, 2025
15424d9
fix test
Paul-Bob Jan 17, 2025
431435d
wip refactor
Paul-Bob Jan 17, 2025
95367b4
lint
Paul-Bob Jan 17, 2025
c4884bc
Revert "wip refactor"
Paul-Bob Jan 17, 2025
be1dc23
re-try
Paul-Bob Jan 17, 2025
815c2fa
Revert "Revert "wip refactor""
Paul-Bob Jan 17, 2025
adc614b
re-add display
Paul-Bob Jan 17, 2025
504bebb
rm display
Paul-Bob Jan 17, 2025
224a2a3
Merge branch 'main' into feature/array-adapter
Paul-Bob Jan 17, 2025
8ee548f
add description
Paul-Bob Jan 17, 2025
5bcede9
Merge branch 'feature/array-adapter' of github.com:avo-hq/avo into fe…
Paul-Bob Jan 17, 2025
8d7f82d
fix habtm
Paul-Bob Jan 17, 2025
3a9c32a
fix attach_fields on has one
Paul-Bob Jan 17, 2025
432ff04
is_searchable?
Paul-Bob Jan 17, 2025
4b4b9c1
add attach_scope to has one field
Paul-Bob Jan 20, 2025
0a58c58
rbac changes
Paul-Bob Jan 20, 2025
5dc8f26
lint
Paul-Bob Jan 20, 2025
3641325
lint
Paul-Bob Jan 20, 2025
91de7f5
Merge branch 'main' into feature/array-adapter
Paul-Bob Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/avo/base_resource.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Avo
class BaseResource < Avo::Resources::Base
class BaseResource < Avo::Resources::ActiveRecordResource
# Users can override this class to add custom methods for all resources.
end
end
66 changes: 66 additions & 0 deletions app/controllers/avo/array_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Avo
class ArrayController < BaseController

Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
def apply_pagination
# Set `trim_extra` to false in associations so the first page has the `page=1` param assigned
@pagy, @records = @resource.apply_pagination(index_params: @index_params, query: @resource.itemss)
end


Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved

Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
# Sub-method called only by #pagy_countless: here for easy customization of variables by overriding
def pagy_countless_get_vars(collection, vars)
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
vars[:page] ||= params[vars[:page_param] || :page]
# abort [collection.all, vars].inspect
vars[:per_page] ||= params[vars[:per_page_param] || :per_page]
vars
end

# Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
# You may need to override this method for collections without offset|limit
def pagy_countless_get_items(collection, pagy)
# abort collection.inspect
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
# abort pagy.inspect
if pagy.vars[:per_page].present?
collection.page(pagy.page).limit(pagy.vars[:per_page]).all.to_a # get the actual collection
else
# abort 1.inspect
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
eager_loaded = collection.page(pagy.page).limit(pagy.items + 1).all.to_a # eager load items + 1
pagy.finalize(eager_loaded.size)
collection.page(pagy.page).limit(pagy.vars[:per_page]).all.to_a # get the actual collection
end
end
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved

Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
def paginate_query
if false
pagy_countless(
@query,
items: @index_params[:per_page],
link_extra: "data-turbo-frame=\"#{params[:turbo_frame]}\"",
size: [],
params: extra_pagy_params
Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
)
else
pagy_countless(
@query,
items: @index_params[:per_page],
link_extra: "data-turbo-frame=\"#{params[:turbo_frame]}\"",
size: [],
params: extra_pagy_params
)
end
end

def perform_save_action!
if @model&.id.present?
@resource.client.update(@model.id, @model)
else
@resource.client.create(@model)
end
end

def perform_destroy_action!
@resource.client.delete(@model.id)
end
end
end
1 change: 1 addition & 0 deletions config/initializers/pagy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "pagy/extras/trim"
require "pagy/extras/countless"
require "pagy/extras/array"
if ::Pagy::VERSION >= ::Gem::Version.new("9.0")
require "pagy/extras/size"
end
Expand Down
1 change: 1 addition & 0 deletions lib/avo/concerns/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Pagination
PAGINATION_METHOD = {
default: :pagy,
countless: :pagy_countless,
array: :pagy_array,
}
end

Expand Down
6 changes: 6 additions & 0 deletions lib/avo/resources/active_record_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Avo
module Resources
class ActiveRecordResource < Base
end
end
end
52 changes: 52 additions & 0 deletions lib/avo/resources/array_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Avo
module Resources
class ArrayResource < Base
extend ActiveSupport::DescendantsTracker

include ActionView::Helpers::UrlHelper
include Avo::Concerns::HasItems
include Avo::Concerns::CanReplaceItems
include Avo::Concerns::HasControls
include Avo::Concerns::HasResourceStimulusControllers
include Avo::Concerns::ModelClassConstantized
include Avo::Concerns::HasDescription
include Avo::Concerns::HasCoverPhoto
include Avo::Concerns::HasProfilePhoto
include Avo::Concerns::HasHelpers
include Avo::Concerns::Hydration
include Avo::Concerns::ControlsPlacement
include Avo::Concerns::Pagination

delegate :model_class, to: :class

class << self

Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved

Paul-Bob marked this conversation as resolved.
Show resolved Hide resolved
# Returns the model class being used for this resource.
#
# The Resource instance has a model_class method too so it can support the STI use cases
# where we figure out the model class from the record
# def model_class(record_class: nil)
# # get the model class off of the static property
# return constantized_model_class if @model_class.present?

# # get the model class off of the record for STI models
# return record_class if record_class.present?

# # generate a model class
# class_name.safe_constantize
# end

def model_class
ActiveSupport::OrderedOptions.new.tap do |obj|
obj.model_name = ActiveSupport::OrderedOptions.new.tap do |thing|
thing.plural = "Array"
# thing.plural = "Array"
end
obj.column_names = [:id, :name, :release_date]
end
end
end
end
end
end
Loading
Loading