Skip to content

Commit

Permalink
feat: add customizer with decorator stack and empty-decorator (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Alexandre <nicolasalexandre9@gmail.com>
  • Loading branch information
matthv and nicolasalexandre9 authored Dec 18, 2023
1 parent 8920018 commit 900effd
Show file tree
Hide file tree
Showing 82 changed files with 1,283 additions and 457 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
package:
- forest_admin_agent
- forest_admin_datasource_active_record
- forest_admin_datasource_customizer
- forest_admin_datasource_toolkit
- forest_admin_rails

Expand All @@ -34,13 +35,8 @@ jobs:
gem install bundler
bundle install
- name: Install dependencies on agent package
working-directory: ./packages/forest_admin_agent
run: bundle install

- name: Install dependencies on plugin rails package
working-directory: ./packages/forest_admin_rails
run: bundle install
- name: Install dependencies on packages
run: cd packages/${{ matrix.package }} && bundle install && cd -

- name: Run RuboCop
run: bundle exec rubocop
Expand All @@ -54,6 +50,9 @@ jobs:
ruby-version: ['3.0', '3.1', '3.2']
package:
- forest_admin_agent
- forest_admin_datasource_active_record
- forest_admin_datasource_customizer
- forest_admin_datasource_toolkit
- forest_admin_rails
steps:
- name: Checkout
Expand Down
3 changes: 3 additions & 0 deletions .releaserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ module.exports = {
'sed -i \'s/LIANA_VERSION = ".*"/LIANA_VERSION = "${nextRelease.version}"/g\' packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/schema_emitter.rb; ' +
'sed -i \'s/VERSION = ".*"/VERSION = "${nextRelease.version}"/g\' packages/forest_admin_datasource_active_record/lib/forest_admin_datasource_active_record/version.rb; '+
'sed -i \'s/VERSION = ".*"/VERSION = "${nextRelease.version}"/g\' packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/version.rb; '+
'sed -i \'s/VERSION = ".*"/VERSION = "${nextRelease.version}"/g\' packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_customizer/version.rb; '+
'sed -i \'s/VERSION = ".*"/VERSION = "${nextRelease.version}"/g\' packages/forest_admin_rails/lib/forest_admin_rails/version.rb; ',
successCmd:
'( cd packages/forest_admin_agent && gem build && gem push forest_admin_agent-*.gem );' +
'( cd packages/forest_admin_datasource_active_record && gem build && gem push forest_admin_datasource_active_record-*.gem );' +
'( cd packages/forest_admin_datasource_toolkit && gem build && gem push forest_admin_datasource_toolkit-*.gem );' +
'( cd packages/forest_admin_datasource_customizer && gem build && gem push forest_admin_datasource_customizer-*.gem );' +
'( cd packages/forest_admin_rails && gem build && gem push forest_admin_rails-*.gem );' ,
},
],
Expand All @@ -40,6 +42,7 @@ module.exports = {
'packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/schema_emitter.rb',
'packages/forest_admin_datasource_active_record/lib/forest_admin_datasource_active_record/version.rb',
'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/version.rb',
'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_customizer/version.rb',
'packages/forest_admin_rails/lib/forest_admin_rails/version.rb',
'package.json'
],
Expand Down
14 changes: 14 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gemspec/OrderedDependencies:
- 'packages/forest_admin_agent/forest_admin_agent.gemspec'
- 'packages/forest_admin_rails/forest_admin_rails.gemspec'
- 'packages/forest_admin_datasource_toolkit/forest_admin_datasource_toolkit.gemspec'
- 'packages/forest_admin_datasource_customizer/forest_admin_datasource_customizer.gemspec'
- 'packages/forest_admin_datasource_active_record/forest_admin_datasource_active_record.gemspec'

