Permalink
Browse files

gems can be added or skipped from the template

  • Loading branch information...
1 parent 37d4bfb commit 8beb42cfbc41753ae4dbb91e16abcd1fb7d00356 @tenderlove tenderlove committed Nov 4, 2013
@@ -138,12 +138,73 @@ def create_root
FileUtils.cd(destination_root) unless options[:pretend]
end
+ class TemplateRecorder < ::BasicObject # :nodoc:
+ attr_reader :gems
+
+ def initialize(target)
+ @target = target
+ # unfortunately, instance eval has access to these ivars
+ @app_const = target.send :app_const
+ @app_const_base = target.send :app_const_base
+ @app_name = target.send :app_name
+ @commands = []
+ @gems = []
+ end
+
+ def gemfile_entry(*args)
+ @target.send :gemfile_entry, *args
+ end
+
+ def add_gem_entry_filter(*args, &block)
+ @target.send :add_gem_entry_filter, *args, &block
+ end
+
+ def method_missing(name, *args, &block)
+ @commands << [name, args, block]
+ end
+
+ def respond_to_missing?(method, priv = false)
+ super || @target.respond_to?(method, priv)
+ end
+
+ def replay!
+ @commands.each do |name, args, block|
+ @target.send name, *args, &block
+ end
+ end
+ end
+
def apply_rails_template
- apply rails_template if rails_template
+ @recorder = TemplateRecorder.new self
+
+ apply(rails_template, target: @recorder) if rails_template
rescue Thor::Error, LoadError, Errno::ENOENT => e
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
end
+ def replay_template
+ @recorder.replay! if @recorder
+ end
+
+ def apply(path, config={})
+ verbose = config.fetch(:verbose, true)
+ target = config.fetch(:target, self)
+ is_uri = path =~ /^https?\:\/\//
+ path = find_in_source_paths(path) unless is_uri
+
+ say_status :apply, path, verbose
+ shell.padding += 1 if verbose
+
+ if is_uri
+ contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
+ else
+ contents = open(path) {|io| io.read }
+ end
+
+ target.instance_eval(contents, path)
+ shell.padding -= 1 if verbose
+ end
+
def set_default_accessors!
self.destination_root = File.expand_path(app_path, destination_root)
self.rails_template = case options[:template]
@@ -165,6 +165,7 @@ def initialize(*args)
end
public_task :set_default_accessors!
+ public_task :apply_rails_template
public_task :create_root
def create_root_files
@@ -234,7 +235,8 @@ def delete_js_folder_skipping_javascript
end
end
- public_task :apply_rails_template, :run_bundle
+ public_task :replay_template
+ public_task :run_bundle
protected
@@ -156,6 +156,32 @@ def test_config_database_is_added_by_default
end
end
+ def test_add_gemfile_entry
+ template = Tempfile.open 'my_template'
+ template.puts 'gemfile_entry "tenderlove"'
+ template.flush
+
+ run_generator([destination_root, "-m", template.path])
+ assert_file "Gemfile", /tenderlove/
+ ensure
+ template.close
+ template.unlink
+ end
+
+ def test_add_skip_entry
+ template = Tempfile.open 'my_template'
+ template.puts 'add_gem_entry_filter { |gem| gem.name != "jbuilder" }'
+ template.flush
+
+ run_generator([destination_root, "-m", template.path])
+ assert_file "Gemfile" do |contents|
+ assert_no_match 'jbuilder', contents
+ end
+ ensure
+ template.close
+ template.unlink
+ end
+
def test_config_another_database
run_generator([destination_root, "-d", "mysql"])
assert_file "config/database.yml", /mysql/
@@ -1,6 +1,7 @@
require 'active_support/test_case'
require 'active_support/testing/autorun'
require 'rails/generators/app_base'
+require 'rails/generators/rails/app/app_generator'
module Rails
module Generators

1 comment on commit 8beb42c

I've checked the CHANGELOG for railties:
https://github.com/rails/rails/blob/v4.1.0.beta1/railties/CHANGELOG.md
but cannot find anything that explains these changes. It seems behavior of application templates has changed between Rails 4.0.2 and 4.1.0.beta1. See my issue #13390.

Please sign in to comment.