aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails_generator
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails_generator')
-rw-r--r--railties/lib/rails_generator/commands.rb8
-rw-r--r--railties/lib/rails_generator/generators/applications/app/template_runner.rb30
2 files changed, 31 insertions, 7 deletions
diff --git a/railties/lib/rails_generator/commands.rb b/railties/lib/rails_generator/commands.rb
index 299044c3d7..b684dc92be 100644
--- a/railties/lib/rails_generator/commands.rb
+++ b/railties/lib/rails_generator/commands.rb
@@ -182,15 +182,19 @@ HELP
nesting = class_name.split('::')
name = nesting.pop
+ # Hack to limit const_defined? to non-inherited on 1.9.
+ extra = []
+ extra << false unless Object.method(:const_defined?).arity == 1
+
# Extract the last Module in the nesting.
last = nesting.inject(Object) { |last, nest|
- break unless last.const_defined?(nest)
+ break unless last.const_defined?(nest, *extra)
last.const_get(nest)
}
# If the last Module exists, check whether the given
# class exists and raise a collision if so.
- if last and last.const_defined?(name.camelize)
+ if last and last.const_defined?(name.camelize, *extra)
raise_class_collision(class_name)
end
end
diff --git a/railties/lib/rails_generator/generators/applications/app/template_runner.rb b/railties/lib/rails_generator/generators/applications/app/template_runner.rb
index eeb6b17661..73ab57d4f0 100644
--- a/railties/lib/rails_generator/generators/applications/app/template_runner.rb
+++ b/railties/lib/rails_generator/generators/applications/app/template_runner.rb
@@ -85,6 +85,7 @@ module Rails
# Adds an entry into config/environment.rb for the supplied gem :
def gem(name, options = {})
log 'gem', name
+ env = options.delete(:env)
gems_code = "config.gem '#{name}'"
@@ -93,18 +94,26 @@ module Rails
gems_code << ", #{opts}"
end
- environment gems_code
+ environment gems_code, :env => env
end
# Adds a line inside the Initializer block for config/environment.rb. Used by #gem
- def environment(data = nil, &block)
+ # If options :env is specified, the line is appended to the corresponding
+ # file in config/environments/#{env}.rb
+ def environment(data = nil, options = {}, &block)
sentinel = 'Rails::Initializer.run do |config|'
data = block.call if !data && block_given?
in_root do
- gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
- "#{match}\n " << data
+ if options[:env].nil?
+ gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
+ "#{match}\n " << data
+ end
+ else
+ Array.wrap(options[:env]).each do|env|
+ append_file "config/environments/#{env}.rb", "\n#{data}"
+ end
end
end
end
@@ -307,7 +316,7 @@ module Rails
#
def ask(string)
log '', string
- gets.strip
+ STDIN.gets.strip
end
# Do something in the root of the Rails application or
@@ -356,6 +365,17 @@ module Rails
File.open(path, 'wb') { |file| file.write(content) }
end
+ # Append text to a file
+ #
+ # ==== Example
+ #
+ # append_file 'config/environments/test.rb', 'config.gem "rspec"'
+ #
+ def append_file(relative_destination, data)
+ path = destination_path(relative_destination)
+ File.open(path, 'ab') { |file| file.write(data) }
+ end
+
def destination_path(relative_destination)
File.join(root, relative_destination)
end