-
Notifications
You must be signed in to change notification settings - Fork 1
feat: handle rpc relations #240
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b15df3e
feat: handle rpc relations
arnaud-moncel 95ec2b6
fix: add econciliate plugin
arnaud-moncel 18acf85
fix: access datasource
arnaud-moncel af19ddc
chore: fix perf after introspection
arnaud-moncel 307f808
fix: attribute access
arnaud-moncel c5d0d43
chore: improve rel
arnaud-moncel 189c74f
fix: add rename options to reconciliate pluggin
arnaud-moncel cc6b9fb
fix: lint
arnaud-moncel ab8202c
test: fix test
arnaud-moncel 70b3ab0
chore: refactore add_relation plugin
arnaud-moncel 0997e27
test: add test
arnaud-moncel 29fcdcd
test: fix test
arnaud-moncel 73984d1
test: add test
arnaud-moncel 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
2 changes: 2 additions & 0 deletions
2
...dmin_datasource_customizer/lib/forest_admin_datasource_customizer/composite_datasource.rb
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
2 changes: 1 addition & 1 deletion
2
...min_datasource_customizer/lib/forest_admin_datasource_customizer/datasource_customizer.rb
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
71 changes: 71 additions & 0 deletions
71
packages/forest_admin_datasource_rpc/lib/forest_admin_datasource_rpc/reconciliate_rpc.rb
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,71 @@ | ||
| module ForestAdminDatasourceRpc | ||
| class ReconciliateRpc < ForestAdminDatasourceCustomizer::Plugins::Plugin | ||
| def run(datasource_customizer, _collection_customizer = nil, options = {}) | ||
| datasource_customizer.composite_datasource.datasources.each do |datasource| | ||
| real_datasource = get_datasource(datasource) | ||
| next unless real_datasource.is_a?(ForestAdminDatasourceRpc::Datasource) | ||
|
|
||
| # Disable search for non-searchable collections | ||
| real_datasource.collections.each_value do |collection| | ||
| unless collection.schema[:searchable] | ||
| cz = datasource_customizer.get_collection(get_collection_name(options[:rename], collection.name)) | ||
| cz.disable_search | ||
| end | ||
| end | ||
|
|
||
| # Add relations from rpc_relations | ||
| (real_datasource.rpc_relations || {}).each do |collection_name, relations| | ||
| collection_name = get_collection_name(options[:rename], collection_name) | ||
| cz = datasource_customizer.get_collection(collection_name) | ||
|
|
||
| relations.each do |relation_name, relation_definition| | ||
| add_relation(cz, options[:rename], relation_name.to_s, relation_definition) | ||
| end | ||
| end | ||
| end | ||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| private | ||
|
|
||
| def get_datasource(datasource) | ||
| # can be publication -> rename deco or a custom one | ||
| while datasource.is_a?(ForestAdminDatasourceToolkit::Decorators::DatasourceDecorator) | ||
| datasource = datasource.child_datasource | ||
| end | ||
|
|
||
| datasource | ||
| end | ||
|
|
||
| def get_collection_name(renames, collection_name) | ||
| name = collection_name | ||
|
|
||
| if renames.is_a?(Proc) | ||
| name = renames.call(collection_name) | ||
| elsif renames.is_a?(Hash) && renames.key?(collection_name.to_s) | ||
| name = renames[collection_name.to_s] | ||
| end | ||
|
|
||
| name | ||
| end | ||
|
|
||
| def add_relation(collection_customizer, renames, relation_name, relation_definition) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| relation = relation_definition.transform_keys(&:to_sym) | ||
| foreign_collection = get_collection_name(renames, relation[:foreign_collection]) | ||
| options = relation.except(:type, :foreign_collection, :through_collection) | ||
|
|
||
| case relation[:type] | ||
| when 'ManyToMany' | ||
| through_collection = get_collection_name(renames, relation[:through_collection]) | ||
| collection_customizer.add_many_to_many_relation(relation_name, foreign_collection, through_collection, options) | ||
| when 'OneToMany' | ||
| collection_customizer.add_one_to_many_relation(relation_name, foreign_collection, options) | ||
| when 'OneToOne' | ||
| collection_customizer.add_one_to_one_relation(relation_name, foreign_collection, options) | ||
| when 'ManyToOne' | ||
| collection_customizer.add_many_to_one_relation(relation_name, foreign_collection, options) | ||
| else | ||
| raise ForestAdminDatasourceToolkit::Exceptions::ForestException, "Unsupported relation type: #{relation[:type]}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
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.