Skip to content

Loading…

Feature/engine support (Issue #11) #12

Open
wants to merge 6 commits into from

3 participants

@rthbound

Addresses #11 by generating an env.rb file that loads routes for any mounted engine and for the application itself (loads url_helpers for main_app and 'engine_name')

Warning, it's ugly... The thought of this code going into every spinach user's env.rb file grosses me out.

Perhaps it could be moved into the railties file...

@josepjaume
Codegram member

This is really cool :)

@josepjaume josepjaume commented on an outdated diff
lib/spinach-rails/generators/rails.rb
@@ -9,6 +9,28 @@ def create_environment_file
require 'minitest/spec'
+module Spinach
+ class FeatureSteps
+ known_engines = ObjectSpace.enum_for(:each_object, ::Rails::Railtie.singleton_class).to_a
@josepjaume Codegram member

This hack is pretty neat, but isn't there any other way to iterate through all the engines other than that? Also, I think this only works if spinach is loaded after all the other Engines am I right?

If that's the case, maybe this code should be initialized someplace here:

https://github.com/codegram/spinach-rails/blob/master/lib/spinach-rails/railtie.rb#L12

There should be a way to be sure we're the last gem to be initialized.

Or did I get something wrong?

@josepjaume Codegram member

Ok, I see you actually mentioned this on the Issue :+1:

Do you have the time to maybe tackle the problem from the Railtie and we can then merge it into master?

Would love to... Maybe you also noticed the engine is installed into vendor/plugins, support for which will be removed in Rails 4. May want to address that prior to merging, also.

@txus Codegram member
txus added a note

@rthbound ObjectSpace is disabled by default in JRuby. I don't think it's a very good idea to use it if we plan to keep our cross-implementation support.

@txus
Totally fair. It's a handy hack, but agreeably not a solution we should merge.

@txus Codegram member
txus added a note

If you get rid of ObjectSpace, it's a big +1 for me and I'll merge it right away :)

Looks like Rails::Engine.ancestors could replace searching ObjectSpace

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

@txus I've got something together, but tests are failing (even on a fresh clone)...

@txus
Codegram member

@rthbound any luck?

@rthbound

@txus looks like the failures are due to problems with the test itself and likely not to do with the implementation. Have not yet confirmed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 44 additions and 1 deletion.
  1. +22 −1 features/support/rails_fixtures.rb
  2. +22 −0 lib/spinach-rails/generators/rails.rb
View
23 features/support/rails_fixtures.rb
@@ -31,6 +31,20 @@ module RailsFixtures
end
private
+ def create_and_mount_engine(version = '3.2')
+ run 'bundle exec rails plugin new vendor/plugins/rails_engine --mountable'
+ stop_processes!
+ write_file('vendor/plugins/rails_engine/config/routes.rb', "
+ RailsEngine::Engine.routes.draw do
+ root to: redirect('/index.html')
+ end
+ ")
+ append_to_file("Gemfile",
+ "gem 'rails_engine', :path => 'vendor/plugins/rails_engine'\n")
+ stop_processes!
+ run "bundle install"
+ stop_processes!
+ end
def create_rails_app(version = '3.2')
@aruba_timeout_seconds = 100
@@ -41,15 +55,16 @@ def create_rails_app(version = '3.2')
")
run "bundle install"
run "rm -fR rails_app"
- stop_processes!
run 'bundle exec rails new rails_app'
stop_processes!
cd "rails_app"
write_file('config/routes.rb', "
RailsApp::Application.routes.draw do
root to: redirect('/index.html')
+ mount RailsEngine::Engine, :at => '/'
end
")
+ create_and_mount_engine(version)
end
def add_spinach_rails
@@ -64,12 +79,18 @@ def add_test_features
Scenario: Test scenario
Given I am running spinach
Then It should be all OK
+ Scenario: Mounted engine test scenario
+ Given the test app has mounted an engine
+ Then It should be all OK
")
write_file('features/steps/test_feature.rb',
"class TestFeature < Spinach::FeatureSteps
Given 'I am running spinach' do
visit root_path
end
+ Given 'the test app has mounted an engine' do
+ visit rails_engine.root_path
+ end
Then 'It should be all OK' do
page.has_content?('Rails').must_equal true
end
View
22 lib/spinach-rails/generators/rails.rb
@@ -9,6 +9,28 @@ def create_environment_file
require 'minitest/spec'
+module Spinach
+ class FeatureSteps
+ known_engines = Rails::Engine.descendants
+ engines_with_routes = known_engines.select{ |engine|
+ engine.respond_to?(:routes) && engine.routes.named_routes.routes.present?
+ }
+ engines_with_routes.each do |engine|
+ engine_name = engine.to_s.underscore.split("/")
+ case engine_name.pop
+ when "application"
+ send :define_method, :main_app do
+ engine.routes.url_helpers
+ end
+ when "engine"
+ send :define_method, engine_name[0].to_sym do
+ engine.routes.url_helpers
+ end
+ end
+ end
+ end
+end
+
# require 'database_cleaner'
# DatabaseCleaner.strategy = :truncation
#
Something went wrong with that request. Please try again.