Gemspec/RequireMFA:
Expand All @@ -24,6 +25,7 @@ Gemspec/RequireMFA:
- 'packages/forest_admin_agent/forest_admin_agent.gemspec'
- 'packages/forest_admin_rails/forest_admin_rails.gemspec'
- 'packages/forest_admin_datasource_toolkit/forest_admin_datasource_toolkit.gemspec'
- 'packages/forest_admin_datasource_customizer/forest_admin_datasource_customizer.gemspec'
- 'packages/forest_admin_datasource_active_record/forest_admin_datasource_active_record.gemspec'

# Offense count: 1
Expand All @@ -33,6 +35,7 @@ Lint/PercentStringArray:
- 'packages/forest_admin_agent/forest_admin_agent.gemspec'
- 'packages/forest_admin_rails/forest_admin_rails.gemspec'
- 'packages/forest_admin_datasource_toolkit/forest_admin_datasource_toolkit.gemspec'
- 'packages/forest_admin_datasource_customizer/forest_admin_datasource_customizer.gemspec'
- 'packages/forest_admin_datasource_active_record/forest_admin_datasource_active_record.gemspec'

# Offense count: 1
Expand All @@ -55,6 +58,7 @@ Style/BlockComments:
- 'packages/forest_admin_agent/spec/spec_helper.rb'
- 'packages/forest_admin_rails/spec/spec_helper.rb'
- 'packages/forest_admin_datasource_toolkit/spec/spec_helper.rb'
- 'packages/forest_admin_datasource_customizer/spec/spec_helper.rb'
- 'packages/forest_admin_datasource_active_record/spec/spec_helper.rb'

# Offense count: 3
Expand All @@ -80,6 +84,7 @@ Style/MutableConstant:
- 'packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/schema_emitter.rb'
- 'packages/forest_admin_rails/lib/forest_admin_rails/version.rb'
- 'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/version.rb'
- 'packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/version.rb'
- 'packages/forest_admin_datasource_active_record/lib/forest_admin_datasource_active_record/version.rb'

# Offense count: 38
Expand Down Expand Up @@ -119,6 +124,13 @@ Style/StringLiterals:
- 'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit.rb'
- 'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/version.rb'
- 'packages/forest_admin_datasource_toolkit/spec/spec_helper.rb'
- 'packages/forest_admin_datasource_customizer/forest_admin_datasource_customizer.gemspec'
- 'packages/forest_admin_datasource_customizer/Gemfile'
- 'packages/forest_admin_datasource_customizer/Rakefile'
- 'packages/forest_admin_datasource_customizer/bin/console'
- 'packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer.rb'
- 'packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/version.rb'
- 'packages/forest_admin_datasource_customizer/spec/spec_helper.rb'
- 'packages/forest_admin_datasource_active_record/forest_admin_datasource_active_record.gemspec'
- 'packages/forest_admin_datasource_active_record/Gemfile'
- 'packages/forest_admin_datasource_active_record/Rakefile'
Expand Down Expand Up @@ -180,6 +192,7 @@ Metrics/ModuleLength:
- 'packages/forest_admin_agent/spec/**/*'
- 'packages/forest_admin_datasource_active_record/spec/**/*'
- 'packages/forest_admin_datasource_toolkit/spec/**/*'
- 'packages/forest_admin_datasource_customizer/spec/**/*'
- 'packages/forest_admin_rails/spec/**/*'

Metrics/MethodLength:
Expand Down Expand Up @@ -244,6 +257,7 @@ Layout/LineLength:
- 'packages/forest_admin_datasource_toolkit/spec/lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/comparisons_spec.rb'
- 'packages/forest_admin_datasource_toolkit/spec/lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern_spec.rb'
- 'packages/forest_admin_datasource_toolkit/spec/lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/times_spec.rb'
- 'packages/forest_admin_datasource_toolkit/spec/lib/forest_admin_datasource_toolkit/utils/collection_spec.rb'

RSpec/MultipleMemoizedHelpers:
Max: 10
5 changes: 0 additions & 5 deletions packages/forest_admin_agent/CHANGELOG.md

This file was deleted.

1 change: 1 addition & 0 deletions packages/forest_admin_agent/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ source "https://rubygems.org"

