clio / polymorphic_integer_type

MIT License
37 stars 35 forks source link

Use a dynamic module to include the writing methods that are generated from using belongs_to #24

Closed doliveirakn closed 4 years ago

doliveirakn commented 4 years ago

Prior to this, if we have something like

class Link < ActiveRecord::Base
  include PolymorphicIntegerType::Extensions

  belongs_to :source, polymorphic: true, integer_type: true
  belongs_to :target, polymorphic: true, integer_type: true

  def source=(val)
    super(val)
  end

  def source_type=(val)
    super(val)
  end
end

the source= will override the method defined by the belongs_to and what will end up happening is that the source_type will not end up being created properly.

By created an anonymous module, we can prepend it to the class and then the super behaviour will work as expected.