Skip to content

Commit

Permalink
feat(polymorphic): enabled polymorphic associations support by default (
Browse files Browse the repository at this point in the history
#644)

BREAKING CHANGE: polymorphic association support enabled by default. It is no longer necessary to define the env variable ENABLE_SUPPORT_POLYMORPHISM
  • Loading branch information
nicolasalexandre9 authored Jan 23, 2024
1 parent 8014369 commit 92853a9
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 26 deletions.
2 changes: 0 additions & 2 deletions app/services/forest_liana/schema_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ def add_associations
SchemaUtils.associations(@model).each do |association|
begin
if SchemaUtils.polymorphic?(association) &&
(ENV['ENABLE_SUPPORT_POLYMORPHISM'].present? && ENV['ENABLE_SUPPORT_POLYMORPHISM'].downcase == 'true')

collection.fields << {
field: association.name.to_s,
type: get_type_for_association(association),
Expand Down
7 changes: 1 addition & 6 deletions app/services/forest_liana/schema_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ class SchemaUtils
def self.associations(active_record_class)
active_record_class.reflect_on_all_associations.select do |association|
begin
if (ENV['ENABLE_SUPPORT_POLYMORPHISM'].present? && ENV['ENABLE_SUPPORT_POLYMORPHISM'].downcase == 'true')
polymorphic?(association) ? true : !is_active_type?(association.klass)
else
!polymorphic?(association) && !is_active_type?(association.klass)
end

polymorphic?(association) ? true : !is_active_type?(association.klass)
rescue
FOREST_LOGGER.warn "Unknown association #{association.name} on class #{active_record_class.name}"
false
Expand Down
17 changes: 0 additions & 17 deletions spec/services/forest_liana/schema_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,6 @@ module ForestLiana

expect(removed_fields).to be_empty
end

context 'when the polymorphic support was disabled' do
it 'should not define the association' do
ENV['ENABLE_SUPPORT_POLYMORPHISM'] = 'false'
Bootstrapper.new(true)
collection = ForestLiana.apimap.find do |object|
object.name.to_s == ForestLiana.name_for(Address)
end
association = collection.fields.find { |field| field[:field] == 'addressable' }
fields = collection.fields.select do |field|
field[:field] == 'addressable_id' || field[:field] == 'addressable_type'
end

expect(association).to be_nil
expect(fields.size).to eq(2)
end
end
end

context 'with an "unhandled" column types (binary, postgis geography, ...)' do
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
ENV['RAILS_ENV'] = 'test'
ENV['ENABLE_SUPPORT_POLYMORPHISM'] = 'true'

require File.expand_path('../dummy/config/environment', __FILE__)

Expand Down

0 comments on commit 92853a9

Please sign in to comment.