@@ -241,8 +241,33 @@ def add_columns
241241 def add_associations
242242 SchemaUtils . associations ( @model ) . each do |association |
243243 begin
244+ if SchemaUtils . polymorphic? ( association ) &&
245+ ( ENV [ 'ENABLE_SUPPORT_POLYMORPHISM' ] . present? && ENV [ 'ENABLE_SUPPORT_POLYMORPHISM' ] . downcase == 'true' )
246+
247+ collection . fields << {
248+ field : association . name . to_s ,
249+ type : get_type_for_association ( association ) ,
250+ relationship : get_relationship_type ( association ) ,
251+ reference : "#{ association . name . to_s } .id" ,
252+ inverse_of : @model . name . demodulize . underscore ,
253+ is_filterable : false ,
254+ is_sortable : true ,
255+ is_read_only : false ,
256+ is_required : false ,
257+ is_virtual : false ,
258+ default_value : nil ,
259+ integration : nil ,
260+ relationships : nil ,
261+ widget : nil ,
262+ validations : [ ] ,
263+ polymorphic_referenced_models : get_polymorphic_types ( association )
264+ }
265+
266+ collection . fields = collection . fields . reject do |field |
267+ field [ :field ] == association . foreign_key || field [ :field ] == association . foreign_type
268+ end
244269 # NOTICE: Delete the association if the targeted model is excluded.
245- if !SchemaUtils . model_included? ( association . klass )
270+ elsif !SchemaUtils . model_included? ( association . klass )
246271 field = collection . fields . find do |x |
247272 x [ :field ] == association . foreign_key
248273 end
@@ -275,6 +300,17 @@ def inverse_of(association)
275300 automatic_inverse_of ( association )
276301 end
277302
303+ def get_polymorphic_types ( relation )
304+ types = [ ]
305+ ForestLiana . models . each do |model |
306+ unless model . reflect_on_all_associations . select { |association | association . options [ :as ] == relation . name . to_sym } . empty?
307+ types << model . name
308+ end
309+ end
310+
311+ types
312+ end
313+
278314 def automatic_inverse_of ( association )
279315 name = association . active_record . name . demodulize . underscore
280316
0 commit comments