Search code examples
ruby-on-railsrubymonkeypatching

monkey patch a block in a module in ruby


module Cequel
  module Record

    module Schema
      extend ActiveSupport::Concern
      extend Util::Forwardable

      included do
        class_attribute :table_name, instance_writer: false
        self.table_name = name.demodulize.tableize.to_sym unless name.nil? || self.table_name.present?
      end
    end
  end
end

I would like to monkey patch included block from this module in my rails application, but when I define the same code in initializer in rails I get

included': Cannot define multiple 'included' blocks for a Concern (ActiveSupport::Concern::MultipleIncludedBlocks)

This is what I want to fix with a patch until this PR is merged, Is there any way to patch the included block?


Solution

  • You need to start from the scratch in your initializer. That said, you should remove the module constant definition completely with

    Cequel::Record.send :remove_const, :Schema
    

    Now you can re-execute the snippet, defining Schema as you want: Ruby does not know anything about it anymore.