forest_admin_datasource_active_record 1.0.0.pre.beta.59 → 1.0.0.pre.beta.61

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: fc828978458b7c1566d7f61d007df24401281c115bc54a551fbedde6e6cf3783
4
- data.tar.gz: 3fbea7107c9ffbb8442f01b47fc58bb23bccf47513e34d6b7caec47516098742
3
+ metadata.gz: c9ce7a6a32ae3e985c64e2c9458165de5d24e8d7a532ca6a405aac048ef5ede8
4
+ data.tar.gz: 65ba39d8d70775cbb98e4b0f0d85888a5946fda8e34bd88bb8e7e3b52004fe2c
5
5
  SHA512:
6
- metadata.gz: 0ec4a0787f25fc01d5de4a3afaa320962bdb78716d5db67e05fc195d30eacf386b9f4ea571e57e7fd663415727b2ed75c84d4bf2f05486b7286d9bb4d2a66f5a
7
- data.tar.gz: 4b201a38b1543eb8a528eab862e653ca5316173c841b426024b5de56632920a7cec966e5687914b8108d261d906920a3a9b1d990996318ba8ed1c28fe2184437
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
- add_field(
71
- association.name.to_s,
72
- ForestAdminDatasourceToolkit::Schema::Relations::OneToOneSchema.new(
73
- foreign_collection: association.class_name.demodulize.underscore,
74
- origin_key: association.foreign_key,
75
- origin_key_target: association.association_primary_key
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
- add_field(
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::ManyToManySchema.new(
104
+ ForestAdminDatasourceToolkit::Schema::Relations::ManyToOneSchema.new(
92
105
  foreign_collection: association.class_name.demodulize.underscore,
93
- origin_key: association.through_reflection.foreign_key,
94
- origin_key_target: association.through_reflection.join_foreign_key,
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
- else
101
- add_field(
102
- association.name.to_s,
103
- ForestAdminDatasourceToolkit::Schema::Relations::OneToManySchema.new(
104
- foreign_collection: association.class_name.demodulize.underscore,
105
- origin_key: association.foreign_key,
106
- origin_key_target: association.association_primary_key
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? || @models.include?(model) || !model.table_exists?
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 {} if object.nil?
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(item, with_associations: false)
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
- @query = @query.includes(@projection.relations.keys.map(&:to_sym))
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
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceActiveRecord
2
- VERSION = "1.0.0-beta.59"
2
+ VERSION = "1.0.0-beta.61"
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.59
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-01 00:00:00.000000000 Z
12
+ date: 2024-07-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord