aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/code_statistics.rb11
-rw-r--r--railties/lib/rails/commands.rb10
-rw-r--r--railties/lib/rails/commands/console.rb48
-rw-r--r--railties/lib/rails/commands/dbconsole.rb137
-rw-r--r--railties/lib/rails/commands/server.rb5
-rw-r--r--railties/lib/rails/generators/app_base.rb2
-rw-r--r--railties/lib/rails/generators/named_base.rb11
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt8
-rw-r--r--railties/lib/rails/generators/rails/controller/templates/controller.rb4
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec2
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/templates/Gemfile15
-rw-r--r--railties/lib/rails/generators/rails/plugin_new/templates/Rakefile2
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb2
-rw-r--r--railties/lib/rails/railtie.rb3
-rw-r--r--railties/lib/rails/tasks/documentation.rake2
-rw-r--r--railties/lib/rails/tasks/statistics.rake1
16 files changed, 154 insertions, 109 deletions
diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb
index bcac0751d6..1aed2796c1 100644
--- a/railties/lib/rails/code_statistics.rb
+++ b/railties/lib/rails/code_statistics.rb
@@ -26,7 +26,7 @@ class CodeStatistics #:nodoc:
Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
end
- def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
+ def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
Dir.foreach(directory) do |file_name|
@@ -39,6 +39,13 @@ class CodeStatistics #:nodoc:
comment_started = false
+ case file_name
+ when /.*\.js$/
+ comment_pattern = /^\s*\/\//
+ else
+ comment_pattern = /^\s*#/
+ end
+
File.open(directory + "/" + file_name) do |f|
while line = f.gets
stats["lines"] += 1
@@ -55,7 +62,7 @@ class CodeStatistics #:nodoc:
end
stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
- stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
+ stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ comment_pattern
end
end
end
diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb
index 7f473c237c..8816387d34 100644
--- a/railties/lib/rails/commands.rb
+++ b/railties/lib/rails/commands.rb
@@ -36,9 +36,17 @@ when 'benchmarker', 'profiler'
when 'console'
require 'rails/commands/console'
+ options = Rails::Console.parse_arguments(ARGV)
+
+ # RAILS_ENV needs to be set before config/application is required
+ ENV['RAILS_ENV'] = options[:environment] if options[:environment]
+
+ # shift ARGV so IRB doesn't freak
+ ARGV.shift if ARGV.first && ARGV.first[0] != '-'
+
require APP_PATH
Rails.application.require_environment!
- Rails::Console.start(Rails.application)
+ Rails::Console.start(Rails.application, options)
when 'server'
# Change to the application's path if there is no config.ru file in current dir.
diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb
index b95df3e545..92cee6b638 100644
--- a/railties/lib/rails/commands/console.rb
+++ b/railties/lib/rails/commands/console.rb
@@ -4,21 +4,12 @@ require 'irb/completion'
module Rails
class Console
- attr_reader :options, :app, :console, :arguments
-
- def self.start(*args)
- new(*args).start
- end
-
- def initialize(app, arguments = ARGV)
- @app = app
- @arguments = arguments
- app.load_console
- @console = app.config.console || IRB
- end
+ class << self
+ def start(*args)
+ new(*args).start
+ end
- def options
- @options ||= begin
+ def parse_arguments(arguments)
options = {}
OptionParser.new do |opt|
@@ -31,20 +22,38 @@ module Rails
opt.parse!(arguments)
end
+ if arguments.first && arguments.first[0] != '-'
+ env = arguments.first
+ options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
+ end
+
options
end
end
+ attr_reader :options, :app, :console
+
+ def initialize(app, options={})
+ @app = app
+ @options = options
+ app.load_console
+ @console = app.config.console || IRB
+ end
+
def sandbox?
options[:sandbox]
end
+ def environment
+ options[:environment] ||= ENV['RAILS_ENV'] || 'development'
+ end
+
def environment?
- options[:environment]
+ environment
end
def set_environment!
- Rails.env = options[:environment]
+ Rails.env = environment
end
def debugger?
@@ -53,9 +62,7 @@ module Rails
def start
app.sandbox = sandbox?
-
require_debugger if debugger?
-
set_environment! if environment?
if sandbox?
@@ -82,8 +89,3 @@ module Rails
end
end
end
-
-# Has to set the RAILS_ENV before config/application is required
-if ARGV.first && !ARGV.first.index("-") && env = ARGV.shift # has to shift the env ARGV so IRB doesn't freak
- ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
-end
diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb
index cc7caffc3d..cc0552184a 100644
--- a/railties/lib/rails/commands/dbconsole.rb
+++ b/railties/lib/rails/commands/dbconsole.rb
@@ -5,66 +5,19 @@ require 'rbconfig'
module Rails
class DBConsole
- attr_reader :arguments, :config
-
+ attr_reader :config, :arguments
+
def self.start
- new(config).start
+ new.start
end
- def self.config
- config = begin
- YAML.load(ERB.new(IO.read("config/database.yml")).result)
- rescue SyntaxError, StandardError
- require APP_PATH
- Rails.application.config.database_configuration
- end
-
- unless config[env]
- abort "No database is configured for the environment '#{env}'"
- end
-
- config[env]
- end
-
- def self.env
- if Rails.respond_to?(:env)
- Rails.env
- else
- ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
- end
- end
-
- def initialize(config, arguments = ARGV)
- @config, @arguments = config, arguments
+ def initialize(arguments = ARGV)
+ @arguments = arguments
end
def start
- include_password = false
- options = {}
- OptionParser.new do |opt|
- opt.banner = "Usage: rails dbconsole [environment] [options]"
- opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
- include_password = true
- end
-
- opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
- "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
- options['mode'] = mode
- end
-
- opt.on("--header") do |h|
- options['header'] = h
- end
-
- opt.on("-h", "--help", "Show this help message.") do
- puts opt
- exit
- end
-
- opt.parse!(arguments)
- abort opt.to_s unless (0..1).include?(arguments.size)
- end
-
+ options = parse_arguments(arguments)
+ ENV['RAILS_ENV'] = options[:environment] || environment
case config["adapter"]
when /^mysql/
@@ -76,7 +29,7 @@ module Rails
'encoding' => '--default-character-set'
}.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
- if config['password'] && include_password
+ if config['password'] && options['include_password']
args << "--password=#{config['password']}"
elsif config['password'] && !config['password'].to_s.empty?
args << "-p"
@@ -90,7 +43,7 @@ module Rails
ENV['PGUSER'] = config["username"] if config["username"]
ENV['PGHOST'] = config["host"] if config["host"]
ENV['PGPORT'] = config["port"].to_s if config["port"]
- ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
+ ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && options['include_password']
find_cmd_and_exec('psql', config["database"])
when "sqlite"
@@ -110,7 +63,7 @@ module Rails
if config['username']
logon = config['username']
- logon << "/#{config['password']}" if config['password'] && include_password
+ logon << "/#{config['password']}" if config['password'] && options['include_password']
logon << "@#{config['database']}" if config['database']
end
@@ -121,8 +74,73 @@ module Rails
end
end
+ def config
+ @config ||= begin
+ cfg = begin
+ cfg = YAML.load(ERB.new(IO.read("config/database.yml")).result)
+ rescue SyntaxError, StandardError
+ require APP_PATH
+ Rails.application.config.database_configuration
+ end
+
+ unless cfg[environment]
+ abort "No database is configured for the environment '#{environment}'"
+ end
+
+ cfg[environment]
+ end
+ end
+
+ def environment
+ if Rails.respond_to?(:env)
+ Rails.env
+ else
+ ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
+ end
+ end
+
protected
+ def parse_arguments(arguments)
+ options = {}
+
+ OptionParser.new do |opt|
+ opt.banner = "Usage: rails dbconsole [environment] [options]"
+ opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
+ options['include_password'] = true
+ end
+
+ opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
+ "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
+ options['mode'] = mode
+ end
+
+ opt.on("--header") do |h|
+ options['header'] = h
+ end
+
+ opt.on("-h", "--help", "Show this help message.") do
+ puts opt
+ exit
+ end
+
+ opt.on("-e", "--environment=name", String,
+ "Specifies the environment to run this console under (test/development/production).",
+ "Default: development"
+ ) { |v| options[:environment] = v.strip }
+
+ opt.parse!(arguments)
+ abort opt.to_s unless (0..1).include?(arguments.size)
+ end
+
+ if arguments.first && arguments.first[0] != '-'
+ env = arguments.first
+ options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
+ end
+
+ options
+ end
+
def find_cmd_and_exec(commands, *args)
commands = Array(commands)
@@ -145,8 +163,3 @@ module Rails
end
end
end
-
-# Has to set the RAILS_ENV before config/application is required
-if ARGV.first && !ARGV.first.index("-") && env = ARGV.first
- ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
-end
diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb
index e68d2e05c5..9ef64da3ef 100644
--- a/railties/lib/rails/commands/server.rb
+++ b/railties/lib/rails/commands/server.rb
@@ -32,11 +32,6 @@ module Rails
opt_parser.parse! args
- # Handle's environment like RAILS_ENV=production passed in directly
- if index = args.index {|arg| arg.include?("RAILS_ENV")}
- options[:environment] ||= args.delete_at(index).split('=').last
- end
-
options[:server] = args.shift
options
end
diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb
index 2c1742c6be..5838c9fc38 100644
--- a/railties/lib/rails/generators/app_base.rb
+++ b/railties/lib/rails/generators/app_base.rb
@@ -230,7 +230,7 @@ module Rails
if defined?(JRUBY_VERSION)
"gem 'therubyrhino'\n"
else
- "# gem 'therubyracer', platform: :ruby\n"
+ "# gem 'therubyracer', platforms: :ruby\n"
end
end
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb
index 63703176de..b61a5fc69d 100644
--- a/railties/lib/rails/generators/named_base.rb
+++ b/railties/lib/rails/generators/named_base.rb
@@ -84,10 +84,11 @@ module Rails
end
def namespaced_class_path
- @namespaced_class_path ||= begin
- namespace_path = namespace.name.split("::").map {|m| m.underscore }
- namespace_path + @class_path
- end
+ @namespaced_class_path ||= [namespaced_path] + @class_path
+ end
+
+ def namespaced_path
+ @namespaced_path ||= namespace.name.split("::").map {|m| m.underscore }[0]
end
def class_name
@@ -134,7 +135,7 @@ module Rails
end
def route_url
- @route_url ||= class_path.collect{|dname| "/" + dname }.join('') + "/" + plural_file_name
+ @route_url ||= class_path.collect {|dname| "/" + dname }.join + "/" + plural_file_name
end
# Tries to retrieve the application name or simple return application.
diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt
index 19cbf0e4f1..280f777cc0 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt
+++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt
@@ -9,8 +9,8 @@ ActiveSupport.on_load(:action_controller) do
end
<%- unless options.skip_active_record? -%>
-# Disable root element in JSON by default.
-ActiveSupport.on_load(:active_record) do
- self.include_root_in_json = false
-end
+# To enable root element in JSON for ActiveRecord objects.
+# ActiveSupport.on_load(:active_record) do
+# self.include_root_in_json = true
+# end
<%- end -%>
diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb
index ece6bbba3b..633e0b3177 100644
--- a/railties/lib/rails/generators/rails/controller/templates/controller.rb
+++ b/railties/lib/rails/generators/rails/controller/templates/controller.rb
@@ -1,7 +1,7 @@
<% if namespaced? -%>
-require_dependency "<%= namespaced_file_path %>/application_controller"
-<% end -%>
+require_dependency "<%= namespaced_path %>/application_controller"
+<% end -%>
<% module_namespacing do -%>
class <%= class_name %>Controller < ApplicationController
<% actions.each do |action| -%>
diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec
index 82ffeebb86..568ed653b7 100644
--- a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec
+++ b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec
@@ -22,6 +22,8 @@ Gem::Specification.new do |s|
<% if full? && !options[:skip_javascript] -%>
# s.add_dependency "<%= "#{options[:javascript]}-rails" %>"
<% end -%>
+<% unless options[:skip_active_record] -%>
s.add_development_dependency "<%= gem_for_database %>"
+<% end -%>
end
diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
index 9399c9cb77..7448b386c5 100644
--- a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
+++ b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
@@ -1,17 +1,32 @@
source "http://rubygems.org"
+<% if options[:skip_gemspec] -%>
+<%= '# ' if options.dev? || options.edge? -%>gem "rails", "~> <%= Rails::VERSION::STRING %>"
+<% if full? && !options[:skip_javascript] -%>
+# gem "<%= "#{options[:javascript]}-rails" %>"
+<% end -%>
+<% else -%>
# Declare your gem's dependencies in <%= name %>.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
+<% end -%>
+<% unless options[:javascript] == 'jquery' -%>
# jquery-rails is used by the dummy application
gem "jquery-rails"
+<% end -%>
+<% if options[:skip_gemspec] -%>
+group :development do
+ gem "<%= gem_for_database %>"
+end
+<% else -%>
# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.
+<% end -%>
<% if options.dev? || options.edge? -%>
# Your gem is dependent on dev or edge Rails. Once you can lock this
diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
index 743036362e..1369140537 100644
--- a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
+++ b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
@@ -14,7 +14,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
-<% if full? && !options[:skip_active_record] -%>
+<% if full? && !options[:skip_active_record] && !options[:skip_test_unit] -%>
APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
<% end %>
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
index 0294bde582..b3e74f9b02 100644
--- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
+++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -1,7 +1,7 @@
<% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"
-<% end -%>
+<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
# GET <%= route_url %>
diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb
index c3cc65ab31..a06be59759 100644
--- a/railties/lib/rails/railtie.rb
+++ b/railties/lib/rails/railtie.rb
@@ -190,7 +190,8 @@ module Rails
end
def load_tasks(app=self)
- extend Rake::DSL if defined? Rake::DSL
+ require 'rake'
+ extend Rake::DSL
self.class.rake_tasks.each { |block| self.instance_exec(app, &block) }
# load also tasks from all superclasses
diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake
index 4ec49eee76..2851ca4189 100644
--- a/railties/lib/rails/tasks/documentation.rake
+++ b/railties/lib/rails/tasks/documentation.rake
@@ -2,7 +2,7 @@ require 'rdoc/task'
# Monkey-patch to remove redoc'ing and clobber descriptions to cut down on rake -T noise
class RDocTaskWithoutDescriptions < RDoc::Task
- include ::Rake::DSL if defined?(::Rake::DSL)
+ include ::Rake::DSL
def define
task rdoc_task_name
diff --git a/railties/lib/rails/tasks/statistics.rake b/railties/lib/rails/tasks/statistics.rake
index f684e71267..67a6d2d2ac 100644
--- a/railties/lib/rails/tasks/statistics.rake
+++ b/railties/lib/rails/tasks/statistics.rake
@@ -3,6 +3,7 @@ STATS_DIRECTORIES = [
%w(Helpers app/helpers),
%w(Models app/models),
%w(Mailers app/mailers),
+ %w(Javascripts app/assets/javascripts),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Integration\ tests test/integration),