forest_admin_datasource_active_record 1.0.0.pre.beta.22 → 1.0.0.pre.beta.23

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c70063e15deac4061f339ad3c5bba3c57b7c4876ec06a302562d717e4b5206d
4
- data.tar.gz: f2cc11652a6616cf11f88dfa55a7b1c08597f4f0cbdfb993f622b6ac9733362c
3
+ metadata.gz: 51b8d4407b45292ae0e56ff578709bec39e08d678e6b47a1fbc24d719f2f63f3
4
+ data.tar.gz: d56d1f01095082675496cab77136c0ff076f1159d2f8399494d9ce168e361337
5
5
  SHA512:
6
- metadata.gz: 2fc56c8fa822a558ccd33cf8bb94736efa135381c0bf146756fbd228e6d752b340209afa25e0c04bd1ef331911edd3869858a89902b2919a6155b529920da778
7
- data.tar.gz: 4517008faf5c70345836d1b0541c355b33a2c4d6076803106ece0d948b095b0af3ff094b615ff7c7c9ab75b21a6790d566387e182bdc550cee30b56e3b119557
6
+ metadata.gz: ea8ce626732ebaad40be56a418e45367c7c51093ad19059b2c5f24ca9a0d36f8879b4a998b07f41e903c8e2491ba2d42d2cfd0998cae9a1bf24bc31c8b8fd762
7
+ data.tar.gz: dbb0fac6f26019beb6802c0f254e6b919ad08e7e8bbb0026aaca500c22ca92f1bd0101ab2da33d0b37315f155c6ede874f6fb420ec8f684aa936bf53bfa64e85
@@ -33,5 +33,6 @@ admin work on any Ruby application."
33
33
  spec.require_paths = ["lib"]
34
34
 
35
35
  spec.add_dependency "activerecord", ">= 6.1"
36
+ spec.add_dependency "activesupport", ">= 6.1"
36
37
  spec.add_dependency "zeitwerk", "~> 2.3"
37
38
  end
@@ -20,15 +20,8 @@ module ForestAdminDatasourceActiveRecord
20
20
  query.all
21
21
  end
22
22
 
23
- def aggregate(_caller, _filter, aggregation, _limit = nil)
24
- field = aggregation.field || '*'
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
- @query = select
16
- @query = apply_filter
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 select
74
+ def build_select
75
75
  unless @projection.nil?
76
- query_select = @projection.columns.map { |field| "#{@collection.model.table_name}.#{field}" }
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
- query_select.push("#{@collection.model.table_name}.#{relation_schema.origin_key_target}")
81
- else
82
- query_select.push("#{@collection.model.table_name}.#{relation_schema.foreign_key}")
83
- end
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
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceActiveRecord
2
- VERSION = "1.0.0-beta.22"
2
+ VERSION = "1.0.0-beta.23"
3
3
  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.22
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-08 00:00:00.000000000 Z
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