gemspec

gem 'forest_admin_datasource_customizer', path: '../forest_admin_datasource_customizer'
gem 'forest_admin_datasource_toolkit', path: '../forest_admin_datasource_toolkit'

group :development, :test do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@ class AgentFactory
def setup(options)
@options = options
@has_env_secret = options.to_h.key?(:env_secret)
@customizer = ForestAdminDatasourceToolkit::Datasource.new
@customizer = ForestAdminDatasourceCustomizer::DatasourceCustomizer.new
build_container
build_cache
build_logger
end

def add_datasource(datasource)
datasource.collections.each_value { |collection| @customizer.add_collection(collection) }
def add_datasource(datasource, options = {})
@customizer.add_datasource(datasource, options)

self
end

def customize_collection(name, handle)
@customizer.customize_collection(name, handle)
end

def build
@container.register(:datasource, @customizer)
@container.register(:datasource, @customizer.datasource)
send_schema
end

def send_schema(force: false)
return unless @has_env_secret

schema = ForestAdminAgent::Utils::Schema::SchemaEmitter.get_serialized_schema(@customizer)
schema = ForestAdminAgent::Utils::Schema::SchemaEmitter.get_serialized_schema(@customizer.datasource)
schema_is_know = @container.key?(:schema_file_hash) &&
@container.resolve(:schema_file_hash).get('value') == schema[:meta][:schemaFileHash]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def format_attributes(args)
record = args[:params][:data][:attributes]

args[:params][:data][:relationships]&.map do |field, value|
schema = @collection.fields[field]
schema = @collection.schema[:fields][field]

record[schema.foreign_key] = value['data'][schema.foreign_key_target] if schema.type == 'ManyToOne'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class AbstractRelatedRoute < AbstractAuthenticatedRoute
def build(args = {})
super

relation = @collection.fields[args[:params]['relation_name']]
@child_collection = @datasource.collection(relation.foreign_collection)
relation = @collection.schema[:fields][args[:params]['relation_name']]
@child_collection = @datasource.get_collection(relation.foreign_collection)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize

def build(args)
@datasource = ForestAdminAgent::Facades::Container.datasource
@collection = @datasource.collection(args[:params]['collection_name'])
@collection = @datasource.get_collection(args[:params]['collection_name'])
end

def routes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def make_line
end

def make_leaderboard
field = @collection.fields[@args[:params][:relationshipFieldName]]
field = @collection.schema[:fields][@args[:params][:relationshipFieldName]]

