forest_admin_datasource_active_record 1.0.0.pre.beta.22 → 1.0.0.pre.beta.24

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: e0e8066d5c92b8c29a1af80b8abf88f7f619eeb22c101a45aab964dd4b1c8d5c
4
+ data.tar.gz: bcdf8bbc3b7b45f0488317c56b9058b6becf7a1a93e7aa0f5609f7cbdf362ff0
5
5
  SHA512:
6
- metadata.gz: 2fc56c8fa822a558ccd33cf8bb94736efa135381c0bf146756fbd228e6d752b340209afa25e0c04bd1ef331911edd3869858a89902b2919a6155b529920da778
7
- data.tar.gz: 4517008faf5c70345836d1b0541c355b33a2c4d6076803106ece0d948b095b0af3ff094b615ff7c7c9ab75b21a6790d566387e182bdc550cee30b56e3b119557
6
+ metadata.gz: 6369edf2a171a44f5ae77474531cbe2d7943b1d12253e423dfc5353e01daacfa22a3f5870d5cc732624329f81c814115056be130c3b2a694bd7f11eaf7449eee
7
+ data.tar.gz: 8a91c54e1aecc09faa1dfe38dae73fb40849d73093ad46c99d0b74ea47008ca28a2053147367917a8931fd57364b659caa3f553900f52ffd9aa2569638ca4a89
@@ -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
@@ -12,6 +12,7 @@ module ForestAdminDatasourceActiveRecord
12
12
  super(datasource, name)
13
13
  fetch_fields
14
14
  fetch_associations
15
+ enable_count
15
16
  end
16
17
 
17
18
  def list(_caller, filter, projection)
@@ -20,15 +21,8 @@ module ForestAdminDatasourceActiveRecord
20
21
  query.all
21
22
  end
22
23
 
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
- ]
24
+ def aggregate(_caller, filter, aggregation, limit = nil)
25
+ Utils::QueryAggregate.new(self, aggregation, filter, limit).get
32
26
  end
33
27
 
34
28
  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
- 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
78
+ relation_schema = @collection.schema[:fields][relation]
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))
@@ -105,8 +116,8 @@ module ForestAdminDatasourceActiveRecord
105
116
  def format_field(field)
106
117
  if field.include?(':')
107
118
  relation_name, field = field.split(':')
108
- relation = @collection.fields[relation_name]
109
- table_name = @collection.datasource.collection(relation.foreign_collection).model.table_name
119
+ relation = @collection.schema[:fields][relation_name]
120
+ table_name = @collection.datasource.get_collection(relation.foreign_collection).model.table_name
110
121
  add_join_relation(relation, relation_name)
111
122
  return "#{table_name}.#{field}"
112
123
  end
@@ -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.24"
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.24
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-18 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