forest_admin_datasource_active_record 1.0.0.pre.beta.22 → 1.0.0.pre.beta.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/forest_admin_datasource_active_record.gemspec +1 -0
- data/lib/forest_admin_datasource_active_record/collection.rb +2 -9
- data/lib/forest_admin_datasource_active_record/utils/query.rb +23 -12
- data/lib/forest_admin_datasource_active_record/utils/query_aggregate.rb +49 -0
- data/lib/forest_admin_datasource_active_record/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51b8d4407b45292ae0e56ff578709bec39e08d678e6b47a1fbc24d719f2f63f3
|
4
|
+
data.tar.gz: d56d1f01095082675496cab77136c0ff076f1159d2f8399494d9ce168e361337
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea8ce626732ebaad40be56a418e45367c7c51093ad19059b2c5f24ca9a0d36f8879b4a998b07f41e903c8e2491ba2d42d2cfd0998cae9a1bf24bc31c8b8fd762
|
7
|
+
data.tar.gz: dbb0fac6f26019beb6802c0f254e6b919ad08e7e8bbb0026aaca500c22ca92f1bd0101ab2da33d0b37315f155c6ede874f6fb420ec8f684aa936bf53bfa64e85
|
@@ -20,15 +20,8 @@ module ForestAdminDatasourceActiveRecord
|
|
20
20
|
query.all
|
21
21
|
end
|
22
22
|
|
23
|
-
def aggregate(_caller,
|
24
|
-
|
25
|
-
|
26
|
-
[
|
27
|
-
{
|
28
|
-
value: @model.send(aggregation.operation.downcase, field),
|
29
|
-
group: []
|
30
|
-
}
|
31
|
-
]
|
23
|
+
def aggregate(_caller, filter, aggregation, limit = nil)
|
24
|
+
Utils::QueryAggregate.new(self, aggregation, filter, limit).get
|
32
25
|
end
|
33
26
|
|
34
27
|
def create(_caller, data)
|
@@ -9,13 +9,13 @@ module ForestAdminDatasourceActiveRecord
|
|
9
9
|
@projection = projection
|
10
10
|
@filter = filter
|
11
11
|
@arel_table = @collection.model.arel_table
|
12
|
+
@select = []
|
12
13
|
end
|
13
14
|
|
14
15
|
def build
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
@query
|
16
|
+
build_select
|
17
|
+
apply_filter
|
18
|
+
apply_select
|
19
19
|
end
|
20
20
|
|
21
21
|
def apply_filter
|
@@ -71,19 +71,30 @@ module ForestAdminDatasourceActiveRecord
|
|
71
71
|
@query
|
72
72
|
end
|
73
73
|
|
74
|
-
def
|
74
|
+
def build_select
|
75
75
|
unless @projection.nil?
|
76
|
-
|
76
|
+
@select += @projection.columns.map { |field| "#{@collection.model.table_name}.#{field}" }
|
77
77
|
@projection.relations.each_key do |relation|
|
78
78
|
relation_schema = @collection.fields[relation]
|
79
|
-
if relation_schema.type == 'OneToOne'
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
79
|
+
@select << if relation_schema.type == 'OneToOne'
|
80
|
+
"#{@collection.model.table_name}.#{relation_schema.origin_key_target}"
|
81
|
+
else
|
82
|
+
"#{@collection.model.table_name}.#{relation_schema.foreign_key}"
|
83
|
+
end
|
84
84
|
end
|
85
85
|
|
86
|
-
@query = @query.select(query_select.join(', '))
|
86
|
+
# @query = @query.select(query_select.join(', '))
|
87
|
+
# @query = @query.eager_load(@projection.relations.keys.map(&:to_sym))
|
88
|
+
# # TODO: replace eager_load by joins because eager_load select ALL columns of relation
|
89
|
+
# # @query = @query.joins(@projection.relations.keys.map(&:to_sym))
|
90
|
+
end
|
91
|
+
|
92
|
+
@query
|
93
|
+
end
|
94
|
+
|
95
|
+
def apply_select
|
96
|
+
unless @projection.nil?
|
97
|
+
@query = @query.select(@select.join(', '))
|
87
98
|
@query = @query.eager_load(@projection.relations.keys.map(&:to_sym))
|
88
99
|
# TODO: replace eager_load by joins because eager_load select ALL columns of relation
|
89
100
|
# @query = @query.joins(@projection.relations.keys.map(&:to_sym))
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ForestAdminDatasourceActiveRecord
|
2
|
+
module Utils
|
3
|
+
class QueryAggregate < Query
|
4
|
+
include ForestAdminDatasourceToolkit::Components::Query::ConditionTree
|
5
|
+
include ForestAdminDatasourceToolkit::Components::Query
|
6
|
+
|
7
|
+
def initialize(collection, aggregation, filter = nil, limit = nil)
|
8
|
+
super(collection, ForestAdminDatasourceToolkit::Components::Query::Projection.new, filter)
|
9
|
+
@aggregation = aggregation
|
10
|
+
@limit = limit
|
11
|
+
@operation = aggregation.operation.downcase
|
12
|
+
@field = aggregation.field.nil? ? '*' : format_field(aggregation.field)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get
|
16
|
+
group_fields = []
|
17
|
+
@aggregation.groups.each do |group|
|
18
|
+
field = format_field(group[:field])
|
19
|
+
if group[:operation]
|
20
|
+
@select << "DATE_TRUNC('#{group[:operation].downcase}', #{field}) AS \"#{group[:field]}\""
|
21
|
+
group_fields << "DATE_TRUNC('#{group[:operation].downcase}', #{field})"
|
22
|
+
else
|
23
|
+
@select << "#{field} AS \"#{group[:field]}\""
|
24
|
+
group_fields << field
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
@select << "#{@operation}(#{@field}) AS #{@operation}"
|
29
|
+
@query = @query.order("#{@operation} DESC")
|
30
|
+
@query = @query.limit(@limit) if @limit
|
31
|
+
@query = @query.group(group_fields.join(','))
|
32
|
+
build
|
33
|
+
|
34
|
+
compute_result_aggregate(@query)
|
35
|
+
end
|
36
|
+
|
37
|
+
def compute_result_aggregate(rows)
|
38
|
+
rows.map do |row|
|
39
|
+
{
|
40
|
+
value: row.send(@operation.to_sym),
|
41
|
+
group: @aggregation.groups.each_with_object({}) do |group, memo|
|
42
|
+
memo[group[:field]] = row.send(group[:field].to_sym)
|
43
|
+
end
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forest_admin_datasource_active_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.pre.beta.
|
4
|
+
version: 1.0.0.pre.beta.23
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthieu
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-12-
|
12
|
+
date: 2023-12-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -25,6 +25,20 @@ dependencies:
|
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '6.1'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: activesupport
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '6.1'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '6.1'
|
28
42
|
- !ruby/object:Gem::Dependency
|
29
43
|
name: zeitwerk
|
30
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,6 +74,7 @@ files:
|
|
60
74
|
- lib/forest_admin_datasource_active_record/parser/relation.rb
|
61
75
|
- lib/forest_admin_datasource_active_record/parser/validation.rb
|
62
76
|
- lib/forest_admin_datasource_active_record/utils/query.rb
|
77
|
+
- lib/forest_admin_datasource_active_record/utils/query_aggregate.rb
|
63
78
|
- lib/forest_admin_datasource_active_record/version.rb
|
64
79
|
- sig/forest_admin_datasource_active_record.rbs
|
65
80
|
- sig/forest_admin_datasource_active_record/collection.rbs
|