Created
November 22, 2012 15:51
-
-
Save stas/4131823 to your computer and use it in GitHub Desktop.
Rails Engine: rake task and generator to DRY `spec/dummy`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails/generators' | |
require 'rails/generators/rails/plugin_new/plugin_new_generator' | |
module MyEngine | |
class DummyGenerator < Rails::Generators::PluginNewGenerator | |
def self.default_source_root | |
Rails::Generators::PluginNewGenerator.default_source_root | |
end | |
def do_nothing | |
end | |
alias :create_root :do_nothing | |
alias :create_root_files :do_nothing | |
alias :create_app_files :do_nothing | |
alias :create_config_files :do_nothing | |
alias :create_lib_files :do_nothing | |
alias :create_public_stylesheets_files :do_nothing | |
alias :create_javascript_files :do_nothing | |
alias :create_script_files :do_nothing | |
alias :update_gemfile :do_nothing | |
alias :create_test_files :do_nothing | |
alias :finish_template :do_nothing | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rake | |
require 'rspec/core/rake_task' | |
RSpec::Core::RakeTask.new(:spec => :dummy_app) do |t| | |
t.pattern = File.expand_path('../spec/**/*_spec.rb', __FILE__) | |
end | |
task :default => :spec | |
desc 'Generates a dummy app for testing' | |
task :dummy_app => [:setup, :migrate] | |
task :setup do | |
require 'rails' | |
require 'my_engine' | |
require 'my_engine/generators/dummy_generator' | |
dummy = File.expand_path('../spec/dummy', __FILE__) | |
sh "rm -rf #{dummy}" | |
MyEngine::DummyGenerator.start( | |
%W(. --quiet --force --skip-bundle --old-style-hash --dummy-path=#{dummy}) | |
) | |
end | |
task :migrate do | |
rakefile = File.expand_path('../spec/dummy/Rakefile', __FILE__) | |
sh("rake -f #{rakefile} my_engine:install:migrations") | |
sh("rake -f #{rakefile} db:create db:migrate db:test:prepare") | |
end |
@etdsoft take a look at https://github.com/stas/rails-dummy
Cool gem, definitely plan on using it, Thx ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, it is amazing.
@stas not sure if I'm trying to get too fancy, but I'd like to remove the dependency of hard-coding the engine name in the :migrate task. I'm working on an open source framework for security testing that is extendable using plugins.
I've removed the hard-coding dependency in the :setup task by creating my DummyGenerator in the core of the framework and using it from my plugins. You can see an example here Rakefile#L20-31:
This is the Rake file in one of the plugins and you can see the hard-coded
dradis_html_export
reference next to the bottom. Ideally I'd like to have my plugins:And manage from the framework classes the :dummy_app, :setup and :migrate tasks. My first idea has been to pass the engine name as a variable, something like:
But this isn't very neat. I assume that there should be some way for the core library to learn about the engine name where it is being called from. At the end of the day, this is done by the DummyGenerator, it correctly adds the following to the dummy app's
core/applicarion.rb
:I'm still trying to figure this one out, but I'm not as familiar with the rails internals as I should be and was wondering if you may have some thoughts on it.
Thanks!