Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -262,6 +262,7 @@ Layout/LineLength:
- 'packages/forest_admin_agent/lib/forest_admin_agent/routes/resources/list.rb'
- 'packages/forest_admin_agent/lib/forest_admin_agent/services/permissions.rb'
- 'packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/relation/relation_collection_decorator.rb'
- 'packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/sort/sort_collection_decorator.rb'
- 'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/components/query/condition_tree/condition_tree_factory.rb'
- 'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/utils/collection.rb'
- 'packages/forest_admin_datasource_toolkit/lib/forest_admin_datasource_toolkit/components/query/filter_factory.rb'
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -155,6 +155,31 @@ def add_field_validation(name, operator, value = nil)
end
end

# Enable sorting on a specific field using emulation.
# As for all the emulation method, the field sorting will be done in-memory.
# @param name the name of the field to enable emulation on
# @example
# .emulate_field_sorting('fullName')
def emulate_field_sorting(name)
push_customization { @stack.sort.get_collection(@name).emulate_field_sorting(name) }
end

# Replace an implementation for the sorting.
# The field sorting will be done by the datasource.
# @param name the name of the field to enable sort
# @param equivalent_sort the sort equivalent
# @example
# .replace_field_sorting(
# 'fullName',
# [
# { field: 'firstName', ascending: true },
# { field: 'lastName', ascending: true },
# ]
# )
def replace_field_sorting(name, equivalent_sort)
push_customization { @stack.sort.get_collection(@name).replace_field_sorting(name, equivalent_sort) }
end

private

def push_customization(&customization)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@ class DecoratorsStack
include ForestAdminDatasourceToolkit::Decorators

attr_reader :datasource, :schema, :search, :early_computed, :late_computed, :action, :relation, :late_op_emulate,
:early_op_emulate, :validation
:early_op_emulate, :validation, :sort

def initialize(datasource)
@customizations = []
Expand All@@ -22,6 +22,7 @@ def initialize(datasource)
last = DatasourceDecorator.new(last, OperatorsEquivalence::OperatorsEquivalenceCollectionDecorator)

last = @search = DatasourceDecorator.new(last, Search::SearchCollectionDecorator)
last = @sort = DatasourceDecorator.new(last, Sort::SortCollectionDecorator)
last = @action = DatasourceDecorator.new(last, Action::ActionCollectionDecorator)
last = @schema = DatasourceDecorator.new(last, Schema::SchemaCollectionDecorator)
last = @validation = DatasourceDecorator.new(last, Validation::ValidationCollectionDecorator)
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
module ForestAdminDatasourceCustomizer
module Decorators
module Sort
class SortCollectionDecorator < ForestAdminDatasourceToolkit::Decorators::CollectionDecorator
include ForestAdminDatasourceToolkit::Exceptions
include ForestAdminDatasourceToolkit::Validations
include ForestAdminDatasourceToolkit::Components::Query
include ForestAdminDatasourceToolkit::Utils

attr_reader :sorts

def initialize(child_collection, datasource)
super
@sorts = {}
end

def emulate_field_sorting(name)
replace_or_emulate_field_sorting(name, nil)
end

def replace_field_sorting(name, equivalent_sort)
if equivalent_sort.nil?
raise ForestException, 'A new sorting method should be provided to replace field sorting'
end

replace_or_emulate_field_sorting(name, equivalent_sort)
end

def list(caller, filter = nil, projection = nil)
child_filter = filter.override(sort: filter.sort&.replace_clauses do |clause|
rewrite_plain_sort_clause(clause)
end)

if child_filter.sort.nil? || child_filter.sort.none? { |clause| emulated?(clause[:field]) }
return child_collection.list(caller, child_filter, projection)
end

# Fetch the whole collection, but only with the fields we need to sort
reference_records = child_collection.list(caller, child_filter.override(sort: nil, page: nil),
child_filter.sort.projection.with_pks(self))
reference_records = child_filter.sort.apply(reference_records)
reference_records = child_filter.page.apply(reference_records) if child_filter.page

# We now have the information we need to sort by the field
new_filter = Filter.new(condition_tree: ConditionTree::ConditionTreeFactory.match_records(schema,
reference_records))

records = child_collection.list(caller, new_filter, projection.with_pks(self))
records = sort_records(reference_records, records)

projection.apply(records)
end

def refine_schema(child_schema)
child_schema[:fields].each do |name, schema|
if schema.type == 'Column'
schema.is_sortable = true if @sorts[name].nil?
child_schema[:fields][name] = schema
end
end

child_schema
end

def rewrite_plain_sort_clause(clause)
# Order by is targeting a field on another collection => recurse.
if clause[:field].include?(':')
prefix = clause[:field].split(':')[0]
schema = self.schema[:fields][prefix]
association = datasource.get_collection(schema.foreign_collection)

return ForestAdminDatasourceToolkit::Components::Query::Sort.new([clause])
.unnest
.replace_clauses { |sub_clause| association.rewrite_plain_sort_clause(sub_clause) }
.nest(prefix)
end

# Field that we own: recursively replace using equivalent sort
equivalent_sort = @sorts[clause[:field]]

if equivalent_sort
equivalent_sort = equivalent_sort.inverse unless clause[:ascending]

return equivalent_sort.replace_clauses { |sub_clause| rewrite_plain_sort_clause(sub_clause) }
end

ForestAdminDatasourceToolkit::Components::Query::Sort.new([clause])
end

def emulated?(path)
index = path.index(':')
return @sorts[path] if index.nil?

foreign_collection = schema[:fields][path[0, index]].foreign_collection
association = datasource.get_collection(foreign_collection)

association.emulated?(path[index + 1, path.length - index - 1])
end

private

def replace_or_emulate_field_sorting(name, equivalent_sort)
FieldValidator.validate(self, name)
@sorts[name] =
equivalent_sort ? ForestAdminDatasourceToolkit::Components::Query::Sort.new(equivalent_sort) : nil
mark_schema_as_dirty
end

def sort_records(reference_records, records)
position_by_id = {}
sorted = Array.new(records.length)

reference_records.each_with_index do |record, index|
position_by_id[Record.primary_keys(schema, record).join('|')] = index
end

records.each do |record|
id = Record.primary_keys(schema, record).join('|')
sorted[position_by_id[id]] = record
end

sorted
end
end
end
end
end
Original file line numberDiff line numberDiff line change
Expand Up@@ -67,7 +67,7 @@ module ForestAdminDatasourceCustomizer
schema: {
fields: {
'id' => ColumnSchema.new(column_type: 'Number', is_primary_key: true, filter_operators: [Operators::EQUAL, Operators::IN]),
'name' => ColumnSchema.new(column_type: 'String'),
'name' => ColumnSchema.new(column_type: 'String', is_sortable: true),
'name_in_read_only' => ColumnSchema.new(column_type: 'String', is_read_only: true),
'book' => Relations::OneToOneSchema.new(
origin_key: 'author_id',
Expand DownExpand Up@@ -266,6 +266,14 @@ module ForestAdminDatasourceCustomizer
expect(relation_collection.relations['myBooks'].origin_key).to eq('person_id')
expect(relation_collection.relations['myBooks'].origin_key_target).to eq('id')
end

it 'does not allow replaceFieldSorting' do
customizer = described_class.new(@datasource_customizer, @datasource_customizer.stack, 'person')
customizer.add_one_to_one_relation('myBookAuthor', 'book_person', { origin_key: 'person_id', origin_key_target: 'id' })
customizer.replace_field_sorting('myBookAuthor', [])

expect { @datasource_customizer.datasource({}) }.to raise_error(Exceptions::ValidationError, "🌳🌳🌳 Unexpected field type: 'person.myBookAuthor' (found 'OneToOne' expected 'Column')")
end
end

context 'when adding external relation' do
Expand DownExpand Up@@ -353,5 +361,38 @@ module ForestAdminDatasourceCustomizer
expect(op_emulate_collection.fields['title']).to eq({ Operators::PRESENT => replacer })
end
end

context 'when using emulate_field_sorting' do
it 'emulate sort on field' do
stack = @datasource_customizer.stack
allow(stack.sort).to receive(:get_collection).with('person').and_return(@datasource_customizer.stack.sort.get_collection('person'))

customizer = described_class.new(@datasource_customizer, @datasource_customizer.stack, 'person')
customizer.emulate_field_sorting('name')
@datasource_customizer.datasource({})

sort_collection = @datasource_customizer.stack.sort.get_collection('person')

expect(sort_collection.sorts).to have_key('name')
expect(sort_collection.emulated?('name')).to be_nil
end
end

context 'when using replace_field_sorting' do
it 'replace sort on field' do
stack = @datasource_customizer.stack
allow(stack.sort).to receive(:get_collection).with('person').and_return(@datasource_customizer.stack.sort.get_collection('person'))

customizer = described_class.new(@datasource_customizer, @datasource_customizer.stack, 'person')
sort_clauses = [{ field: 'name', ascending: true }]
customizer.replace_field_sorting('name', sort_clauses)
@datasource_customizer.datasource({})

sort_collection = @datasource_customizer.stack.sort.get_collection('person')

expect(sort_collection.sorts).to have_key('name')
expect(sort_collection.sorts['name']).to eq(ForestAdminDatasourceToolkit::Components::Query::Sort.new(sort_clauses))
end
end
end
end
Original file line numberDiff line numberDiff line change
Expand Up@@ -561,13 +561,13 @@ module Relation
it 'replaces sorts in emulated many to one into sort by fk' do
ascending = @datasource_decorator.get_collection('passport').list(
caller,
Filter.new(sort: Sort.new([{ field: 'owner:name', ascending: true }])),
Filter.new(sort: ForestAdminDatasourceToolkit::Components::Query::Sort.new([{ field: 'owner:name', ascending: true }])),
Projection.new(%w[id owner_id owner:name])
)

descending = @datasource_decorator.get_collection('passport').list(
caller,
Filter.new(sort: Sort.new([{ field: 'owner:name', ascending: false }])),
Filter.new(sort: ForestAdminDatasourceToolkit::Components::Query::Sort.new([{ field: 'owner:name', ascending: false }])),
Projection.new(%w[id owner_id owner:name])
)

Expand Down
Loading