Skip to content

Instantly share code, notes, and snippets.

@yoon
Last active August 29, 2015 14:22
Show Gist options
  • Save yoon/92ba2963bf96cd387800 to your computer and use it in GitHub Desktop.
Save yoon/92ba2963bf96cd387800 to your computer and use it in GitHub Desktop.
Rails engines links

Keep your migrations in the engine

via: http://blog.pivotal.io/labs/labs/leave-your-migrations-in-your-rails-engines

in lib/[engine_name]/engine.rb

initializer :append_migrations do |app|
  unless app.root.to_s.match root.to_s
    config.paths["db/migrate"].expanded.each do |expanded_path|
      app.config.paths["db/migrate"] << expanded_path
    end
  end
end

Decorators, inputs, etc.

in lib/[engine_name]/engine.rb

root = File.expand_path('../../', __FILE__)
config.autoload_paths << root

config.to_prepare do
  Dir[File.join(root, "../app/decorators/**/*_decorator*.rb")].each do |c|
    require_dependency(c)
  end
end

Testing with RSpec

in spec/rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path("../dummy/config/environment.rb",  __FILE__)
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.join(ENGINE_RAILS_ROOT, "spec/support/**/*.rb")].each {|f| require f }

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment