-
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 publication decorator support (#34)
- Loading branch information
Showing
17 changed files
with
550 additions
and
21 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
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
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
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
95 changes: 95 additions & 0 deletions
95
...st_admin_datasource_customizer/decorators/publication/publication_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,95 @@ | ||
module ForestAdminDatasourceCustomizer | ||
module Decorators | ||
module Publication | ||
class PublicationCollectionDecorator < ForestAdminDatasourceToolkit::Decorators::CollectionDecorator | ||
include ForestAdminDatasourceToolkit::Exceptions | ||
attr_reader :blacklist | ||
|
||
def initialize(child_collection, datasource) | ||
super | ||
@blacklist = [] | ||
end | ||
|
||
def change_field_visibility(name, visible) | ||
field = child_collection.schema[:fields][name] | ||
raise ForestException, "No such field '#{name}'" unless field | ||
raise ForestException, 'Cannot hide primary key' if ForestAdminDatasourceToolkit::Utils::Schema.primary_key?( | ||
child_collection, name | ||
) | ||
|
||
if visible | ||
@blacklist.delete(name) | ||
else | ||
@blacklist << name | ||
end | ||
|
||
mark_schema_as_dirty | ||
end | ||
|
||
def create(caller, data) | ||
record = {} | ||
child_collection.create(caller, data).each do |key, value| | ||
record[key] = value unless @blacklist.include?(key) | ||
end | ||
|
||
record | ||
end | ||
|
||
def refine_schema(child_schema) | ||
fields = {} | ||
|
||
child_schema[:fields].each do |name, field| | ||
fields[name] = field if published?(name) | ||
end | ||
|
||
child_schema[:fields] = fields | ||
|
||
child_schema | ||
end | ||
|
||
def published?(name) | ||
# Explicitly hidden | ||
return false if @blacklist.include?(name) | ||
|
||
# Implicitly hidden | ||
field = child_collection.schema[:fields][name] | ||
|
||
if field.type == 'ManyToOne' | ||
return ( | ||
datasource.published?(field.foreign_collection) && | ||
published?(field.foreign_key) && | ||
datasource.get_collection(field.foreign_collection).published?(field.foreign_key_target) | ||
) | ||
end | ||
|
||
if field.type == 'OneToOne' || field.type == 'OneToMany' | ||
return ( | ||
datasource.published?(field.foreign_collection) && | ||
datasource.get_collection(field.foreign_collection).published?(field.origin_key) && | ||
published?(field.origin_key_target) | ||
) | ||
end | ||
|
||
if field.type == 'ManyToMany' | ||
return ( | ||
datasource.published?(field.through_collection) && | ||
datasource.published?(field.foreign_collection) && | ||
datasource.get_collection(field.through_collection).published?(field.foreign_key) && | ||
datasource.get_collection(field.through_collection).published?(field.origin_key) && | ||
published?(field.origin_key_target) && | ||
datasource.get_collection(field.foreign_collection).published?(field.foreign_key_target) | ||
) | ||
end | ||
|
||
true | ||
end | ||
|
||
# rubocop:disable Lint/UselessMethodDefinition | ||
def mark_schema_as_dirty | ||
super | ||
end | ||
# rubocop:enable Lint/UselessMethodDefinition | ||
end | ||
end | ||
end | ||
end |
57 changes: 57 additions & 0 deletions
57
...st_admin_datasource_customizer/decorators/publication/publication_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,57 @@ | ||
module ForestAdminDatasourceCustomizer | ||
module Decorators | ||
module Publication | ||
class PublicationDatasourceDecorator < ForestAdminDatasourceToolkit::Decorators::DatasourceDecorator | ||
include ForestAdminDatasourceToolkit::Exceptions | ||
attr_reader :blacklist | ||
|
||
def initialize(child_datasource) | ||
super(child_datasource, PublicationCollectionDecorator) | ||
@blacklist = [] | ||
end | ||
|
||
def collections | ||
@child_datasource.collections.except(*@blacklist).to_h do |name, _collection| | ||
[name, get_collection(name)] | ||
end | ||
end | ||
|
||
def get_collection(name) | ||
raise ForestException, "Collection '#{name}' was removed." if @blacklist.include?(name) | ||
|
||
super(name) | ||
end | ||
|
||
def keep_collections_matching(include = [], exclude = []) | ||
validate_collection_names(include.to_a + exclude.to_a) | ||
|
||
# List collection we're keeping from the white/black list. | ||
@child_datasource.collections.each_key do |collection| | ||
remove_collection(collection) if (include && !include.include?(collection)) || exclude&.include?(collection) | ||
end | ||
end | ||
|
||
def remove_collection(collection_name) | ||
validate_collection_names([collection_name]) | ||
|
||
# Delete the collection | ||
@blacklist << collection_name | ||
|
||
# Tell all collections that their schema is dirty: if we removed a collection, all | ||
# relations to this collection are now invalid and should be unpublished. | ||
collections.each_value(&:mark_schema_as_dirty) | ||
end | ||
|
||
def published?(collection_name) | ||
!@blacklist.include?(collection_name) | ||
end | ||
|
||
private | ||
|
||
def validate_collection_names(names) | ||
names.each { |name| get_collection(name) } | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.