-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rename collection decorator support (#35)
- Loading branch information
1 parent
765ef50
commit e0cad85
Showing
11 changed files
with
485 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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
70 changes: 70 additions & 0 deletions
70
...asource_customizer/decorators/rename_collection/rename_collection_datasource_decorator.rb
This file contains 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,70 @@ | ||
module ForestAdminDatasourceCustomizer | ||
module Decorators | ||
module RenameCollection | ||
class RenameCollectionDatasourceDecorator < ForestAdminDatasourceToolkit::Decorators::DatasourceDecorator | ||
include ForestAdminDatasourceToolkit | ||
include ForestAdminDatasourceToolkit::Decorators | ||
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree | ||
|
||
def initialize(child_datasource) | ||
@from_child_name = {} | ||
@to_child_name = {} | ||
super(child_datasource, RenameCollectionDecorator) | ||
end | ||
|
||
def collections | ||
@child_datasource.collections.to_h do |name, _collection| | ||
[name, method(:get_collection).super_method.call(name)] | ||
end | ||
end | ||
|
||
def get_collection(name) | ||
# Collection has been renamed, user is using the new name | ||
return super(@to_child_name[name]) if @to_child_name.key?(name) | ||
|
||
# Collection has been renamed, user is using the old name | ||
if @from_child_name.key?(name) | ||
raise Exceptions::ForestException, "Collection '#{name}' has been renamed to '#{@from_child_name[name]}'" | ||
end | ||
|
||
# Collection has not been renamed | ||
super(name) | ||
end | ||
|
||
def rename_collections(renames = []) | ||
renames.each do |current_name, new_name| | ||
rename_collection(current_name, new_name) | ||
end | ||
end | ||
|
||
def rename_collection(current_name, new_name) | ||
# Check collection exists | ||
get_collection(current_name) | ||
|
||
return unless current_name != new_name | ||
|
||
# Check new name is not already used | ||
if collections.any? { |name, _collection| name == new_name } | ||
raise Exceptions::ForestException, | ||
"The given new collection name '#{new_name}' is already defined" | ||
end | ||
|
||
# Check we don't rename a collection twice | ||
if @to_child_name[current_name] | ||
raise Exceptions::ForestException, | ||
"Cannot rename a collection twice: #{@to_child_name[current_name]}->#{current_name}->#{new_name}" | ||
end | ||
|
||
@from_child_name[current_name] = new_name | ||
@to_child_name[new_name] = current_name | ||
|
||
collections.each_value(&:mark_schema_as_dirty) | ||
end | ||
|
||
def get_collection_name(child_name) | ||
@from_child_name[child_name] || child_name | ||
end | ||
end | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
...t_admin_datasource_customizer/decorators/rename_collection/rename_collection_decorator.rb
This file contains 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,37 @@ | ||
module ForestAdminDatasourceCustomizer | ||
module Decorators | ||
module RenameCollection | ||
class RenameCollectionDecorator < ForestAdminDatasourceToolkit::Decorators::CollectionDecorator | ||
include ForestAdminDatasourceToolkit::Decorators | ||
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree | ||
|
||
def name | ||
datasource.get_collection_name(super) | ||
end | ||
|
||
def refine_schema(sub_schema) | ||
fields = {} | ||
|
||
sub_schema[:fields].each do |name, old_schema| | ||
if old_schema.type != 'Column' | ||
old_schema.foreign_collection = datasource.get_collection_name(old_schema.foreign_collection) | ||
if old_schema.type == 'ManyToMany' | ||
old_schema.through_collection = datasource.get_collection_name(old_schema.through_collection) | ||
end | ||
end | ||
|
||
fields[name] = old_schema | ||
end | ||
|
||
sub_schema | ||
end | ||
|
||
# rubocop:disable Lint/UselessMethodDefinition | ||
def mark_schema_as_dirty | ||
super | ||
end | ||
# rubocop:enable Lint/UselessMethodDefinition | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 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.