forest_admin_datasource_active_record 1.15.0 → 1.15.1

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: 9c597f5097cc2fca7a364b0ac6b9480e1d8dca6ec481bb321a2507379b21d89d
4
- data.tar.gz: a8bf8f41c39093fe4d13197e32fb03c8ac45aa2ca0c70bb4e7ce28e702fa7d8a
3
+ metadata.gz: fae5816b36cb706d530aedc0e3828e030f09f9f67de2e653b3aeba5b56c56afe
4
+ data.tar.gz: b95fa478d9af1ba920e91b94458b4be1700f24cfea1f0ab480e66ba478e7be97
5
5
  SHA512:
6
- metadata.gz: '0595ed02849bb6f65b4913c7893147f36fba65f337a3ec5dc336998e827566cf374e8a6084ae9f4bdea208ef44a639ee7dc737e17e559765a193388e38455fee'
7
- data.tar.gz: a847aad91d684e88f4e25c10984b8f5b62c7e6b537066e9d4cae54bd2f74c42ebaa7520e3467c5ee14568684a32453f0059dab0a199475acd5880dff851e0c1b
6
+ metadata.gz: 9bfdda35755f2071b0a3e3fbf8e93be8c8cb22681205f04adb918a2d7ff6c2ca7bd0183fc68e21bbfe3916cb2c3049ea056737dd389d5404cb936e55d499049c
7
+ data.tar.gz: 77e3b2af2d4a736c63c7edaa1f412e17d5c0dbb3b89ca9b138033a57ccdf2eef64c4ca7f5f8fd4b9bb548210be1342579b8f19bd2bdaa17e1dfe794faab886e0
@@ -196,6 +196,7 @@ module ForestAdminDatasourceActiveRecord
196
196
  end
197
197
  end
198
198
  when :has_and_belongs_to_many
199
+ through_collection_name = resolve_habtm_through_collection(association)
199
200
  add_field(
200
201
  association.name.to_s,
201
202
  ForestAdminDatasourceToolkit::Schema::Relations::ManyToManySchema.new(
@@ -204,7 +205,7 @@ module ForestAdminDatasourceActiveRecord
204
205
  origin_key_target: association.join_foreign_key,
205
206
  foreign_key: association.association_foreign_key,
206
207
  foreign_key_target: association.association_primary_key,
207
- through_collection: association.join_table.classify
208
+ through_collection: through_collection_name
208
209
  )
209
210
  )
210
211
  end
@@ -218,6 +219,62 @@ module ForestAdminDatasourceActiveRecord
218
219
  end
219
220
  # rubocop:enable Metrics/BlockNesting
220
221
 
222
+ def resolve_habtm_through_collection(association)
223
+ join_table = association.join_table.to_s
224
+ through_model_name = association.join_table.classify
225
+
226
+ # Check if the join table exists and has an 'id' column
227
+ if ActiveRecord::Base.connection.table_exists?(join_table)
228
+ columns = ActiveRecord::Base.connection.columns(join_table)
229
+ has_id_column = columns.any? { |col| col.name == 'id' }
230
+
231
+ if has_id_column
232
+ begin
233
+ through_model_name.constantize
234
+ rescue NameError
235
+ create_virtual_habtm_model(association, through_model_name)
236
+ end
237
+ end
238
+ end
239
+
240
+ format_model_name(through_model_name)
241
+ end
242
+
243
+ def create_virtual_habtm_model(association, model_name)
244
+ parent_module = @model.name.deconstantize
245
+
246
+ # Create the model class dynamically and assign to constant
247
+ klass = if parent_module.empty?
248
+ # Create in global namespace
249
+ Object.const_set(model_name, Class.new(ActiveRecord::Base))
250
+ else
251
+ # Create in parent module namespace
252
+ parent_module_obj = parent_module.constantize
253
+ parent_module_obj.const_set(model_name.demodulize, Class.new(ActiveRecord::Base))
254
+ end
255
+
256
+ klass.table_name = association.join_table
257
+
258
+ # Mark this as a virtual through collection so datasource doesn't try to add it again
259
+ klass.const_set(:VIRTUAL_THROUGH_COLLECTION, true)
260
+
261
+ # add associations
262
+ klass.belongs_to association.active_record.name.demodulize.underscore.to_sym,
263
+ class_name: association.active_record.name,
264
+ foreign_key: association.join_foreign_key
265
+ klass.belongs_to association.name.to_s.singularize.to_sym,
266
+ class_name: association.klass.name,
267
+ foreign_key: association.association_foreign_key
268
+
269
+ logger = ActiveSupport::Logger.new($stdout)
270
+ logger.info(
271
+ "[ForestAdmin] Created virtual model '#{model_name}' for HABTM join table '#{association.join_table}' " \
272
+ 'with id column. This allows proper handling of the many-to-many relationship.'
273
+ )
274
+
275
+ klass
276
+ end
277
+
221
278
  def warn_missing_polymorphic_columns(association)
222
279
  missing_columns = []
223
280
  missing_columns << association.foreign_key unless schema[:fields][association.foreign_key]
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceActiveRecord
2
- VERSION = "1.15.0"
2
+ VERSION = "1.15.1"
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.15.0
4
+ version: 1.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu