2 years ago

#61890

test-img

Michael Evans

Rails 3 initializing custom Railtie

In a Rails 3 project, I'm creating a custom Railtie in order to monkey patch the Rails migration generator. I'm using the multiverse gem as a source of inspiration as it's very close to what I'm trying to do, only in a higher version of Rails.

Here is my Railtie:

require "rails/railtie"


module MultiDb
  class Railtie < Rails::Railtie
    config.after_initialize do
      puts "Hello!"
    end

    generators do
      ActiveSupport.on_load(:active_record) do
        require "rails/generators/migration"
        Rails::Generators::Migration.prepend(MultiDb::MigrationTemplate)
      end
    end
  end
end

module MultiDb
  module MigrationTemplate
    def migration_template(source, destination, config = {})
      p "migration template"

      super
    end
  end
end

I'm just not sure how I'm supposed to actually load/register (not sure of the terminology here) when my application boots. I've tried adding an initializer that requires this file, but that hasn't done anything at all.

The initializer lives in config/initializers/multi_db.rb and looks like:

require Rails.root.join('lib/multi_db/railtie.rb')

This runs, but I'm not sure if I should be calling my Railtie in some way - I tried just with MultiDb::Railtie.new, which of course doesn't work because there is no initialize method.

ruby-on-rails

ruby

ruby-on-rails-3

0 Answers

Your Answer

Accepted video resources