forest_admin_datasource_active_record 1.0.0.pre.beta.59 → 1.0.0.pre.beta.61
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/lib/forest_admin_datasource_active_record/collection.rb +60 -34
- data/lib/forest_admin_datasource_active_record/datasource.rb +10 -1
- data/lib/forest_admin_datasource_active_record/utils/active_record_serializer.rb +9 -5
- data/lib/forest_admin_datasource_active_record/utils/query.rb +12 -1
- data/lib/forest_admin_datasource_active_record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9ce7a6a32ae3e985c64e2c9458165de5d24e8d7a532ca6a405aac048ef5ede8
|
4
|
+
data.tar.gz: 65ba39d8d70775cbb98e4b0f0d85888a5946fda8e34bd88bb8e7e3b52004fe2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a1fcf17fd734b4275c86f82ac5afb4ff03bb4ac5a52b556b2df3ef1407f38076b37c94d650923711e0fe56fb63455e80ad1713c403592a93501b54e088b595b
|
7
|
+
data.tar.gz: 112af29e8fab7793768607f64e5f51c97183623cf461f8fcd24f332267d40d5bb62b04e2cc73115e2c0b7f10f239a1d97cc82023cf09646d78de40c5c0cfaff2
|
@@ -22,7 +22,7 @@ module ForestAdminDatasourceActiveRecord
|
|
22
22
|
def list(_caller, filter, projection)
|
23
23
|
query = Utils::Query.new(self, projection, filter)
|
24
24
|
|
25
|
-
query.get.map { |record| Utils::ActiveRecordSerializer.new(record).to_hash }
|
25
|
+
query.get.map { |record| Utils::ActiveRecordSerializer.new(record).to_hash(projection) }
|
26
26
|
end
|
27
27
|
|
28
28
|
def aggregate(_caller, filter, aggregation, limit = nil)
|
@@ -30,7 +30,7 @@ module ForestAdminDatasourceActiveRecord
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def create(_caller, data)
|
33
|
-
Utils::ActiveRecordSerializer.new(@model.create(data)).to_hash
|
33
|
+
Utils::ActiveRecordSerializer.new(@model.create(data)).to_hash(ProjectionFactory.all(self))
|
34
34
|
end
|
35
35
|
|
36
36
|
def update(_caller, filter, data)
|
@@ -63,49 +63,75 @@ module ForestAdminDatasourceActiveRecord
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
def association_primary_key?(association)
|
67
|
+
!association.association_primary_key.empty?
|
68
|
+
rescue StandardError
|
69
|
+
false
|
70
|
+
end
|
71
|
+
|
66
72
|
def fetch_associations
|
67
73
|
associations(@model).each do |association|
|
68
74
|
case association.macro
|
69
75
|
when :has_one
|
70
|
-
|
71
|
-
association.
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
76
|
+
if association_primary_key?(association)
|
77
|
+
if association.through_reflection?
|
78
|
+
add_field(
|
79
|
+
association.name.to_s,
|
80
|
+
ForestAdminDatasourceToolkit::Schema::Relations::ManyToManySchema.new(
|
81
|
+
foreign_collection: association.class_name.demodulize.underscore,
|
82
|
+
origin_key: association.through_reflection.foreign_key,
|
83
|
+
origin_key_target: association.through_reflection.join_foreign_key,
|
84
|
+
foreign_key: association.join_foreign_key,
|
85
|
+
foreign_key_target: association.association_primary_key,
|
86
|
+
through_collection: association.through_reflection.class_name.demodulize.underscore
|
87
|
+
)
|
88
|
+
)
|
89
|
+
else
|
90
|
+
add_field(
|
91
|
+
association.name.to_s,
|
92
|
+
ForestAdminDatasourceToolkit::Schema::Relations::OneToOneSchema.new(
|
93
|
+
foreign_collection: association.class_name.demodulize.underscore,
|
94
|
+
origin_key: association.foreign_key,
|
95
|
+
origin_key_target: association.association_primary_key
|
96
|
+
)
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end
|
78
100
|
when :belongs_to
|
79
|
-
|
80
|
-
association.name.to_s,
|
81
|
-
ForestAdminDatasourceToolkit::Schema::Relations::ManyToOneSchema.new(
|
82
|
-
foreign_collection: association.class_name.demodulize.underscore,
|
83
|
-
foreign_key: association.foreign_key,
|
84
|
-
foreign_key_target: association.association_primary_key
|
85
|
-
)
|
86
|
-
)
|
87
|
-
when :has_many
|
88
|
-
if association.through_reflection?
|
101
|
+
if association_primary_key?(association)
|
89
102
|
add_field(
|
90
103
|
association.name.to_s,
|
91
|
-
ForestAdminDatasourceToolkit::Schema::Relations::
|
104
|
+
ForestAdminDatasourceToolkit::Schema::Relations::ManyToOneSchema.new(
|
92
105
|
foreign_collection: association.class_name.demodulize.underscore,
|
93
|
-
|
94
|
-
|
95
|
-
foreign_key: association.join_foreign_key,
|
96
|
-
foreign_key_target: association.association_primary_key,
|
97
|
-
through_collection: association.through_reflection.class_name.demodulize.underscore
|
106
|
+
foreign_key: association.foreign_key,
|
107
|
+
foreign_key_target: association.association_primary_key
|
98
108
|
)
|
99
109
|
)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
110
|
+
end
|
111
|
+
when :has_many
|
112
|
+
if association_primary_key?(association)
|
113
|
+
if association.through_reflection?
|
114
|
+
add_field(
|
115
|
+
association.name.to_s,
|
116
|
+
ForestAdminDatasourceToolkit::Schema::Relations::ManyToManySchema.new(
|
117
|
+
foreign_collection: association.class_name.demodulize.underscore,
|
118
|
+
origin_key: association.through_reflection.foreign_key,
|
119
|
+
origin_key_target: association.through_reflection.join_foreign_key,
|
120
|
+
foreign_key: association.join_foreign_key,
|
121
|
+
foreign_key_target: association.association_primary_key,
|
122
|
+
through_collection: association.through_reflection.class_name.demodulize.underscore
|
123
|
+
)
|
107
124
|
)
|
108
|
-
|
125
|
+
else
|
126
|
+
add_field(
|
127
|
+
association.name.to_s,
|
128
|
+
ForestAdminDatasourceToolkit::Schema::Relations::OneToManySchema.new(
|
129
|
+
foreign_collection: association.class_name.demodulize.underscore,
|
130
|
+
origin_key: association.foreign_key,
|
131
|
+
origin_key_target: association.association_primary_key
|
132
|
+
)
|
133
|
+
)
|
134
|
+
end
|
109
135
|
end
|
110
136
|
end
|
111
137
|
end
|
@@ -19,11 +19,20 @@ module ForestAdminDatasourceActiveRecord
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
def primary_key?(model)
|
23
|
+
!model.primary_key.empty?
|
24
|
+
rescue StandardError
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
22
28
|
def fetch_model(model)
|
23
29
|
if model.name.start_with?('HABTM_')
|
24
30
|
build_habtm(model)
|
25
31
|
else
|
26
|
-
@models << model unless model.abstract_class? ||
|
32
|
+
@models << model unless model.abstract_class? ||
|
33
|
+
@models.include?(model) ||
|
34
|
+
!model.table_exists? ||
|
35
|
+
!primary_key?(model)
|
27
36
|
end
|
28
37
|
end
|
29
38
|
|
@@ -1,20 +1,24 @@
|
|
1
1
|
module ForestAdminDatasourceActiveRecord
|
2
2
|
module Utils
|
3
3
|
ActiveRecordSerializer = Struct.new(:object) do
|
4
|
-
def to_hash
|
5
|
-
hash_object(object)
|
4
|
+
def to_hash(projection)
|
5
|
+
hash_object(object, projection)
|
6
6
|
end
|
7
7
|
|
8
|
-
def hash_object(object, with_associations: true)
|
8
|
+
def hash_object(object, projection = nil, with_associations: true)
|
9
9
|
hash = {}
|
10
10
|
|
11
|
-
return
|
11
|
+
return if object.nil?
|
12
12
|
|
13
13
|
hash.merge! object.attributes
|
14
14
|
|
15
15
|
if with_associations
|
16
16
|
each_association_collection(object) do |association_name, item|
|
17
|
-
hash[association_name] = hash_object(
|
17
|
+
hash[association_name] = hash_object(
|
18
|
+
item,
|
19
|
+
projection.relations[association_name],
|
20
|
+
with_associations: projection.relations.key?(association_name)
|
21
|
+
)
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
@@ -109,7 +109,8 @@ module ForestAdminDatasourceActiveRecord
|
|
109
109
|
def apply_select
|
110
110
|
unless @projection.nil?
|
111
111
|
@query = @query.select(@select.join(', '))
|
112
|
-
|
112
|
+
|
113
|
+
@query = @query.includes(format_relation_projection(@projection))
|
113
114
|
end
|
114
115
|
|
115
116
|
@query
|
@@ -144,6 +145,16 @@ module ForestAdminDatasourceActiveRecord
|
|
144
145
|
@query.send(aggregator, query)
|
145
146
|
end
|
146
147
|
end
|
148
|
+
|
149
|
+
def format_relation_projection(projection)
|
150
|
+
result = {}
|
151
|
+
projection&.relations&.each do |relation, projection_relation|
|
152
|
+
formatted_relations = format_relation_projection(projection_relation)
|
153
|
+
|
154
|
+
result[relation.to_sym] = formatted_relations
|
155
|
+
end
|
156
|
+
result
|
157
|
+
end
|
147
158
|
end
|
148
159
|
end
|
149
160
|
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.61
|
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: 2024-07-
|
12
|
+
date: 2024-07-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|