aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/code_statistics.rb2
-rw-r--r--railties/lib/rails/generators/named_base.rb64
-rw-r--r--railties/lib/rails/generators/rails/app/app_generator.rb11
-rw-r--r--railties/lib/rails/generators/rails/app/templates/Gemfile1
-rw-r--r--railties/lib/rails/generators/rails/controller/templates/controller.rb2
-rw-r--r--railties/lib/rails/generators/rails/helper/templates/helper.rb2
-rw-r--r--railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb2
-rw-r--r--railties/lib/rails/generators/test_unit/helper/templates/helper_test.rb2
-rw-r--r--railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb2
-rw-r--r--railties/lib/rails/generators/test_unit/model/templates/unit_test.rb2
-rw-r--r--railties/lib/rails/generators/test_unit/observer/templates/unit_test.rb2
-rw-r--r--railties/lib/rails/test_help.rb8
12 files changed, 88 insertions, 12 deletions
diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb
index 78a4f00ad8..40416dd83a 100644
--- a/railties/lib/rails/code_statistics.rb
+++ b/railties/lib/rails/code_statistics.rb
@@ -23,7 +23,7 @@ class CodeStatistics #:nodoc:
private
def calculate_statistics
- Hash[@pais.mapĀ { |pair| [pair.first, calculate_directory_statistics(pair.last)] }]
+ Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
end
def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb
index 44f831e6f3..9131a19043 100644
--- a/railties/lib/rails/generators/named_base.rb
+++ b/railties/lib/rails/generators/named_base.rb
@@ -5,6 +5,8 @@ module Rails
module Generators
class NamedBase < Base
argument :name, :type => :string
+ class_option :skip_namespace, :type => :boolean, :default => false,
+ :desc => "Skip namespace (affects only isolated applications)"
def initialize(args, *options) #:nodoc:
# Unfreeze name in case it's given as a frozen string
@@ -15,16 +17,72 @@ module Rails
end
protected
-
- attr_reader :class_path, :file_name
+ attr_reader :file_name
alias :singular_name :file_name
+ # Wrap block with namespace of current application
+ # if namespace exists and is not skipped
+ def module_namespacing(&block)
+ inside_namespace do
+ content = capture(&block)
+ content = wrap_with_namespace(content) if namespaced?
+ concat(content)
+ end
+ end
+
+ def indent(content, multiplier = 2)
+ spaces = " " * multiplier
+ content.each_line.map {|line| "#{spaces}#{line}" }.join("\n")
+ end
+
+ def wrap_with_namespace(content)
+ content = indent(content)
+ "module #{namespace.name}\n#{content}\nend\n"
+ end
+
+ def inside_namespace
+ @inside_namespace = true if namespaced?
+ result = yield
+ result
+ ensure
+ @inside_namespace = false
+ end
+
+ def namespace
+ @namespace ||= if defined?(Rails) && Rails.application
+ Rails.application.class.parents.detect { |n| n.respond_to?(:_railtie) }
+ end
+ end
+
+ def namespaced?
+ !options[:skip_namespace] && !!namespace
+ end
+
+ def inside_namespace?
+ @inside_namespace
+ end
+
def file_path
@file_path ||= (class_path + [file_name]).join('/')
end
+ def class_path
+ inside_namespace? || !namespaced? ? regular_class_path : namespaced_class_path
+ end
+
+ def regular_class_path
+ @class_path
+ end
+
+ def namespaced_class_path
+ @namespaced_class_path ||= begin
+ namespace_path = namespace.name.split("::").map {|m| m.underscore }
+ namespace_path + @class_path
+ end
+ end
+
def class_name
- @class_name ||= (class_path + [file_name]).map!{ |m| m.camelize }.join('::')
+ (class_path + [file_name]).map!{ |m| m.camelize }.join('::')
end
def human_name
diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb
index cdff1743ff..2715483914 100644
--- a/railties/lib/rails/generators/rails/app/app_generator.rb
+++ b/railties/lib/rails/generators/rails/app/app_generator.rb
@@ -353,18 +353,21 @@ module Rails
end
def app_name
- @app_name ||= File.basename(destination_root)
+ @app_name ||= defined_app_const_base? ? defined_app_name : File.basename(destination_root)
+ end
+
+ def defined_app_name
+ defined_app_const_base.underscore
end
-
- alias_method :defined_app_name, :app_name
def defined_app_const_base
Rails.respond_to?(:application) && defined?(Rails::Application) &&
Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, "")
end
+ alias :defined_app_const_base? :defined_app_const_base
+
def app_const_base
- defined_app_name # ensures the correct app_name if it's already defined
@app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, '_').squeeze('_').camelize
end
diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile
index d553c09484..1dbf27d978 100644
--- a/railties/lib/rails/generators/rails/app/templates/Gemfile
+++ b/railties/lib/rails/generators/rails/app/templates/Gemfile
@@ -11,6 +11,7 @@ gem 'rails', '<%= Rails::VERSION::STRING %>'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
+# gem 'arel', :git => 'git://github.com/rails/arel.git'
<%- end -%>
<% unless options[:skip_active_record] -%>
diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb
index cda2659e69..c61ea4b510 100644
--- a/railties/lib/rails/generators/rails/controller/templates/controller.rb
+++ b/railties/lib/rails/generators/rails/controller/templates/controller.rb
@@ -1,3 +1,4 @@
+<% module_namespacing do -%>
class <%= class_name %>Controller < ApplicationController
<% for action in actions -%>
def <%= action %>
@@ -5,3 +6,4 @@ class <%= class_name %>Controller < ApplicationController
<% end -%>
end
+<% end -%>
diff --git a/railties/lib/rails/generators/rails/helper/templates/helper.rb b/railties/lib/rails/generators/rails/helper/templates/helper.rb
index 3fe2ecdc74..b4173151b4 100644
--- a/railties/lib/rails/generators/rails/helper/templates/helper.rb
+++ b/railties/lib/rails/generators/rails/helper/templates/helper.rb
@@ -1,2 +1,4 @@
+<% module_namespacing do -%>
module <%= class_name %>Helper
end
+<% end -%>
diff --git a/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb
index 0d4185846d..11a73ebad7 100644
--- a/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb
+++ b/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb
@@ -1,5 +1,6 @@
require 'test_helper'
+<% module_namespacing do -%>
class <%= class_name %>ControllerTest < ActionController::TestCase
<% if actions.empty? -%>
# Replace this with your real tests.
@@ -16,3 +17,4 @@ class <%= class_name %>ControllerTest < ActionController::TestCase
<% end -%>
<% end -%>
end
+<% end -%>
diff --git a/railties/lib/rails/generators/test_unit/helper/templates/helper_test.rb b/railties/lib/rails/generators/test_unit/helper/templates/helper_test.rb
index 591e40900e..7d37bda0f9 100644
--- a/railties/lib/rails/generators/test_unit/helper/templates/helper_test.rb
+++ b/railties/lib/rails/generators/test_unit/helper/templates/helper_test.rb
@@ -1,4 +1,6 @@
require 'test_helper'
+<% module_namespacing do -%>
class <%= class_name %>HelperTest < ActionView::TestCase
end
+<% end -%>
diff --git a/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb
index 80ac7f0feb..b62c7fd279 100644
--- a/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb
+++ b/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb
@@ -1,5 +1,6 @@
require 'test_helper'
+<% module_namespacing do -%>
class <%= class_name %>Test < ActionMailer::TestCase
<% for action in actions -%>
test "<%= action %>" do
@@ -18,3 +19,4 @@ class <%= class_name %>Test < ActionMailer::TestCase
end
<% end -%>
end
+<% end -%>
diff --git a/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb b/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb
index 3e0bc29d3a..6f79879838 100644
--- a/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb
+++ b/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb
@@ -1,8 +1,10 @@
require 'test_helper'
+<% module_namespacing do -%>
class <%= class_name %>Test < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
+<% end -%>
diff --git a/railties/lib/rails/generators/test_unit/observer/templates/unit_test.rb b/railties/lib/rails/generators/test_unit/observer/templates/unit_test.rb
index 03f6d5666e..cd116f5ce9 100644
--- a/railties/lib/rails/generators/test_unit/observer/templates/unit_test.rb
+++ b/railties/lib/rails/generators/test_unit/observer/templates/unit_test.rb
@@ -1,8 +1,10 @@
require 'test_helper'
+<% module_namespacing do -%>
class <%= class_name %>ObserverTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
+<% end -%>
diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb
index ec5e4a357c..38f2f651f4 100644
--- a/railties/lib/rails/test_help.rb
+++ b/railties/lib/rails/test_help.rb
@@ -1,6 +1,6 @@
-# Make double-sure the RAILS_ENV is set to test,
-# so fixtures are loaded to the right database
-abort("Abort testing: Your Rails environment is not running in test mode!") unless Rails.env.test?
+# Make double-sure the RAILS_ENV is not set to production,
+# so fixtures aren't loaded into that environment
+abort("Abort testing: Your Rails environment is running in production mode!") if Rails.env.production?
require 'test/unit'
require 'active_support/core_ext/kernel/requires'
@@ -21,7 +21,7 @@ if defined?(ActiveRecord)
self.fixture_path = "#{Rails.root}/test/fixtures/"
end
- ActionController::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
+ ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
def create_fixtures(*table_names, &block)
Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)