forest_admin_datasource_active_record 1.0.0.pre.beta.106 → 1.0.0.pre.beta.107

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 260198adb60c223c38c2fc1b164f5033e95fa5922505d88af291562ae4c742dd
4
- data.tar.gz: '038e7548441ad9bd25324ac29e8a1021cf1193713897ea5a8369c351c3e7a7a5'
3
+ metadata.gz: abb04f3fb4734ac41643b955a53195d35ab9dcf97a4a49265b7fc73b740f1c07
4
+ data.tar.gz: f70f859ea4767f4156f65801f484bc2e40af4be1784e8adee1699958d7da4128
5
5
  SHA512:
6
- metadata.gz: f3954178af0384bf82c02f4ecaa535cf43adfc6ff61a41cbcdf9e58bc6cd53e960dd6e24a165b21e8f588d5bd1d15855340000fe11a4f0039fdb2e790964d46d
7
- data.tar.gz: ea21a53f9a647bea528908f0c5cb1e66142df95f0459ca2834e89fc4db3a858b4824aa28d8364e591b492cb75239207e3101c6ad3db447eb04dc721ef1a1ee7f
6
+ metadata.gz: '0428ba9a581fd5b81951623b62bca50897ad8a9dcf144faa3f31ef282f06a29a326b974761e0bc35c3e2ce350a6dc5e2d7c94dec84916dab230905319bac1738'
7
+ data.tar.gz: 825302b4bf80296a399661b333f969a073056387da087c653c5c2557f6d2b8b65eb5f82fd7a184292f73af3409dd0c0cee6835671ee47cfba39809296db2bedd
@@ -5,7 +5,7 @@ module ForestAdminDatasourceActiveRecord
5
5
  model.reflect_on_all_associations.select do |association|
6
6
  is_valid_association = !get_class(association).nil? && !active_type?(get_class(association))
7
7
  if support_polymorphic_relations
8
- polymorphic?(association) ? true : is_valid_association
8
+ polymorphic?(association) || is_valid_association
9
9
  else
10
10
  !polymorphic?(association) && is_valid_association
11
11
  end
@@ -5,6 +5,7 @@ module ForestAdminDatasourceActiveRecord
5
5
  include ForestAdminDatasourceToolkit::Components::Query
6
6
 
7
7
  def initialize(collection, aggregation, filter = nil, limit = nil)
8
+ filter ||= Filter.new
8
9
  super(collection, ForestAdminDatasourceToolkit::Components::Query::Projection.new, filter)
9
10
  @aggregation = aggregation
10
11
  @limit = limit
@@ -13,12 +14,16 @@ module ForestAdminDatasourceActiveRecord
13
14
  end
14
15
 
15
16
  def get
17
+ build_select(@collection, @projection)
18
+ apply_filter
19
+
16
20
  group_fields = []
17
21
  @aggregation.groups.each do |group|
18
22
  field = format_field(group[:field])
19
23
  if group[:operation]
20
- @select << "DATE_TRUNC('#{group[:operation].downcase}', #{field}) AS \"#{group[:field]}\""
21
- group_fields << "DATE_TRUNC('#{group[:operation].downcase}', #{field})"
24
+ date_trunc_expression = date_trunc_sql(group[:operation], field)
25
+ @select << "#{date_trunc_expression} AS \"#{group[:field]}\""
26
+ group_fields << date_trunc_expression
22
27
  else
23
28
  @select << "#{field} AS \"#{group[:field]}\""
24
29
  group_fields << field
@@ -29,7 +34,7 @@ module ForestAdminDatasourceActiveRecord
29
34
  @query = @query.order("#{@operation} DESC")
30
35
  @query = @query.limit(@limit) if @limit
31
36
  @query = @query.group(group_fields.join(','))
32
- build
37
+ apply_select
33
38
 
34
39
  compute_result_aggregate(@query)
35
40
  end
@@ -50,6 +55,74 @@ module ForestAdminDatasourceActiveRecord
50
55
 