if field && field.type == 'OneToMany'
inverse = ForestAdminDatasourceToolkit::Utils::Collection.get_inverse_relation(
Expand Down Expand Up @@ -180,7 +180,7 @@ def make_leaderboard
end

if collection && filter && aggregation
rows = @datasource.collection(collection).aggregate(
rows = @datasource.get_collection(collection).aggregate(
@caller,
filter,
aggregation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def handle_request(args = {})
return {
name: args[:params]['collection_name'],
content: {
count: result[0][:value]
count: result.empty? ? 0 : result[0][:value]
}
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def associate_many_to_many(relation, parent_id, target_relation_id)
origin_value = Collection.get_value(@collection, @caller, parent_id, id)
record = { relation.origin_key => origin_value, relation.foreign_key => foreign_value }

through_collection = @datasource.collection(relation.through_collection)
through_collection = @datasource.get_collection(relation.through_collection)
through_collection.create(@caller, record)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def dissociate_or_delete_one_to_many(relation, relation_name, parent_id, is_dele
end

def dissociate_or_delete_many_to_many(relation, relation_name, parent_id, is_delete_mode, filter)
through_collection = @datasource.collection(relation.through_collection)
through_collection = @datasource.get_collection(relation.through_collection)

if is_delete_mode
# Generate filters _BEFORE_ deleting stuff, otherwise things break.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def handle_request(args = {})
build(args)
@permissions.can?(:edit, @collection)

relation = @collection.fields[args[:params]['relation_name']]
relation = @collection.schema[:fields][args[:params]['relation_name']]
parent_id = Utils::Id.unpack_id(@collection, args[:params]['id'])

linked_id = if (id = args.dig(:params, :data, :id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def handle_request(args = {})

def link_one_to_one_relations(args, record)
args[:params][:data][:relationships]&.map do |field, value|
schema = @collection.fields[field]
schema = @collection.schema[:fields][field]
next unless schema.type == 'OneToOne'

id = Utils::Id.unpack_id(@collection, value['data']['id'], with_key: true)
foreign_collection = @datasource.collection(schema.foreign_collection)
foreign_collection = @datasource.get_collection(schema.foreign_collection)
# Load the value that will be used as origin_key
origin_value = record[schema.origin_key_target]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def format_field(name, options)
end

def attributes
forest_collection = ForestAdminAgent::Facades::Container.datasource.collection(object.class.name.demodulize.underscore)
fields = forest_collection.fields.select { |_field_name, field| field.type == 'Column' }
forest_collection = ForestAdminAgent::Facades::Container.datasource.get_collection(object.class.name.demodulize.underscore)
fields = forest_collection.schema[:fields].select { |_field_name, field| field.type == 'Column' }
fields.each { |field_name, _field| add_attribute(field_name) }
return {} if attributes_map.nil?
attributes = {}
Expand Down Expand Up @@ -104,9 +104,9 @@ def add_to_many_association(name, options = {}, &block)
end

def relationships
forest_collection = ForestAdminAgent::Facades::Container.datasource.collection(object.class.name.demodulize.underscore)
relations_to_many = forest_collection.fields.select { |_field_name, field| field.type == 'OneToMany' || field.type == 'ManyToMany' }
relations_to_one = forest_collection.fields.select { |_field_name, field| field.type == 'OneToOne' || field.type == 'ManyToOne' }
forest_collection = ForestAdminAgent::Facades::Container.datasource.get_collection(object.class.name.demodulize.underscore)
relations_to_many = forest_collection.schema[:fields].select { |_field_name, field| field.type == 'OneToMany' || field.type == 'ManyToMany' }
relations_to_one = forest_collection.schema[:fields].select { |_field_name, field| field.type == 'OneToOne' || field.type == 'ManyToOne' }

relations_to_one.each { |field_name, _field| add_to_one_association(field_name) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.unpack_id(collection, packed_id, with_key: false)
end

result = primary_keys.map.with_index do |pk_name, index|
field = collection.fields[pk_name]
field = collection.schema[:fields][pk_name]
value = primary_key_values[index]
casted_value = field.column_type == 'Number' ? value.to_i : value
# TODO: call FieldValidator::validateValue($value, $field, $castedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def self.parse_projection(collection, args)
return ProjectionFactory.all(collection) unless fields != '' && !fields.nil?

fields = fields.split(',').map do |field_name|
column = collection.fields[field_name.strip]
column = collection.schema[:fields][field_name.strip]
column.type == 'Column' ? field_name.strip : "#{field_name.strip}:#{args[:params][:fields][field_name.strip]}"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.build_schema(collection)
fields: build_fields(collection),
icon: nil,
integration: nil,
isReadOnly: collection.fields.all? { |_k, field| field.type != 'Column' || field.is_read_only },
isReadOnly: collection.schema[:fields].all? { |_k, field| field.type != 'Column' || field.is_read_only },
isSearchable: true,
isVirtual: false,
name: collection.name,
Expand All @@ -21,7 +21,7 @@ def self.build_schema(collection)
end

def self.build_fields(collection)
fields = collection.fields.select do |name, _field|
fields = collection.schema[:fields].select do |name, _field|
!ForestAdminDatasourceToolkit::Utils::Schema.foreign_key?(collection, name) ||
ForestAdminDatasourceToolkit::Utils::Schema.primary_key?(collection, name)
end
Expand Down
Loading

0 comments on commit 900effd

Please sign in to comment.