51
56
  @query
52
57
  end
58
+
59
+ private
60
+
61
+ def date_trunc_sql(operation, field)
62
+ adapter_name = @collection.model.connection.adapter_name.downcase
63
+ operation = operation.downcase
64
+
65
+ case adapter_name
66
+ when 'postgresql'
67
+ "DATE_TRUNC('#{operation}', #{field})"
68
+ when 'mysql2', 'mysql'
69
+ mysql_date_trunc(operation, field)
70
+ when 'sqlite3', 'sqlite'
71
+ sqlite_date_trunc(operation, field)
72
+ else
73
+ raise ArgumentError, "Unsupported database adapter '#{adapter_name}' for date truncation"
74
+ end
75
+ end
76
+
77
+ # rubocop:disable Layout/LineLength
78
+ def mysql_date_trunc(operation, field)
79
+ case operation
80
+ when 'year'
81
+ "DATE_FORMAT(#{field}, '%Y-01-01 00:00:00')"
82
+ when 'quarter'
83
+ "DATE_FORMAT(#{field}, CONCAT(YEAR(#{field}), '-', LPAD((QUARTER(#{field}) - 1) * 3 + 1, 2, '0'), '-01 00:00:00'))"
84
+ when 'month'
85
+ "DATE_FORMAT(#{field}, '%Y-%m-01 00:00:00')"
86
+ when 'week'
87
+ "DATE_SUB(#{field}, INTERVAL WEEKDAY(#{field}) DAY)"
88
+ when 'day'
89
+ "DATE(#{field})"
90
+ when 'hour'
91
+ "DATE_FORMAT(#{field}, '%Y-%m-%d %H:00:00')"
92
+ when 'minute'
93
+ "DATE_FORMAT(#{field}, '%Y-%m-%d %H:%i:00')"
94
+ when 'second'
95
+ "DATE_FORMAT(#{field}, '%Y-%m-%d %H:%i:%s')"
96
+ else
97
+ raise ArgumentError, "Unsupported date truncation operation '#{operation}' for MySQL"
98
+ end
99
+ end
100
+ # rubocop:enable Layout/LineLength
101
+
102
+ # rubocop:disable Layout/LineLength
103
+ def sqlite_date_trunc(operation, field)
104
+ case operation
105
+ when 'year'
106
+ "strftime('%Y-01-01 00:00:00', #{field}, 'localtime')"
107
+ when 'quarter'
108
+ "strftime('%Y-', #{field}, 'localtime') || printf('%02d', ((CAST(strftime('%m', #{field}, 'localtime') AS INTEGER) - 1) / 3) * 3 + 1) || '-01 00:00:00'"
109
+ when 'month'
110
+ "strftime('%Y-%m-01 00:00:00', #{field}, 'localtime')"
111
+ when 'week'
112
+ "datetime(#{field}, 'localtime', 'weekday 0', '-6 days')"
113
+ when 'day'
114
+ "strftime('%Y-%m-%d 00:00:00', #{field}, 'localtime')"
115
+ when 'hour'
116
+ "strftime('%Y-%m-%d %H:00:00', #{field}, 'localtime')"
117
+ when 'minute'
118
+ "strftime('%Y-%m-%d %H:%M:00', #{field}, 'localtime')"
119
+ when 'second'
120
+ "strftime('%Y-%m-%d %H:%M:%S', #{field}, 'localtime')"
121
+ else
122
+ raise ArgumentError, "Unsupported date truncation operation '#{operation}' for SQLite"
123
+ end
124
+ end
125
+ # rubocop:enable Layout/LineLength
53
126
  end
54
127
  end
55
128
  end
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceActiveRecord
2
- VERSION = "1.0.0-beta.106"
2
+ VERSION = "1.0.0-beta.107"
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.106
4
+ version: 1.0.0.pre.beta.107
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: 2025-06-12 00:00:00.000000000 Z
12
+ date: 2025-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord