aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG.md2
-rw-r--r--actionpack/actionpack.gemspec2
-rw-r--r--actionpack/lib/abstract_controller/rendering.rb16
-rw-r--r--actionpack/lib/action_controller/model_naming.rb12
-rw-r--r--actionpack/lib/action_controller/record_identifier.rb8
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb17
-rw-r--r--actionpack/lib/action_dispatch/routing/polymorphic_routes.rb14
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb2
-rw-r--r--actionpack/lib/action_view/base.rb17
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb21
-rw-r--r--actionpack/test/dispatch/mapper_test.rb3
-rw-r--r--actionpack/test/dispatch/routing_test.rb49
-rw-r--r--actionpack/test/fixtures/routes/bogus.rb1
-rw-r--r--actionpack/test/fixtures/routes/external.rb1
-rw-r--r--activemodel/lib/active_model/conversion.rb31
-rw-r--r--activemodel/lib/active_model/dirty.rb6
-rw-r--r--activemodel/lib/active_model/errors.rb191
-rw-r--r--activemodel/lib/active_model/lint.rb45
-rw-r--r--activemodel/lib/active_model/model.rb53
-rw-r--r--activemodel/lib/active_model/naming.rb149
-rw-r--r--activerecord/CHANGELOG.md16
-rw-r--r--activerecord/lib/active_record/associations.rb8
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb10
-rw-r--r--activerecord/lib/active_record/railties/databases.rake15
-rw-r--r--activerecord/lib/active_record/relation.rb4
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb12
-rw-r--r--activerecord/lib/active_record/tasks/database_tasks.rb9
-rw-r--r--activerecord/lib/active_record/tasks/mysql_database_tasks.rb4
-rw-r--r--activerecord/lib/active_record/tasks/postgresql_database_tasks.rb4
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb2
-rw-r--r--activerecord/test/cases/migration/change_schema_test.rb18
-rw-r--r--activerecord/test/cases/tasks/database_tasks_test.rb11
-rw-r--r--activerecord/test/cases/tasks/mysql_rake_test.rb18
-rw-r--r--activerecord/test/cases/tasks/postgresql_rake_test.rb18
-rw-r--r--activerecord/test/cases/tasks/sqlite_rake_test.rb21
-rw-r--r--activesupport/CHANGELOG.md2
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/conversions.rb17
-rw-r--r--activesupport/lib/active_support/core_ext/hash/except.rb7
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb25
-rw-r--r--activesupport/lib/active_support/file_update_checker.rb4
-rw-r--r--activesupport/lib/active_support/inflections.rb2
-rw-r--r--activesupport/lib/active_support/inflector/inflections.rb2
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb1
-rw-r--r--activesupport/lib/active_support/values/time_zone.rb3
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb31
-rw-r--r--activesupport/test/file_update_checker_test.rb15
-rw-r--r--guides/source/debugging_rails_applications.textile2
-rw-r--r--guides/source/getting_started.textile4
-rw-r--r--guides/source/performance_testing.textile6
-rw-r--r--guides/source/routing.textile26
-rw-r--r--railties/lib/rails/application.rb130
-rw-r--r--railties/lib/rails/application/routes_reloader.rb13
-rw-r--r--railties/lib/rails/engine.rb130
-rw-r--r--railties/lib/rails/engine/configuration.rb1
-rw-r--r--railties/lib/rails/paths.rb10
-rw-r--r--railties/lib/rails/railtie.rb32
-rw-r--r--railties/test/application/paths_test.rb2
-rw-r--r--railties/test/application/routing_test.rb83
-rw-r--r--railties/test/engine_test.rb10
-rw-r--r--railties/test/paths_test.rb2
-rw-r--r--railties/test/railties/engine_test.rb2
61 files changed, 779 insertions, 593 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 345b5aa330..3e10310179 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Remove Active Model dependency from Action Pack. *Guillermo Iguaran*
+
* Support unicode characters in routes. Route will be automatically escaped, so instead of manually escaping:
get Rack::Utils.escape('こんにちは') => 'home#index'
diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec
index 938f3d6dc2..6075e2f02b 100644
--- a/actionpack/actionpack.gemspec
+++ b/actionpack/actionpack.gemspec
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
s.requirements << 'none'
s.add_dependency('activesupport', version)
- s.add_dependency('activemodel', version)
s.add_dependency('rack-cache', '~> 1.2')
s.add_dependency('builder', '~> 3.0.0')
s.add_dependency('rack', '~> 1.4.1')
@@ -26,5 +25,6 @@ Gem::Specification.new do |s|
s.add_dependency('journey', '~> 1.0.1')
s.add_dependency('erubis', '~> 2.7.0')
+ s.add_development_dependency('activemodel', version)
s.add_development_dependency('tzinfo', '~> 0.3.33')
end
diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb
index 7d73c6af8d..3da2834af0 100644
--- a/actionpack/lib/abstract_controller/rendering.rb
+++ b/actionpack/lib/abstract_controller/rendering.rb
@@ -49,9 +49,19 @@ module AbstractController
module ClassMethods
def view_context_class
@view_context_class ||= begin
- routes = _routes if respond_to?(:_routes)
- helpers = _helpers if respond_to?(:_helpers)
- ActionView::Base.prepare(routes, helpers)
+ routes = respond_to?(:_routes) && _routes
+ helpers = respond_to?(:_helpers) && _helpers
+
+ Class.new(ActionView::Base) do
+ if routes
+ include routes.url_helpers
+ include routes.mounted_helpers
+ end
+
+ if helpers
+ include helpers
+ end
+ end
end
end
end
diff --git a/actionpack/lib/action_controller/model_naming.rb b/actionpack/lib/action_controller/model_naming.rb
new file mode 100644
index 0000000000..785221dc3d
--- /dev/null
+++ b/actionpack/lib/action_controller/model_naming.rb
@@ -0,0 +1,12 @@
+module ActionController
+ module ModelNaming
+ # Converts the given object to an ActiveModel compliant one.
+ def convert_to_model(object)
+ object.respond_to?(:to_model) ? object.to_model : object
+ end
+
+ def model_name_from_record_or_class(record_or_class)
+ (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb
index 16a5decc62..d3ac406618 100644
--- a/actionpack/lib/action_controller/record_identifier.rb
+++ b/actionpack/lib/action_controller/record_identifier.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/module'
+require 'action_controller/model_naming'
module ActionController
# The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
@@ -27,6 +28,8 @@ module ActionController
module RecordIdentifier
extend self
+ include ModelNaming
+
JOIN = '_'.freeze
NEW = 'new'.freeze
@@ -40,7 +43,7 @@ module ActionController
# dom_class(post, :edit) # => "edit_post"
# dom_class(Person, :edit) # => "edit_person"
def dom_class(record_or_class, prefix = nil)
- singular = ActiveModel::Naming.param_key(record_or_class)
+ singular = model_name_from_record_or_class(record_or_class).param_key
prefix ? "#{prefix}#{JOIN}#{singular}" : singular
end
@@ -73,8 +76,7 @@ module ActionController
# method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
# make sure yourself that your dom ids are valid, in case you overwrite this method.
def record_key_for_dom_id(record)
- record = record.to_model if record.respond_to?(:to_model)
- key = record.to_key
+ key = convert_to_model(record).to_key
key ? key.join('_') : key
end
end
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 94242ad962..53a4afecb3 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -1316,22 +1316,6 @@ module ActionDispatch
parent_resource.instance_of?(Resource) && @scope[:shallow]
end
- def draw(name)
- path = @draw_paths.find do |_path|
- File.exists? "#{_path}/#{name}.rb"
- end
-
- unless path
- msg = "Your router tried to #draw the external file #{name}.rb,\n" \
- "but the file was not found in:\n\n"
- msg += @draw_paths.map { |_path| " * #{_path}" }.join("\n")
- raise ArgumentError, msg
- end
-
- route_path = "#{path}/#{name}.rb"
- instance_eval(File.read(route_path), route_path.to_s)
- end
-
# match 'path' => 'controller#action'
# match 'path', to: 'controller#action'
# match 'path', 'otherpath', on: :member, via: :get
@@ -1581,7 +1565,6 @@ module ActionDispatch
def initialize(set) #:nodoc:
@set = set
- @draw_paths = set.draw_paths
@scope = { :path_names => @set.resources_path_names }
end
diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
index 86ce7a83b9..3d7b8878b8 100644
--- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
+++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -1,3 +1,5 @@
+require 'action_controller/model_naming'
+
module ActionDispatch
module Routing
# Polymorphic URL helpers are methods for smart resolution to a named route call when
@@ -53,6 +55,8 @@ module ActionDispatch
# form_for([blog, @post]) # => "/blog/posts/1"
#
module PolymorphicRoutes
+ include ActionController::ModelNaming
+
# Constructs a call to a named RESTful route for the given record and returns the
# resulting URL string. For example:
#
@@ -154,10 +158,6 @@ module ActionDispatch
options[:action] ? "#{options[:action]}_" : ''
end
- def convert_to_model(object)
- object.respond_to?(:to_model) ? object.to_model : object
- end
-
def routing_type(options)
options[:routing_type] || :url
end
@@ -169,7 +169,7 @@ module ActionDispatch
if parent.is_a?(Symbol) || parent.is_a?(String)
parent
else
- ActiveModel::Naming.singular_route_key(parent)
+ model_name_from_record_or_class(parent).singular_route_key
end
end
else
@@ -181,9 +181,9 @@ module ActionDispatch
route << record
elsif record
if inflection == :singular
- route << ActiveModel::Naming.singular_route_key(record)
+ route << model_name_from_record_or_class(record).singular_route_key
else
- route << ActiveModel::Naming.route_key(record)
+ route << model_name_from_record_or_class(record).route_key
end
else
raise ArgumentError, "Nil location provided. Can't build URI."
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index 64b1d58ae9..1d6ca0c78d 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -237,7 +237,6 @@ module ActionDispatch
attr_accessor :formatter, :set, :named_routes, :default_scope, :router
attr_accessor :disable_clear_and_finalize, :resources_path_names
attr_accessor :default_url_options, :request_class, :valid_conditions
- attr_accessor :draw_paths
alias :routes :set
@@ -249,7 +248,6 @@ module ActionDispatch
self.named_routes = NamedRouteCollection.new
self.resources_path_names = self.class.default_resources_path_names.dup
self.default_url_options = {}
- self.draw_paths = []
self.request_class = request_class
@valid_conditions = { :controller => true, :action => true }
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index f98648d930..7bfbc1f0aa 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -148,7 +148,6 @@ module ActionView #:nodoc:
cattr_accessor :prefix_partial_path_with_controller_namespace
@@prefix_partial_path_with_controller_namespace = true
- class_attribute :helpers
class_attribute :_routes
class_attribute :logger
@@ -166,22 +165,6 @@ module ActionView #:nodoc:
def xss_safe? #:nodoc:
true
end
-
- # This method receives routes and helpers from the controller
- # and return a subclass ready to be used as view context.
- def prepare(routes, helpers) #:nodoc:
- Class.new(self) do
- if routes
- include routes.url_helpers
- include routes.mounted_helpers
- end
-
- if helpers
- include helpers
- self.helpers = helpers
- end
- end
- end
end
attr_accessor :view_renderer
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index ba2a26fd09..b34f6c8650 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -12,6 +12,7 @@ require 'active_support/core_ext/string/output_safety'
require 'active_support/core_ext/array/extract_options'
require 'active_support/deprecation'
require 'active_support/core_ext/string/inflections'
+require 'action_controller/model_naming'
module ActionView
# = Action View Form Helpers
@@ -117,11 +118,7 @@ module ActionView
include FormTagHelper
include UrlHelper
-
- # Converts the given object to an ActiveModel compliant one.
- def convert_to_model(object)
- object.respond_to?(:to_model) ? object.to_model : object
- end
+ include ActionController::ModelNaming
# Creates a form that allows the user to create or update the attributes
# of a specific model object.
@@ -411,7 +408,7 @@ module ActionView
object = nil
else
object = record.is_a?(Array) ? record.last : record
- object_name = options[:as] || ActiveModel::Naming.param_key(object)
+ object_name = options[:as] || model_name_from_record_or_class(object).param_key
apply_form_for_options!(record, object, options)
end
@@ -1128,7 +1125,7 @@ module ActionView
object_name = record_name
else
object = record_name
- object_name = ActiveModel::Naming.param_key(object)
+ object_name = model_name_from_record_or_class(object).param_key
end
builder = options[:builder] || default_form_builder
@@ -1142,9 +1139,11 @@ module ActionView
end
class FormBuilder
+ include ActionController::ModelNaming
+
# The methods which wrap a form helper call.
class_attribute :field_helpers
- self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model]
+ self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model, :model_name_from_record_or_class]
attr_accessor :object_name, :object, :options
@@ -1214,7 +1213,7 @@ module ActionView
end
else
record_object = record_name.is_a?(Array) ? record_name.last : record_name
- record_name = ActiveModel::Naming.param_key(record_object)
+ record_name = model_name_from_record_or_class(record_object).param_key
end
index = if options.has_key?(:index)
@@ -1396,10 +1395,6 @@ module ActionView
@nested_child_index[name] ||= -1
@nested_child_index[name] += 1
end
-
- def convert_to_model(object)
- object.respond_to?(:to_model) ? object.to_model : object
- end
end
end
diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb
index bd078d2b21..8070bdec8a 100644
--- a/actionpack/test/dispatch/mapper_test.rb
+++ b/actionpack/test/dispatch/mapper_test.rb
@@ -4,12 +4,11 @@ module ActionDispatch
module Routing
class MapperTest < ActiveSupport::TestCase
class FakeSet
- attr_reader :routes, :draw_paths
+ attr_reader :routes
alias :set :routes
def initialize
@routes = []
- @draw_paths = []
end
def resources_path_names
diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb
index f15dd7214b..fed26d89f8 100644
--- a/actionpack/test/dispatch/routing_test.rb
+++ b/actionpack/test/dispatch/routing_test.rb
@@ -2324,55 +2324,6 @@ class TestNamespaceWithControllerOption < ActionDispatch::IntegrationTest
end
end
-class TestDrawExternalFile < ActionDispatch::IntegrationTest
- class ExternalController < ActionController::Base
- def index
- render :text => "external#index"
- end
- end
-
- DRAW_PATH = File.expand_path('../../fixtures/routes', __FILE__)
-
- DefaultScopeRoutes = ActionDispatch::Routing::RouteSet.new.tap do |app|
- app.draw_paths << DRAW_PATH
- end
-
- def app
- DefaultScopeRoutes
- end
-
- def test_draw_external_file
- DefaultScopeRoutes.draw do
- scope :module => 'test_draw_external_file' do
- draw :external
- end
- end
-
- get '/external'
- assert_equal "external#index", @response.body
- end
-
- def test_draw_nonexistent_file
- exception = assert_raise ArgumentError do
- DefaultScopeRoutes.draw do
- draw :nonexistent
- end
- end
- assert_match 'Your router tried to #draw the external file nonexistent.rb', exception.message
- assert_match DRAW_PATH.to_s, exception.message
- end
-
- def test_draw_bogus_file
- exception = assert_raise NoMethodError do
- DefaultScopeRoutes.draw do
- draw :bogus
- end
- end
- assert_match "undefined method `wrong'", exception.message
- assert_match 'test/fixtures/routes/bogus.rb:1', exception.backtrace.first
- end
-end
-
class TestDefaultScope < ActionDispatch::IntegrationTest
module ::Blog
class PostsController < ActionController::Base
diff --git a/actionpack/test/fixtures/routes/bogus.rb b/actionpack/test/fixtures/routes/bogus.rb
deleted file mode 100644
index 41fbf0cd64..0000000000
--- a/actionpack/test/fixtures/routes/bogus.rb
+++ /dev/null
@@ -1 +0,0 @@
-wrong :route
diff --git a/actionpack/test/fixtures/routes/external.rb b/actionpack/test/fixtures/routes/external.rb
deleted file mode 100644
index d103c39f53..0000000000
--- a/actionpack/test/fixtures/routes/external.rb
+++ /dev/null
@@ -1 +0,0 @@
-get '/external' => 'external#index'
diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb
index d7f30f0920..89d87a8b6f 100644
--- a/activemodel/lib/active_model/conversion.rb
+++ b/activemodel/lib/active_model/conversion.rb
@@ -18,17 +18,23 @@ module ActiveModel
# end
#
# cm = ContactMessage.new
- # cm.to_model == self # => true
- # cm.to_key # => nil
- # cm.to_param # => nil
- # cm.to_partial_path # => "contact_messages/contact_message"
- #
+ # cm.to_model == cm # => true
+ # cm.to_key # => nil
+ # cm.to_param # => nil
+ # cm.to_partial_path # => "contact_messages/contact_message"
module Conversion
extend ActiveSupport::Concern
# If your object is already designed to implement all of the Active Model
# you can use the default <tt>:to_model</tt> implementation, which simply
- # returns self.
+ # returns +self+.
+ #
+ # class Person
+ # include ActiveModel::Conversion
+ # end
+ #
+ # person = Person.new
+ # person.to_model == person # => true
#
# If your model does not act like an Active Model object, then you should
# define <tt>:to_model</tt> yourself returning a proxy object that wraps
@@ -37,21 +43,28 @@ module ActiveModel
self
end
- # Returns an Enumerable of all key attributes if any is set, regardless
- # if the object is persisted or not.
+ # Returns an Enumerable of all key attributes if any is set, regardless if
+ # the object is persisted or not. If there no key attributes, returns +nil+.
def to_key
key = respond_to?(:id) && id
key ? [key] : nil
end
# Returns a string representing the object's key suitable for use in URLs,
- # or nil if <tt>persisted?</tt> is false.
+ # or +nil+ if <tt>persisted?</tt> is false.
def to_param
persisted? ? to_key.join('-') : nil
end
# Returns a string identifying the path associated with the object.
# ActionPack uses this to find a suitable partial to represent the object.
+ #
+ # class Person
+ # include ActiveModel::Conversion
+ # end
+ #
+ # person = Person.new
+ # person.to_partial_path # => "people/person"
def to_partial_path
self.class._to_partial_path
end
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index 7014d8114f..9bd5f903cd 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -80,8 +80,8 @@ module ActiveModel
# person.changes # => {"name" => ["Bill", "Bob"]}
#
# If an attribute is modified in-place then make use of <tt>[attribute_name]_will_change!</tt>
- # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to
- # in-place attributes.
+ # to mark that the attribute is changing. Otherwise ActiveModel can't track
+ # changes to in-place attributes.
#
# person.name_will_change!
# person.name_change # => ["Bill", "Bill"]
@@ -115,7 +115,7 @@ module ActiveModel
end
# Returns a hash of changed attributes indicating their original
- # and new values like <tt>attr => [original value, new value]</tt>.
+ # and new values like <tt>attr => [original value, new value]</tt>.
#
# person.changes # => {}
# person.name = 'bob'
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 567677bbb9..4ed3462e7e 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -54,8 +54,8 @@ module ActiveModel
# The above allows you to do:
#
# p = Person.new
- # p.validate! # => ["can not be nil"]
- # p.errors.full_messages # => ["name can not be nil"]
+ # person.validate! # => ["can not be nil"]
+ # person.errors.full_messages # => ["name can not be nil"]
# # etc..
class Errors
include Enumerable
@@ -81,28 +81,50 @@ module ActiveModel
super
end
- # Clear the messages
+ # Clear the error messages.
+ #
+ # person.errors.full_messages # => ["name can not be nil"]
+ # person.errors.clear
+ # person.errors.full_messages # => []
def clear
messages.clear
end
- # Do the error messages include an error with key +error+?
+ # Returns +true+ if the error messages include an error for the given key
+ # +attribute+, +false+ otherwise.
+ #
+ # person.errors.messages # => { :name => ["can not be nil"] }
+ # person.errors.include?(:name) # => true
+ # person.errors.include?(:age) # => false
def include?(attribute)
(v = messages[attribute]) && v.any?
end
+ # aliases include?
alias :has_key? :include?
- # Get messages for +key+
+ # Get messages for +key+.
+ #
+ # person.errors.messages # => { :name => ["can not be nil"] }
+ # person.errors.get(:name) # => ["can not be nil"]
+ # person.errors.get(:age) # => nil
def get(key)
messages[key]
end
- # Set messages for +key+ to +value+
+ # Set messages for +key+ to +value+.
+ #
+ # person.errors.get(:name) # => ["can not be nil"]
+ # person.errors.set(:name, ["can't be nil"])
+ # person.errors.get(:name) # => ["can't be nil"]
def set(key, value)
messages[key] = value
end
- # Delete messages for +key+
+ # Delete messages for +key+. Returns the deleted messages.
+ #
+ # person.errors.get(:name) # => ["can not be nil"]
+ # person.errors.delete(:name) # => ["can not be nil"]
+ # person.errors.get(:name) # => nil
def delete(key)
messages.delete(key)
end
@@ -110,16 +132,16 @@ module ActiveModel
# When passed a symbol or a name of a method, returns an array of errors
# for the method.
#
- # p.errors[:name] # => ["can not be nil"]
- # p.errors['name'] # => ["can not be nil"]
+ # person.errors[:name] # => ["can not be nil"]
+ # person.errors['name'] # => ["can not be nil"]
def [](attribute)
get(attribute.to_sym) || set(attribute.to_sym, [])
end
# Adds to the supplied attribute the supplied error message.
#
- # p.errors[:name] = "must be set"
- # p.errors[:name] # => ['must be set']
+ # person.errors[:name] = "must be set"
+ # person.errors[:name] # => ['must be set']
def []=(attribute, error)
self[attribute] << error
end
@@ -128,13 +150,13 @@ module ActiveModel
# Yields the attribute and the error for that attribute. If the attribute
# has more than one error message, yields once for each error message.
#
- # p.errors.add(:name, "can't be blank")
- # p.errors.each do |attribute, error|
+ # person.errors.add(:name, "can't be blank")
+ # person.errors.each do |attribute, error|
# # Will yield :name and "can't be blank"
# end
#
- # p.errors.add(:name, "must be specified")
- # p.errors.each do |attribute, error|
+ # person.errors.add(:name, "must be specified")
+ # person.errors.each do |attribute, error|
# # Will yield :name and "can't be blank"
# # then yield :name and "must be specified"
# end
@@ -146,54 +168,65 @@ module ActiveModel
# Returns the number of error messages.
#
- # p.errors.add(:name, "can't be blank")
- # p.errors.size # => 1
- # p.errors.add(:name, "must be specified")
- # p.errors.size # => 2
+ # person.errors.add(:name, "can't be blank")
+ # person.errors.size # => 1
+ # person.errors.add(:name, "must be specified")
+ # person.errors.size # => 2
def size
values.flatten.size
end
- # Returns all message values
+ # Returns all message values.
+ #
+ # person.errors.messages # => { :name => ["can not be nil", "must be specified"] }
+ # person.errors.values # => [["can not be nil", "must be specified"]]
def values
messages.values
end
- # Returns all message keys
+ # Returns all message keys.
+ #
+ # person.errors.messages # => { :name => ["can not be nil", "must be specified"] }
+ # person.errors.keys # => [:name]
def keys
messages.keys
end
- # Returns an array of error messages, with the attribute name included
+ # Returns an array of error messages, with the attribute name included.
#
- # p.errors.add(:name, "can't be blank")
- # p.errors.add(:name, "must be specified")
- # p.errors.to_a # => ["name can't be blank", "name must be specified"]
+ # person.errors.add(:name, "can't be blank")
+ # person.errors.add(:name, "must be specified")
+ # person.errors.to_a # => ["name can't be blank", "name must be specified"]
def to_a
full_messages
end
# Returns the number of error messages.
- # p.errors.add(:name, "can't be blank")
- # p.errors.count # => 1
- # p.errors.add(:name, "must be specified")
- # p.errors.count # => 2
+ #
+ # person.errors.add(:name, "can't be blank")
+ # person.errors.count # => 1
+ # person.errors.add(:name, "must be specified")
+ # person.errors.count # => 2
def count
to_a.size
end
- # Returns true if no errors are found, false otherwise.
+ # Returns +true+ if no errors are found, +false+ otherwise.
# If the error message is a string it can be empty.
+ #
+ # person.errors.full_messages # => ["name can not be nil"]
+ # person.errors.empty? # => false
def empty?
all? { |k, v| v && v.empty? && !v.is_a?(String) }
end
+ # aliases empty?
alias_method :blank?, :empty?
# Returns an xml formatted representation of the Errors hash.
#
- # p.errors.add(:name, "can't be blank")
- # p.errors.add(:name, "must be specified")
- # p.errors.to_xml
+ # person.errors.add(:name, "can't be blank")
+ # person.errors.add(:name, "must be specified")
+ # person.errors.to_xml
# # =>
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
# # <errors>
@@ -204,14 +237,21 @@ module ActiveModel
to_a.to_xml({ :root => "errors", :skip_types => true }.merge!(options))
end
- # Returns an Hash that can be used as the JSON representation for this object.
- # Options:
- # * <tt>:full_messages</tt> - determines if json object should contain
- # full messages or not. Default: <tt>false</tt>.
+ # Returns a Hash that can be used as the JSON representation for this
+ # object. You can pass the <tt>:full_messages</tt> option. This determines
+ # if the json object should contain full messages or not (false by default).
+ #
+ # person.as_json # => { :name => ["can not be nil"] }
+ # person.as_json(full_messages: true) # => { :name => ["name can not be nil"] }
def as_json(options=nil)
to_hash(options && options[:full_messages])
end
+ # Returns a Hash of attributes with their error messages. If +full_messages+
+ # is +true+, it will contain full messages (see +full_message+).
+ #
+ # person.to_hash # => { :name => ["can not be nil"] }
+ # person.to_hash(true) # => { :name => ["name can not be nil"] }
def to_hash(full_messages = false)
if full_messages
messages = {}
@@ -224,12 +264,31 @@ module ActiveModel
end
end
- # Adds +message+ to the error messages on +attribute+. More than one error can be added to the same
- # +attribute+.
- # If no +message+ is supplied, <tt>:invalid</tt> is assumed.
+ # Adds +message+ to the error messages on +attribute+. More than one error
+ # can be added to the same +attribute+. If no +message+ is supplied,
+ # <tt>:invalid</tt> is assumed.
#
- # If +message+ is a symbol, it will be translated using the appropriate scope (see +generate_message+).
- # If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error.
+ # person.errors.add(:name)
+ # # => ["is invalid"]
+ # person.errors.add(:name, 'must be implemented')
+ # # => ["is invalid", "must be implemented"]
+ #
+ # person.errors.messages
+ # # => { :name => ["must be implemented", "is invalid"] }
+ #
+ # If +message+ is a symbol, it will be translated using the appropriate
+ # scope (see +generate_message+).
+ #
+ # If +message+ is a proc, it will be called, allowing for things like
+ # <tt>Time.now</tt> to be used within an error.
+ #
+ # If the <tt>:strict</tt> option is set to true will raise
+ # ActiveModel::StrictValidationFailed instead of adding the error.
+ #
+ # person.errors.add(:name, nil, strict: true)
+ # # => ActiveModel::StrictValidationFailed: name is invalid
+ #
+ # person.errors.messages # => {}
def add(attribute, message = nil, options = {})
message = normalize_message(attribute, message, options)
if options[:strict]
@@ -239,7 +298,12 @@ module ActiveModel
self[attribute] << message
end
- # Will add an error message to each of the attributes in +attributes+ that is empty.
+ # Will add an error message to each of the attributes in +attributes+
+ # that is empty.
+ #
+ # person.errors.add_on_empty(:name)
+ # person.errors.messages
+ # # => { :name => ["can't be empty"] }
def add_on_empty(attributes, options = {})
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
@@ -248,7 +312,12 @@ module ActiveModel
end
end
- # Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).
+ # Will add an error message to each of the attributes in +attributes+ that
+ # is blank (using Object#blank?).
+ #
+ # person.errors.add_on_blank(:name)
+ # person.errors.messages
+ # # => { :name => ["can't be blank"] }
def add_on_blank(attributes, options = {})
[attributes].flatten.each do |attribute|
value = @base.send(:read_attribute_for_validation, attribute)
@@ -256,10 +325,11 @@ module ActiveModel
end
end
- # Returns true if an error on the attribute with the given message is present, false otherwise.
- # +message+ is treated the same as for +add+.
- # p.errors.add :name, :blank
- # p.errors.added? :name, :blank # => true
+ # Returns +true+ if an error on the attribute with the given message is
+ # present, +false+ otherwise. +message+ is treated the same as for +add+.
+ #
+ # person.errors.add :name, :blank
+ # person.errors.added? :name, :blank # => true
def added?(attribute, message = nil, options = {})
message = normalize_message(attribute, message, options)
self[attribute].include? message
@@ -267,22 +337,21 @@ module ActiveModel
# Returns all the full error messages in an array.
#
- # class Company
+ # class Person
# validates_presence_of :name, :address, :email
- # validates_length_of :name, :in => 5..30
+ # validates_length_of :name, in: 5..30
# end
#
- # company = Company.create(:address => '123 First St.')
- # company.errors.full_messages # =>
- # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
+ # person = Person.create(address: '123 First St.')
+ # person.errors.full_messages
+ # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
def full_messages
map { |attribute, message| full_message(attribute, message) }
end
# Returns a full message for a given attribute.
#
- # company.errors.full_message(:name, "is invalid") # =>
- # "Name is invalid"
+ # person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
def full_message(attribute, message)
return message if attribute == :base
attr_name = attribute.to_s.tr('.', '_').humanize
@@ -298,10 +367,11 @@ module ActiveModel
# (<tt>activemodel.errors.messages</tt>).
#
# Error messages are first looked up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>,
- # if it's not there, it's looked up in <tt>models.MODEL.MESSAGE</tt> and if that is not
- # there also, it returns the translation of the default message
- # (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model name,
- # translated attribute name and the value are available for interpolation.
+ # if it's not there, it's looked up in <tt>models.MODEL.MESSAGE</tt> and if
+ # that is not there also, it returns the translation of the default message
+ # (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model
+ # name, translated attribute name and the value are available for
+ # interpolation.
#
# When using inheritance in your models, it will check all the inherited
# models too, but only if the model itself hasn't been found. Say you have
@@ -317,7 +387,6 @@ module ActiveModel
# * <tt>activemodel.errors.messages.blank</tt>
# * <tt>errors.attributes.title.blank</tt>
# * <tt>errors.messages.blank</tt>
- #
def generate_message(attribute, type = :invalid, options = {})
type = options.delete(:message) if options[:message].is_a?(Symbol)
@@ -366,6 +435,8 @@ module ActiveModel
end
end
+ # Raised when a validation cannot be corrected by end users and are considered
+ # exceptional.
class StrictValidationFailed < StandardError
end
end
diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb
index 88b730626c..550fa474ea 100644
--- a/activemodel/lib/active_model/lint.rb
+++ b/activemodel/lib/active_model/lint.rb
@@ -12,19 +12,20 @@ module ActiveModel
# you want all features out of the box.
#
# These tests do not attempt to determine the semantic correctness of the
- # returned values. For instance, you could implement valid? to always
- # return true, and the tests would pass. It is up to you to ensure that
- # the values are semantically meaningful.
+ # returned values. For instance, you could implement <tt>valid?</tt> to
+ # always return true, and the tests would pass. It is up to you to ensure
+ # that the values are semantically meaningful.
#
- # Objects you pass in are expected to return a compliant object from a
- # call to to_model. It is perfectly fine for to_model to return self.
+ # Objects you pass in are expected to return a compliant object from a call
+ # to <tt>to_model</tt>. It is perfectly fine for <tt>to_model</tt> to return
+ # +self+.
module Tests
# == Responds to <tt>to_key</tt>
#
# Returns an Enumerable of all (primary) key attributes
- # or nil if model.persisted? is false. This is used by
- # dom_id to generate unique ids for the object.
+ # or nil if <tt>model.persisted?</tt> is false. This is used by
+ # <tt>dom_id</tt> to generate unique ids for the object.
def test_to_key
assert model.respond_to?(:to_key), "The model should respond to to_key"
def model.persisted?() false end
@@ -34,13 +35,14 @@ module ActiveModel
# == Responds to <tt>to_param</tt>
#
# Returns a string representing the object's key suitable for use in URLs
- # or nil if model.persisted? is false.
+ # or +nil+ if <tt>model.persisted?</tt> is +false+.
#
- # Implementers can decide to either raise an exception or provide a default
- # in case the record uses a composite primary key. There are no tests for this
- # behavior in lint because it doesn't make sense to force any of the possible
- # implementation strategies on the implementer. However, if the resource is
- # not persisted?, then to_param should always return nil.
+ # Implementers can decide to either raise an exception or provide a
+ # default in case the record uses a composite primary key. There are no
+ # tests for this behavior in lint because it doesn't make sense to force
+ # any of the possible implementation strategies on the implementer.
+ # However, if the resource is not persisted?, then <tt>to_param</tt>
+ # should always return +nil+.
def test_to_param
assert model.respond_to?(:to_param), "The model should respond to to_param"
def model.to_key() [1] end
@@ -50,9 +52,8 @@ module ActiveModel
# == Responds to <tt>to_partial_path</tt>
#
- # Returns a string giving a relative path. This is used for looking up
+ # Returns a string giving a relative path. This is used for looking up
# partials. For example, a BlogPost model might return "blog_posts/blog_post"
- #
def test_to_partial_path
assert model.respond_to?(:to_partial_path), "The model should respond to to_partial_path"
assert_kind_of String, model.to_partial_path
@@ -60,11 +61,11 @@ module ActiveModel
# == Responds to <tt>persisted?</tt>
#
- # Returns a boolean that specifies whether the object has been persisted yet.
- # This is used when calculating the URL for an object. If the object is
- # not persisted, a form for that object, for instance, will route to the
- # create action. If it is persisted, a form for the object will routes to
- # the update action.
+ # Returns a boolean that specifies whether the object has been persisted
+ # yet. This is used when calculating the URL for an object. If the object
+ # is not persisted, a form for that object, for instance, will route to
+ # the create action. If it is persisted, a form for the object will routes
+ # to the update action.
def test_persisted?
assert model.respond_to?(:persisted?), "The model should respond to persisted?"
assert_boolean model.persisted?, "persisted?"
@@ -73,8 +74,8 @@ module ActiveModel
# == Naming
#
# Model.model_name must return a string with some convenience methods:
- # :human, :singular, and :plural. Check ActiveModel::Naming for more information.
- #
+ # <tt>:human</tt>, <tt>:singular</tt> and <tt>:plural</tt>. Check
+ # ActiveModel::Naming for more information.
def test_model_naming
assert model.class.respond_to?(:model_name), "The model should respond to model_name"
model_name = model.class.model_name
diff --git a/activemodel/lib/active_model/model.rb b/activemodel/lib/active_model/model.rb
index cb0a051ca5..33a530e6bd 100644
--- a/activemodel/lib/active_model/model.rb
+++ b/activemodel/lib/active_model/model.rb
@@ -2,11 +2,11 @@ module ActiveModel
# == Active Model Basic Model
#
- # Includes the required interface for an object to interact with <tt>ActionPack</tt>,
- # using different <tt>ActiveModel</tt> modules. It includes model name introspections,
- # conversions, translations and validations. Besides that, it allows you to
- # initialize the object with a hash of attributes, pretty much like
- # <tt>ActiveRecord</tt> does.
+ # Includes the required interface for an object to interact with
+ # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules.
+ # It includes model name introspections, conversions, translations and
+ # validations. Besides that, it allows you to initialize the object with a
+ # hash of attributes, pretty much like <tt>ActiveRecord</tt> does.
#
# A minimal implementation could be:
#
@@ -15,13 +15,13 @@ module ActiveModel
# attr_accessor :name, :age
# end
#
- # person = Person.new(:name => 'bob', :age => '18')
+ # person = Person.new(name: 'bob', age: '18')
# person.name # => 'bob'
- # person.age # => 18
+ # person.age # => 18
#
- # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt> to
- # return <tt>false</tt>, which is the most common case. You may want to override it
- # in your class to simulate a different scenario:
+ # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt>
+ # to return +false+, which is the most common case. You may want to override
+ # it in your class to simulate a different scenario:
#
# class Person
# include ActiveModel::Model
@@ -32,11 +32,12 @@ module ActiveModel
# end
# end
#
- # person = Person.new(:id => 1, :name => 'bob')
+ # person = Person.new(id: 1, name: 'bob')
# person.persisted? # => true
#
- # Also, if for some reason you need to run code on <tt>initialize</tt>, make sure you
- # call super if you want the attributes hash initialization to happen.
+ # Also, if for some reason you need to run code on <tt>initialize</tt>, make
+ # sure you call +super+ if you want the attributes hash initialization to
+ # happen.
#
# class Person
# include ActiveModel::Model
@@ -48,11 +49,12 @@ module ActiveModel
# end
# end
#
- # person = Person.new(:id => 1, :name => 'bob')
+ # person = Person.new(id: 1, name: 'bob')
# person.omg # => true
#
- # For more detailed information on other functionalities available, please refer
- # to the specific modules included in <tt>ActiveModel::Model</tt> (see below).
+ # For more detailed information on other functionalities available, please
+ # refer to the specific modules included in <tt>ActiveModel::Model</tt>
+ # (see below).
module Model
def self.included(base) #:nodoc:
base.class_eval do
@@ -63,12 +65,31 @@ module ActiveModel
end
end
+ # Initializes a new model with the given +params+.
+ #
+ # class Person
+ # include ActiveModel::Model
+ # attr_accessor :name, :age
+ # end
+ #
+ # person = Person.new(name: 'bob', age: '18')
+ # person.name # => "bob"
+ # person.age # => 18
def initialize(params={})
params.each do |attr, value|
self.public_send("#{attr}=", value)
end if params
end
+ # Indicates if the model is persisted. Default is +false+.
+ #
+ # class Person
+ # include ActiveModel::Model
+ # attr_accessor :id, :name
+ # end
+ #
+ # person = Person.new(id: 1, name: 'bob')
+ # person.persisted? # => false
def persisted?
false
end
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index 2b5fc57a3a..ea4f9341c6 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -14,9 +14,137 @@ module ActiveModel
alias_method :cache_key, :collection
+ ##
+ # :method: ==
+ #
+ # :call-seq:
+ # ==(other)
+ #
+ # Equivalent to <tt>String#==</tt>. Returns +true+ if the class name and
+ # +other+ are equal, otherwise +false+.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name == 'BlogPost' # => true
+ # BlogPost.model_name == 'Blog Post' # => false
+
+ ##
+ # :method: ===
+ #
+ # :call-seq:
+ # ===(other)
+ #
+ # Equivalent to <tt>#==</tt>.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name === 'BlogPost' # => true
+ # BlogPost.model_name === 'Blog Post' # => false
+
+ ##
+ # :method: <=>
+ #
+ # :call-seq:
+ # ==(other)
+ #
+ # Equivalent to <tt>String#<=></tt>.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name <=> 'BlogPost' # => 0
+ # BlogPost.model_name <=> 'Blog' # => 1
+ # BlogPost.model_name <=> 'BlogPosts' # => -1
+
+ ##
+ # :method: =~
+ #
+ # :call-seq:
+ # =~(regexp)
+ #
+ # Equivalent to <tt>String#=~</tt>. Match the class name against the given
+ # regexp. Returns the position where the match starts or +nil+ if there is
+ # no match.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name =~ /Post/ # => 4
+ # BlogPost.model_name =~ /\d/ # => nil
+
+ ##
+ # :method: !~
+ #
+ # :call-seq:
+ # !~(regexp)
+ #
+ # Equivalent to <tt>String#!~</tt>. Match the class name against the given
+ # regexp. Returns +true+ if there is no match, otherwise +false+.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name !~ /Post/ # => false
+ # BlogPost.model_name !~ /\d/ # => true
+
+ ##
+ # :method: eql?
+ #
+ # :call-seq:
+ # eql?(other)
+ #
+ # Equivalent to <tt>String#eql?</tt>. Returns +true+ if the class name and
+ # +other+ have the same length and content, otherwise +false+.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name.eql?('BlogPost') # => true
+ # BlogPost.model_name.eql?('Blog Post') # => false
+
+ ##
+ # :method: to_s
+ #
+ # :call-seq:
+ # to_s()
+ #
+ # Returns the class name.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
+ #
+ # BlogPost.model_name.to_s # => "BlogPost"
+
+ ##
+ # :method: to_str
+ #
+ # :call-seq:
+ # to_str()
+ #
+ # Equivalent to +to_s+.
delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s,
:to_str, :to => :name
+ # Returns a new ActiveModel::Name instance. By default, the +namespace+
+ # and +name+ option will take the namespace and name of the given class
+ # respectively.
+ #
+ # module Foo
+ # class Bar
+ # end
+ # end
+ #
+ # ActiveModel::Name.new(Foo::Bar).to_s
+ # # => "Foo::Bar"
def initialize(klass, namespace = nil, name = nil)
@name = name || klass.name
@@ -38,7 +166,11 @@ module ActiveModel
end
# Transform the model name into a more humane format, using I18n. By default,
- # it will underscore then humanize the class name
+ # it will underscore then humanize the class name.
+ #
+ # class BlogPost
+ # extend ActiveModel::Naming
+ # end
#
# BlogPost.model_name.human # => "Blog post"
#
@@ -82,11 +214,12 @@ module ActiveModel
# BookModule::BookCover.model_name.i18n_key # => :"book_module/book_cover"
#
# Providing the functionality that ActiveModel::Naming provides in your object
- # is required to pass the Active Model Lint test. So either extending the provided
- # method below, or rolling your own is required.
+ # is required to pass the Active Model Lint test. So either extending the
+ # provided method below, or rolling your own is required.
module Naming
# Returns an ActiveModel::Name object for module. It can be
- # used to retrieve all kinds of naming-related information.
+ # used to retrieve all kinds of naming-related information
+ # (See ActiveModel::Name for more information).
def model_name
@_model_name ||= begin
namespace = self.parents.detect do |n|
@@ -96,7 +229,7 @@ module ActiveModel
end
end
- # Returns the plural class name of a record or class. Examples:
+ # Returns the plural class name of a record or class.
#
# ActiveModel::Naming.plural(post) # => "posts"
# ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
@@ -104,7 +237,7 @@ module ActiveModel
model_name_from_record_or_class(record_or_class).plural
end
- # Returns the singular class name of a record or class. Examples:
+ # Returns the singular class name of a record or class.
#
# ActiveModel::Naming.singular(post) # => "post"
# ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
@@ -112,10 +245,10 @@ module ActiveModel
model_name_from_record_or_class(record_or_class).singular
end
- # Identifies whether the class name of a record or class is uncountable. Examples:
+ # Identifies whether the class name of a record or class is uncountable.
#
# ActiveModel::Naming.uncountable?(Sheep) # => true
- # ActiveModel::Naming.uncountable?(Post) => false
+ # ActiveModel::Naming.uncountable?(Post) # => false
def self.uncountable?(record_or_class)
plural(record_or_class) == singular(record_or_class)
end
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 7a3dfd34dc..73f9212371 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,21 @@
## Rails 4.0.0 (unreleased) ##
+* Add `:default` and `:null` options to `column_exists?`.
+
+ column_exists?(:testings, :taggable_id, :integer, null: false)
+ column_exists?(:testings, :taggable_type, :string, default: 'Photo')
+
+ *Aleksey Magusev*
+
+* `ActiveRelation#inspect` no longer calls `#to_a`. This means that in places
+ where `#inspect` is implied (such as in the console), creating a relation
+ will not execute it anymore, you'll have to call `#to_a` when necessary:
+
+ User.where(:age => 30) # => returns the relation
+ User.where(:age => 30).to_a # => executes the query and returns the loaded objects, as before
+
+ *Brian Cardarella*
+
* Add `collate` and `ctype` support to PostgreSQL. These are available for PostgreSQL 8.4 or later.
Example:
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 68f8bbeb1c..a62fce4756 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1167,6 +1167,8 @@ module ActiveRecord
# If true, always save the associated objects or destroy them if marked for destruction,
# when saving the parent object. If false, never save or destroy the associated objects.
# By default, only save associated objects that are new records.
+ #
+ # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>.
# [:inverse_of]
# Specifies the name of the <tt>belongs_to</tt> association on the associated object
# that is the inverse of this <tt>has_many</tt> association. Does not work in combination
@@ -1288,6 +1290,8 @@ module ActiveRecord
# If true, always save the associated object or destroy it if marked for destruction,
# when saving the parent object. If false, never save or destroy the associated object.
# By default, only save the associated object if it's a new record.
+ #
+ # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>.
# [:inverse_of]
# Specifies the name of the <tt>belongs_to</tt> association on the associated object
# that is the inverse of this <tt>has_one</tt> association. Does not work in combination
@@ -1404,6 +1408,8 @@ module ActiveRecord
# saving the parent object.
# If false, never save or destroy the associated object.
# By default, only save the associated object if it's a new record.
+ #
+ # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>.
# [:touch]
# If true, the associated object will be touched (the updated_at/on attributes set to now)
# when this record is either saved or destroyed. If you specify a symbol, that attribute
@@ -1589,6 +1595,8 @@ module ActiveRecord
# If false, never save or destroy the associated objects.
# By default, only save associated objects that are new records.
#
+ # Note that <tt>accepts_nested_attributes_for</tt> sets <tt>:autosave</tt> to <tt>true</tt>.
+ #
# Option examples:
# has_and_belongs_to_many :projects
# has_and_belongs_to_many :projects, :include => [ :milestones, :manager ]
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index f5794a4e54..2b0ba2f479 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -68,10 +68,12 @@ module ActiveRecord
# column_exists?(:suppliers, :name, :string, :limit => 100)
def column_exists?(table_name, column_name, type = nil, options = {})
columns(table_name).any?{ |c| c.name == column_name.to_s &&
- (!type || c.type == type) &&
- (!options[:limit] || c.limit == options[:limit]) &&
- (!options[:precision] || c.precision == options[:precision]) &&
- (!options[:scale] || c.scale == options[:scale]) }
+ (!type || c.type == type) &&
+ (!options.key?(:limit) || c.limit == options[:limit]) &&
+ (!options.key?(:precision) || c.precision == options[:precision]) &&
+ (!options.key?(:scale) || c.scale == options[:scale]) &&
+ (!options.key?(:default) || c.default == options[:default]) &&
+ (!options.key?(:null) || c.null == options[:null]) }
end
# Creates a new table with the name +table_name+. +table_name+ may either
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake
index c6ec946b73..78ecb1cdc5 100644
--- a/activerecord/lib/active_record/railties/databases.rake
+++ b/activerecord/lib/active_record/railties/databases.rake
@@ -148,13 +148,10 @@ db_namespace = namespace :db do
# desc "Retrieves the collation for the current environment's database"
task :collation => [:environment, :load_config] do
- config = ActiveRecord::Base.configurations[Rails.env || 'development']
- case config['adapter']
- when /mysql/
- ActiveRecord::Base.establish_connection(config)
- puts ActiveRecord::Base.connection.collation
- else
- $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
+ begin
+ puts ActiveRecord::Tasks::DatabaseTasks.collation_current
+ rescue NoMethodError
+ $stderr.puts 'Sorry, your database adapter is not supported yet, feel free to submit a patch'
end
end
@@ -274,7 +271,7 @@ db_namespace = namespace :db do
ENV['ISC_USER'] = config['username'].to_s if config['username']
ENV['ISC_PASSWORD'] = config['password'].to_s if config['password']
end
-
+
def firebird_db_string(config)
FireRuby::Database.db_string_for(config.symbolize_keys)
end
@@ -435,7 +432,7 @@ namespace :railties do
task :migrations => :'db:load_config' do
to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip }
railties = {}
- Rails.application.railties.all do |railtie|
+ Rails.application.railties.each do |railtie|
next unless to_load == :all || to_load.include?(railtie.railtie_name)
if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index fe3aa00a74..e268d451e0 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -492,10 +492,6 @@ module ActiveRecord
end
end
- def inspect
- to_a.inspect
- end
-
def pretty_print(q)
q.pp(self.to_a)
end
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index e6a67b76fe..974cd326ef 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -146,8 +146,8 @@ module ActiveRecord
to_a
end
- # Returns true if a record exists in the table that matches the +id+ or
- # conditions given, or false otherwise. The argument can take five forms:
+ # Returns +true+ if a record exists in the table that matches the +id+ or
+ # conditions given, or +false+ otherwise. The argument can take six forms:
#
# * Integer - Finds the record with this primary key.
# * String - Finds the record with a primary key corresponding to this
@@ -155,8 +155,9 @@ module ActiveRecord
# * Array - Finds the record that matches these +find+-style conditions
# (such as <tt>['color = ?', 'red']</tt>).
# * Hash - Finds the record that matches these +find+-style conditions
- # (such as <tt>{:color => 'red'}</tt>).
- # * No args - Returns false if the table is empty, true otherwise.
+ # (such as <tt>{color: 'red'}</tt>).
+ # * +false+ - Returns always +false+.
+ # * No args - Returns +false+ if the table is empty, +true+ otherwise.
#
# For more information about specifying conditions as a Hash or Array,
# see the Conditions section in the introduction to ActiveRecord::Base.
@@ -168,7 +169,8 @@ module ActiveRecord
# Person.exists?(5)
# Person.exists?('5')
# Person.exists?(['name LIKE ?', "%#{query}%"])
- # Person.exists?(:name => "David")
+ # Person.exists?(name: 'David')
+ # Person.exists?(false)
# Person.exists?
def exists?(conditions = :none)
conditions = conditions.id if ActiveRecord::Model === conditions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index 999b2ebc85..f1241502f5 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -56,6 +56,15 @@ module ActiveRecord
class_for_adapter(configuration['adapter']).new(*arguments).charset
end
+ def collation_current(environment = Rails.env)
+ collation ActiveRecord::Base.configurations[environment]
+ end
+
+ def collation(*arguments)
+ configuration = arguments.first
+ class_for_adapter(configuration['adapter']).new(*arguments).collation
+ end
+
def purge(configuration)
class_for_adapter(configuration['adapter']).new(configuration).purge
end
diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
index b39cd2282f..bf62dfd5b5 100644
--- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -44,6 +44,10 @@ module ActiveRecord
connection.charset
end
+ def collation
+ connection.collation
+ end
+
def structure_dump(filename)
establish_connection configuration
File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
index a210392e53..4139460273 100644
--- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
@@ -29,6 +29,10 @@ module ActiveRecord
connection.encoding
end
+ def collation
+ connection.collate
+ end
+
def purge
clear_active_connections!
drop
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index b8481175b2..3ea6201d60 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -193,7 +193,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_no_sql_should_be_fired_if_association_already_loaded
Car.create(:name => 'honda')
bulbs = Car.first.bulbs
- bulbs.inspect # to load all instances of bulbs
+ bulbs.to_a # to load all instances of bulbs
assert_no_queries do
bulbs.first()
diff --git a/activerecord/test/cases/migration/change_schema_test.rb b/activerecord/test/cases/migration/change_schema_test.rb
index ab61a4dcef..ce9be66069 100644
--- a/activerecord/test/cases/migration/change_schema_test.rb
+++ b/activerecord/test/cases/migration/change_schema_test.rb
@@ -291,14 +291,20 @@ module ActiveRecord
def test_column_exists_with_definition
connection.create_table :testings do |t|
- t.column :foo, :string, :limit => 100
- t.column :bar, :decimal, :precision => 8, :scale => 2
+ t.column :foo, :string, limit: 100
+ t.column :bar, :decimal, precision: 8, scale: 2
+ t.column :taggable_id, :integer, null: false
+ t.column :taggable_type, :string, default: 'Photo'
end
- assert connection.column_exists?(:testings, :foo, :string, :limit => 100)
- refute connection.column_exists?(:testings, :foo, :string, :limit => 50)
- assert connection.column_exists?(:testings, :bar, :decimal, :precision => 8, :scale => 2)
- refute connection.column_exists?(:testings, :bar, :decimal, :precision => 10, :scale => 2)
+ assert connection.column_exists?(:testings, :foo, :string, limit: 100)
+ refute connection.column_exists?(:testings, :foo, :string, limit: nil)
+ assert connection.column_exists?(:testings, :bar, :decimal, precision: 8, scale: 2)
+ refute connection.column_exists?(:testings, :bar, :decimal, precision: nil, scale: nil)
+ assert connection.column_exists?(:testings, :taggable_id, :integer, null: false)
+ refute connection.column_exists?(:testings, :taggable_id, :integer, null: true)
+ assert connection.column_exists?(:testings, :taggable_type, :string, default: 'Photo')
+ refute connection.column_exists?(:testings, :taggable_type, :string, default: nil)
end
def test_column_exists_on_table_with_no_options_parameter_supplied
diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb
index 5f36b2c841..8c96a8aaa1 100644
--- a/activerecord/test/cases/tasks/database_tasks_test.rb
+++ b/activerecord/test/cases/tasks/database_tasks_test.rb
@@ -251,6 +251,17 @@ module ActiveRecord
end
end
+ class DatabaseTasksCollationTest < ActiveRecord::TestCase
+ include DatabaseTasksSetupper
+
+ ADAPTERS_TASKS.each do |k, v|
+ define_method("test_#{k}_collation") do
+ eval("@#{v}").expects(:collation)
+ ActiveRecord::Tasks::DatabaseTasks.collation 'adapter' => k
+ end
+ end
+ end
+
class DatabaseTasksStructureDumpTest < ActiveRecord::TestCase
include DatabaseTasksSetupper
diff --git a/activerecord/test/cases/tasks/mysql_rake_test.rb b/activerecord/test/cases/tasks/mysql_rake_test.rb
index 42a11b0171..9a0eb423bd 100644
--- a/activerecord/test/cases/tasks/mysql_rake_test.rb
+++ b/activerecord/test/cases/tasks/mysql_rake_test.rb
@@ -195,6 +195,24 @@ module ActiveRecord
end
end
+ class MysqlDBCollationTest < ActiveRecord::TestCase
+ def setup
+ @connection = stub(:create_database => true)
+ @configuration = {
+ 'adapter' => 'mysql',
+ 'database' => 'my-app-db'
+ }
+
+ ActiveRecord::Base.stubs(:connection).returns(@connection)
+ ActiveRecord::Base.stubs(:establish_connection).returns(true)
+ end
+
+ def test_db_retrieves_collation
+ @connection.expects(:collation)
+ ActiveRecord::Tasks::DatabaseTasks.collation @configuration
+ end
+ end
+
class MySQLStructureDumpTest < ActiveRecord::TestCase
def setup
@connection = stub(:structure_dump => true)
diff --git a/activerecord/test/cases/tasks/postgresql_rake_test.rb b/activerecord/test/cases/tasks/postgresql_rake_test.rb
index 75ae7db4c9..df1075d816 100644
--- a/activerecord/test/cases/tasks/postgresql_rake_test.rb
+++ b/activerecord/test/cases/tasks/postgresql_rake_test.rb
@@ -159,6 +159,24 @@ module ActiveRecord
end
end
+ class PostgreSQLDBCollationTest < ActiveRecord::TestCase
+ def setup
+ @connection = stub(:create_database => true)
+ @configuration = {
+ 'adapter' => 'postgresql',
+ 'database' => 'my-app-db'
+ }
+
+ ActiveRecord::Base.stubs(:connection).returns(@connection)
+ ActiveRecord::Base.stubs(:establish_connection).returns(true)
+ end
+
+ def test_db_retrieves_collation
+ @connection.expects(:collate)
+ ActiveRecord::Tasks::DatabaseTasks.collation @configuration
+ end
+ end
+
class PostgreSQLStructureDumpTest < ActiveRecord::TestCase
def setup
@connection = stub(:structure_dump => true)
diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb
index b5557fc953..06a1d0ffc2 100644
--- a/activerecord/test/cases/tasks/sqlite_rake_test.rb
+++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb
@@ -124,6 +124,27 @@ module ActiveRecord
end
end
+ class SqliteDBCollationTest < ActiveRecord::TestCase
+ def setup
+ @database = 'db_create.sqlite3'
+ @connection = stub :connection
+ @configuration = {
+ 'adapter' => 'sqlite3',
+ 'database' => @database
+ }
+
+ File.stubs(:exist?).returns(false)
+ ActiveRecord::Base.stubs(:connection).returns(@connection)
+ ActiveRecord::Base.stubs(:establish_connection).returns(true)
+ end
+
+ def test_db_retrieves_collation
+ assert_raise NoMethodError do
+ ActiveRecord::Tasks::DatabaseTasks.collation @configuration, '/rails/root'
+ end
+ end
+ end
+
class SqliteStructureDumpTest < ActiveRecord::TestCase
def setup
@database = "db_create.sqlite3"
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 87b956e3fc..d2e07d6762 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Time#change now works with time values with offsets other than UTC or the local time zone. *Andrew White*
+
* AS::Callbacks: deprecate usage of filter object with `#before` and `#after` methods as `around` callback. *Bogdan Gusiev*
* Add `Time#prev_quarter' and 'Time#next_quarter' short-hands for months_ago(3) and months_since(3). *SungHee Kang*
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
index 13d659f52a..262c03540b 100644
--- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -4,10 +4,6 @@ require 'active_support/core_ext/date_time/calculations'
require 'active_support/values/time_zone'
class DateTime
- # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
- # DateTimes outside the range of what can be created with Time.
- remove_method :to_time
-
# Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
#
# This method is aliased to <tt>to_s</tt>.
@@ -57,16 +53,6 @@ class DateTime
alias_method :default_inspect, :inspect
alias_method :inspect, :readable_inspect
- # Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class.
- # If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time.
- def to_time
- if offset == 0
- ::Time.utc_time(year, month, day, hour, min, sec, sec_fraction * 1000000)
- else
- self
- end
- end
-
# Returns DateTime with local offset for given year if format is local else offset is zero
#
# DateTime.civil_from_format :local, 2012
@@ -95,7 +81,6 @@ class DateTime
private
def seconds_since_unix_epoch
- seconds_per_day = 86_400
- (self - ::DateTime.civil(1970)) * seconds_per_day
+ strftime('%s')
end
end
diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb
index 5a61906222..c82da3c6c2 100644
--- a/activesupport/lib/active_support/core_ext/hash/except.rb
+++ b/activesupport/lib/active_support/core_ext/hash/except.rb
@@ -4,13 +4,6 @@ class Hash
#
# @person.update_attributes(params[:person].except(:admin))
#
- # If the receiver responds to +convert_key+, the method is called on each of the
- # arguments. This allows +except+ to play nice with hashes with indifferent access
- # for instance:
- #
- # {:a => 1}.with_indifferent_access.except(:a) # => {}
- # {:a => 1}.with_indifferent_access.except('a') # => {}
- #
def except(*keys)
dup.except!(*keys)
end
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index e8623a9c54..0a71fc117c 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -86,16 +86,21 @@ class Time
# (hour, min, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and
# minute is passed, then sec and usec is set to 0.
def change(options)
- ::Time.send(
- utc? ? :utc_time : :local_time,
- options.fetch(:year, year),
- options.fetch(:month, month),
- options.fetch(:day, day),
- options.fetch(:hour, hour),
- options.fetch(:min, options[:hour] ? 0 : min),
- options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec),
- options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
- )
+ new_year = options.fetch(:year, year)
+ new_month = options.fetch(:month, month)
+ new_day = options.fetch(:day, day)
+ new_hour = options.fetch(:hour, hour)
+ new_min = options.fetch(:min, options[:hour] ? 0 : min)
+ new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec)
+ new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000))
+
+ if utc?
+ ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
+ elsif zone
+ ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec)
+ else
+ ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset)
+ end
end
# Uses Date to provide precise Time calculations for years, months, and days.
diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb
index 48c39d9370..9decbaef32 100644
--- a/activesupport/lib/active_support/file_update_checker.rb
+++ b/activesupport/lib/active_support/file_update_checker.rb
@@ -101,7 +101,9 @@ module ActiveSupport
end
def updated_at(paths)
- @updated_at || paths.map { |path| File.mtime(path) }.max || Time.at(0)
+ time_now = Time.now
+ @updated_at || paths.map { |path| File.mtime(path) }.
+ reject { |time| time > time_now }.max || Time.at(0)
end
def compile_glob(hash)
diff --git a/activesupport/lib/active_support/inflections.rb b/activesupport/lib/active_support/inflections.rb
index c04c2ed15b..ca2d8cb270 100644
--- a/activesupport/lib/active_support/inflections.rb
+++ b/activesupport/lib/active_support/inflections.rb
@@ -1,3 +1,5 @@
+require 'active_support/inflector/inflections'
+
module ActiveSupport
Inflector.inflections do |inflect|
inflect.plural(/$/, 's')
diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb
index 600e353812..c9e50a9462 100644
--- a/activesupport/lib/active_support/inflector/inflections.rb
+++ b/activesupport/lib/active_support/inflector/inflections.rb
@@ -2,6 +2,8 @@ require 'active_support/core_ext/array/prepend_and_append'
module ActiveSupport
module Inflector
+ extend self
+
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
# inflection rules.
#
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index 2acc6ddee5..c14a43de0d 100644
--- a/activesupport/lib/active_support/inflector/methods.rb
+++ b/activesupport/lib/active_support/inflector/methods.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'active_support/inflector/inflections'
+require 'active_support/inflections'
module ActiveSupport
# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb
index 28bc06f103..cc3e6a4bf3 100644
--- a/activesupport/lib/active_support/values/time_zone.rb
+++ b/activesupport/lib/active_support/values/time_zone.rb
@@ -254,8 +254,7 @@ module ActiveSupport
# Time.utc(2000).to_f # => 946684800.0
# Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00
def at(secs)
- utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs)
- utc.in_time_zone(self)
+ Time.at(secs).utc.in_time_zone(self)
end
# Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string.
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index d6f285598e..412aef9301 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -459,6 +459,15 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal Time.utc(2005,2,22,15,45), Time.utc(2005,2,22,15,15,10).change(:min => 45)
end
+ def test_offset_change
+ assert_equal Time.new(2006,2,22,15,15,10,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:year => 2006)
+ assert_equal Time.new(2005,6,22,15,15,10,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:month => 6)
+ assert_equal Time.new(2012,9,22,15,15,10,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:year => 2012, :month => 9)
+ assert_equal Time.new(2005,2,22,16,0,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:hour => 16)
+ assert_equal Time.new(2005,2,22,16,45,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:hour => 16, :min => 45)
+ assert_equal Time.new(2005,2,22,15,45,0,"-08:00"), Time.new(2005,2,22,15,15,10,"-08:00").change(:min => 45)
+ end
+
def test_advance
assert_equal Time.local(2006,2,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:years => 1)
assert_equal Time.local(2005,6,28,15,15,10), Time.local(2005,2,28,15,15,10).advance(:months => 4)
@@ -503,6 +512,28 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal Time.utc(2013,10,17,20,22,19), Time.utc(2005,2,28,15,15,10).advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9)
end
+ def test_offset_advance
+ assert_equal Time.new(2006,2,22,15,15,10,'-08:00'), Time.new(2005,2,22,15,15,10,'-08:00').advance(:years => 1)
+ assert_equal Time.new(2005,6,22,15,15,10,'-08:00'), Time.new(2005,2,22,15,15,10,'-08:00').advance(:months => 4)
+ assert_equal Time.new(2005,3,21,15,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:weeks => 3)
+ assert_equal Time.new(2005,3,25,3,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:weeks => 3.5)
+ assert_in_delta Time.new(2005,3,26,12,51,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:weeks => 3.7), 1
+ assert_equal Time.new(2005,3,5,15,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:days => 5)
+ assert_equal Time.new(2005,3,6,3,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:days => 5.5)
+ assert_in_delta Time.new(2005,3,6,8,3,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:days => 5.7), 1
+ assert_equal Time.new(2012,9,22,15,15,10,'-08:00'), Time.new(2005,2,22,15,15,10,'-08:00').advance(:years => 7, :months => 7)
+ assert_equal Time.new(2013,10,3,15,15,10,'-08:00'), Time.new(2005,2,22,15,15,10,'-08:00').advance(:years => 7, :months => 19, :days => 11)
+ assert_equal Time.new(2013,10,17,15,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:years => 7, :months => 19, :weeks => 2, :days => 5)
+ assert_equal Time.new(2001,12,27,15,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:years => -3, :months => -2, :days => -1)
+ assert_equal Time.new(2005,2,28,15,15,10,'-08:00'), Time.new(2004,2,29,15,15,10,'-08:00').advance(:years => 1) #leap day plus one year
+ assert_equal Time.new(2005,2,28,20,15,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:hours => 5)
+ assert_equal Time.new(2005,2,28,15,22,10,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:minutes => 7)
+ assert_equal Time.new(2005,2,28,15,15,19,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:seconds => 9)
+ assert_equal Time.new(2005,2,28,20,22,19,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:hours => 5, :minutes => 7, :seconds => 9)
+ assert_equal Time.new(2005,2,28,10,8,1,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:hours => -5, :minutes => -7, :seconds => -9)
+ assert_equal Time.new(2013,10,17,20,22,19,'-08:00'), Time.new(2005,2,28,15,15,10,'-08:00').advance(:years => 7, :months => 19, :weeks => 2, :days => 5, :hours => 5, :minutes => 7, :seconds => 9)
+ end
+
def test_advance_with_nsec
t = Time.at(0, Rational(108635108, 1000))
assert_equal t, t.advance(:months => 0)
diff --git a/activesupport/test/file_update_checker_test.rb b/activesupport/test/file_update_checker_test.rb
index 8adff5de8d..bd1df0f858 100644
--- a/activesupport/test/file_update_checker_test.rb
+++ b/activesupport/test/file_update_checker_test.rb
@@ -48,6 +48,21 @@ class FileUpdateCheckerWithEnumerableTest < ActiveSupport::TestCase
assert_equal 1, i
end
+ def test_should_be_robust_to_handle_files_with_wrong_modified_time
+ i = 0
+ now = Time.now
+ time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future
+ File.utime time, time, FILES[2]
+
+ checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 }
+
+ sleep(1)
+ FileUtils.touch(FILES[0..1])
+
+ assert checker.execute_if_updated
+ assert_equal 1, i
+ end
+
def test_should_cache_updated_result_until_execute
i = 0
checker = ActiveSupport::FileUpdateChecker.new(FILES){ i += 1 }
diff --git a/guides/source/debugging_rails_applications.textile b/guides/source/debugging_rails_applications.textile
index 0802a2db26..cc172042e9 100644
--- a/guides/source/debugging_rails_applications.textile
+++ b/guides/source/debugging_rails_applications.textile
@@ -102,7 +102,7 @@ It can also be useful to save information to log files at runtime. Rails maintai
h4. What is the Logger?
-Rails makes use of Ruby's standard +logger+ to write log information. You can also substitute another logger such as +Log4r+ if you wish.
+Rails makes use of the +ActiveSupport::BufferedLogger+ class to write log information. You can also substitute another logger such as +Log4r+ if you wish.
You can specify an alternative logger in your +environment.rb+ or any environment file:
diff --git a/guides/source/getting_started.textile b/guides/source/getting_started.textile
index 897327a888..07419d11b4 100644
--- a/guides/source/getting_started.textile
+++ b/guides/source/getting_started.textile
@@ -900,7 +900,7 @@ end
</ruby>
The new method, +update_attributes+, is used when you want to update a record
-that already exists, and it accepts an hash containing the attributes
+that already exists, and it accepts a hash containing the attributes
that you want to update. As before, if there was an error updating the
post we want to show the form back to the user.
@@ -1185,7 +1185,7 @@ delete "posts/:id" => "posts#destroy"
That's a lot to type for covering a single *resource*. Fortunately,
Rails provides a +resources+ method which can be used to declare a
-standard REST resource. Here's how +config/routes/rb+ looks after the
+standard REST resource. Here's how +config/routes.rb+ looks after the
cleanup:
<ruby>
diff --git a/guides/source/performance_testing.textile b/guides/source/performance_testing.textile
index 958b13cd9e..982fd1b070 100644
--- a/guides/source/performance_testing.textile
+++ b/guides/source/performance_testing.textile
@@ -524,11 +524,11 @@ Please refer to the "API docs":http://api.rubyonrails.org/classes/ActiveRecord/B
h4. Controller
-Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715
+Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html
<ruby>
def process_projects
- self.class.benchmark("Processing projects") do
+ benchmark("Processing projects") do
Project.process(params[:project_ids])
Project.update_cached_projects
end
@@ -539,7 +539,7 @@ NOTE: +benchmark+ is a class method inside controllers
h4. View
-And in "views":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715:
+And in "views":http://api.rubyonrails.org/classes/ActiveSupport/Benchmarkable.html:
<erb>
<% benchmark("Showing projects partial") do %>
diff --git a/guides/source/routing.textile b/guides/source/routing.textile
index dae25853cd..cffbf9bec4 100644
--- a/guides/source/routing.textile
+++ b/guides/source/routing.textile
@@ -32,7 +32,13 @@ the request is dispatched to the +patients+ controller's +show+ action with <tt>
h4. Generating Paths and URLs from Code
-You can also generate paths and URLs. If your application contains this code:
+You can also generate paths and URLs. If the route above is modified to be
+
+<ruby>
+get "/patients/:id" => "patients#show", :as => "patient"
+</ruby>
+
+If your application contains this code:
<ruby>
@patient = Patient.find(17)
@@ -845,24 +851,6 @@ end
This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+.
-h3. Breaking Up a Large Route File
-
-If you have a large route file that you would like to break up into multiple files, you can use the +#draw+ method in your router:
-
-<ruby>
-draw :admin
-</ruby>
-
-Then, create a file called +config/routes/admin.rb+. Name the file the same as the symbol passed to the +draw+ method. You can then use the normal routing DSL inside that file:
-
-<ruby>
-# in config/routes/admin.rb
-
-namespace :admin do
- resources :posts
-end
-</ruby>
-
h3. Inspecting and Testing Routes
Rails offers facilities for inspecting and testing your routes.
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 1a993a25cb..d5ec2cbfd9 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -79,9 +79,11 @@ module Rails
@routes_reloader = nil
@env_config = nil
@ordered_railties = nil
+ @railties = nil
@queue = nil
end
+ # Returns true if the application is initialized.
def initialized?
@initialized
end
@@ -98,30 +100,6 @@ module Rails
routes_reloader.reload!
end
- # Load the application and its railties tasks and invoke the registered hooks.
- # Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
- def load_tasks(app=self)
- initialize_tasks
- super
- self
- end
-
- # Load the application console and invoke the registered hooks.
- # Check <tt>Rails::Railtie.console</tt> for more info.
- def load_console(app=self)
- initialize_console
- super
- self
- end
-
- # Load the application runner and invoke the registered hooks.
- # Check <tt>Rails::Railtie.runner</tt> for more info.
- def load_runner(app=self)
- initialize_runner
- super
- self
- end
-
# Stores some of the Rails initial environment parameters which
# will be used by middlewares and engines to configure themselves.
def env_config
@@ -188,32 +166,9 @@ module Rails
self
end
- # Returns the ordered railties for this application considering railties_order.
- def ordered_railties #:nodoc:
- @ordered_railties ||= begin
- order = config.railties_order.map do |railtie|
- if railtie == :main_app
- self
- elsif railtie.respond_to?(:instance)
- railtie.instance
- else
- railtie
- end
- end
-
- all = (railties - order)
- all.push(self) unless (all + order).include?(self)
- order.push(:all) unless order.include?(:all)
-
- index = order.index(:all)
- order[index] = all
- order.reverse.flatten
- end
- end
-
def initializers #:nodoc:
Bootstrap.initializers_for(self) +
- super +
+ railties_initializers(super) +
Finisher.initializers_for(self)
end
@@ -246,6 +201,66 @@ module Rails
alias :build_middleware_stack :app
+ def run_tasks_blocks(app) #:nodoc:
+ railties.each { |r| r.run_tasks_blocks(app) }
+ super
+ require "rails/tasks"
+ task :environment do
+ $rails_rake_task = true
+ require_environment!
+ end
+ end
+
+ def run_generators_blocks(app) #:nodoc:
+ railties.each { |r| r.run_generators_blocks(app) }
+ super
+ end
+
+ def run_runner_blocks(app) #:nodoc:
+ railties.each { |r| r.run_runner_blocks(app) }
+ super
+ end
+
+ def run_console_blocks(app) #:nodoc:
+ railties.each { |r| r.run_console_blocks(app) }
+ super
+ end
+
+ # Returns the ordered railties for this application considering railties_order.
+ def ordered_railties #:nodoc:
+ @ordered_railties ||= begin
+ order = config.railties_order.map do |railtie|
+ if railtie == :main_app
+ self
+ elsif railtie.respond_to?(:instance)
+ railtie.instance
+ else
+ railtie
+ end
+ end
+
+ all = (railties - order)
+ all.push(self) unless (all + order).include?(self)
+ order.push(:all) unless order.include?(:all)
+
+ index = order.index(:all)
+ order[index] = all
+ order.reverse.flatten
+ end
+ end
+
+ def railties_initializers(current) #:nodoc:
+ initializers = []
+ ordered_railties.each do |r|
+ if r == self
+ initializers += current
+ else
+ initializers += r.initializers
+ end
+ end
+ initializers
+ end
+
def reload_dependencies? #:nodoc:
config.reload_classes_only_on_change != true || reloaders.map(&:updated?).any?
end
@@ -305,25 +320,6 @@ module Rails
end
end
- def initialize_tasks #:nodoc:
- self.class.rake_tasks do
- require "rails/tasks"
- task :environment do
- $rails_rake_task = true
- require_environment!
- end
- end
- end
-
- def initialize_console #:nodoc:
- require "pp"
- require "rails/console/app"
- require "rails/console/helpers"
- end
-
- def initialize_runner #:nodoc:
- end
-
def build_original_fullpath(env) #:nodoc:
path_info = env["PATH_INFO"]
query_string = env["QUERY_STRING"]
diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb
index 19f616ec50..6f9a200aa9 100644
--- a/railties/lib/rails/application/routes_reloader.rb
+++ b/railties/lib/rails/application/routes_reloader.rb
@@ -3,13 +3,12 @@ require "active_support/core_ext/module/delegation"
module Rails
class Application
class RoutesReloader
- attr_reader :route_sets, :paths, :external_routes
+ attr_reader :route_sets, :paths
delegate :execute_if_updated, :execute, :updated?, :to => :updater
def initialize
- @paths = []
- @route_sets = []
- @external_routes = []
+ @paths = []
+ @route_sets = []
end
def reload!
@@ -24,11 +23,7 @@ module Rails
def updater
@updater ||= begin
- dirs = @external_routes.each_with_object({}) do |dir, hash|
- hash[dir.to_s] = %w(rb)
- end
-
- updater = ActiveSupport::FileUpdateChecker.new(paths, dirs) { reload! }
+ updater = ActiveSupport::FileUpdateChecker.new(paths) { reload! }
updater.execute
updater
end
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb
index 7db602279a..383c159d3d 100644
--- a/railties/lib/rails/engine.rb
+++ b/railties/lib/rails/engine.rb
@@ -338,26 +338,6 @@ module Rails
class Engine < Railtie
autoload :Configuration, "rails/engine/configuration"
- def initialize
- @_all_autoload_paths = nil
- @_all_load_paths = nil
- @app = nil
- @config = nil
- @env_config = nil
- @helpers = nil
- @railties = nil
- @routes = nil
- super
- end
-
- def load_generators(app=self)
- initialize_generators
- railties.each { |r| r.load_generators(app) }
- Rails::Generators.configure!(app.config.generators)
- super
- self
- end
-
class << self
attr_accessor :called_from, :isolated
alias :isolated? :isolated
@@ -406,7 +386,7 @@ module Rails
end
unless mod.respond_to?(:railtie_routes_url_helpers)
- define_method(:railtie_routes_url_helpers) { railtie.routes_url_helpers }
+ define_method(:railtie_routes_url_helpers) { railtie.routes.url_helpers }
end
end
end
@@ -426,25 +406,54 @@ module Rails
delegate :middleware, :root, :paths, :to => :config
delegate :engine_name, :isolated?, :to => "self.class"
- def load_tasks(app=self)
- railties.each { |r| r.load_tasks(app) }
+ def initialize
+ @_all_autoload_paths = nil
+ @_all_load_paths = nil
+ @app = nil
+ @config = nil
+ @env_config = nil
+ @helpers = nil
+ @routes = nil
super
- paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
end
+ # Load console and invoke the registered hooks.
+ # Check <tt>Rails::Railtie.console</tt> for more info.
def load_console(app=self)
- railties.each { |r| r.load_console(app) }
- super
+ require "pp"
+ require "rails/console/app"
+ require "rails/console/helpers"
+ run_console_blocks(app)
+ self
end
+ # Load Rails runner and invoke the registered hooks.
+ # Check <tt>Rails::Railtie.runner</tt> for more info.
def load_runner(app=self)
- railties.each { |r| r.load_runner(app) }
- super
+ run_runner_blocks(app)
+ self
end
- def eager_load!
- railties.each(&:eager_load!)
+ # Load Rake, railties tasks and invoke the registered hooks.
+ # Check <tt>Rails::Railtie.rake_tasks</tt> for more info.
+ def load_tasks(app=self)
+ require "rake"
+ run_tasks_blocks(app)
+ self
+ end
+ # Load rails generators and invoke the registered hooks.
+ # Check <tt>Rails::Railtie.generators</tt> for more info.
+ def load_generators(app=self)
+ require "rails/generators"
+ run_generators_blocks(app)
+ Rails::Generators.configure!(app.config.generators)
+ self
+ end
+
+ # Eager load the application by loading all ruby
+ # files inside eager_load paths.
+ def eager_load!
config.eager_load_paths.each do |load_path|
matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
@@ -453,10 +462,7 @@ module Rails
end
end
- def railties
- @railties ||= []
- end
-
+ # Returns a module with all the helpers defined for the engine.
def helpers
@helpers ||= begin
helpers = Module.new
@@ -468,14 +474,12 @@ module Rails
end
end
+ # Returns all registered helpers paths.
def helpers_paths
paths["app/helpers"].existent
end
- def routes_url_helpers
- routes.url_helpers
- end
-
+ # Returns the underlying rack application for this engine.
def app
@app ||= begin
config.middleware = config.middleware.merge_into(default_middleware_stack)
@@ -483,45 +487,33 @@ module Rails
end
end
+ # Returns the endpoint for this engine. If none is registered,
+ # defaults to an ActionDispatch::Routing::RouteSet.
def endpoint
self.class.endpoint || routes
end
+ # Define the Rack API for this engine.
def call(env)
app.call(env.merge!(env_config))
end
+ # Defines additional Rack env configuration that is added on each call.
def env_config
@env_config ||= {
'action_dispatch.routes' => routes
}
end
+ # Defines the routes for this engine. If a block is given to
+ # routes, it is appended to the engine.
def routes
- @routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
- routes.draw_paths.concat paths["config/routes"].paths
- end
-
+ @routes ||= ActionDispatch::Routing::RouteSet.new
@routes.append(&Proc.new) if block_given?
@routes
end
- def ordered_railties
- railties + [self]
- end
-
- def initializers
- initializers = []
- ordered_railties.each do |r|
- if r == self
- initializers += super
- else
- initializers += r.initializers
- end
- end
- initializers
- end
-
+ # Define the configuration object for the engine.
def config
@config ||= Engine::Configuration.new(find_root_with_flag("lib"))
end
@@ -560,12 +552,10 @@ module Rails
initializer :add_routing_paths do |app|
paths = self.paths["config/routes.rb"].existent
- external_paths = self.paths["config/routes"].paths
if routes? || paths.any?
app.routes_reloader.paths.unshift(*paths)
app.routes_reloader.route_sets << routes
- app.routes_reloader.external_routes.unshift(*external_paths)
end
end
@@ -626,7 +616,6 @@ module Rails
else
Rake::Task["app:railties:install:migrations"].invoke
end
-
end
end
end
@@ -634,19 +623,20 @@ module Rails
protected
- def initialize_generators
- require "rails/generators"
+ def run_tasks_blocks(*) #:nodoc:
+ super
+ paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
end
- def routes?
+ def routes? #:nodoc:
@routes
end
- def has_migrations?
+ def has_migrations? #:nodoc:
paths["db/migrate"].existent.any?
end
- def find_root_with_flag(flag, default=nil)
+ def find_root_with_flag(flag, default=nil) #:nodoc:
root_path = self.class.called_from
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
@@ -660,19 +650,19 @@ module Rails
Pathname.new File.realpath root
end
- def default_middleware_stack
+ def default_middleware_stack #:nodoc:
ActionDispatch::MiddlewareStack.new
end
- def _all_autoload_once_paths
+ def _all_autoload_once_paths #:nodoc:
config.autoload_once_paths
end
- def _all_autoload_paths
+ def _all_autoload_paths #:nodoc:
@_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq
end
- def _all_load_paths
+ def _all_load_paths #:nodoc:
@_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq
end
end
diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb
index e31df807a6..6b18b1e249 100644
--- a/railties/lib/rails/engine/configuration.rb
+++ b/railties/lib/rails/engine/configuration.rb
@@ -53,7 +53,6 @@ module Rails
paths.add "config/initializers", :glob => "**/*.rb"
paths.add "config/locales", :glob => "*.{rb,yml}"
paths.add "config/routes.rb"
- paths.add "config/routes", :glob => "**/*.rb"
paths.add "db"
paths.add "db/migrate"
paths.add "db/seeds.rb"
diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb
index 316ecca87b..3c2210aaf9 100644
--- a/railties/lib/rails/paths.rb
+++ b/railties/lib/rails/paths.rb
@@ -115,7 +115,7 @@ module Rails
class Path
include Enumerable
- attr_reader :path, :root
+ attr_reader :path
attr_accessor :glob
def initialize(root, current, paths, options = {})
@@ -181,14 +181,6 @@ module Rails
@paths
end
- def paths
- raise "You need to set a path root" unless @root.path
-
- map do |p|
- File.join @root.path, p
- end
- end
-
# Expands all paths against the root and return all unique values.
def expanded
raise "You need to set a path root" unless @root.path
diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb
index a06be59759..1cb99463cc 100644
--- a/railties/lib/rails/railtie.rb
+++ b/railties/lib/rails/railtie.rb
@@ -181,33 +181,35 @@ module Rails
def eager_load!
end
- def load_console(app=self)
+ def railtie_namespace
+ @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
+ end
+
+ protected
+
+ def run_console_blocks(app) #:nodoc:
self.class.console.each { |block| block.call(app) }
end
- def load_runner(app=self)
+ def run_generators_blocks(app) #:nodoc:
+ self.class.generators.each { |block| block.call(app) }
+ end
+
+ def run_runner_blocks(app) #:nodoc:
self.class.runner.each { |block| block.call(app) }
end
- def load_tasks(app=self)
- require 'rake'
+ def run_tasks_blocks(app) #:nodoc:
extend Rake::DSL
- self.class.rake_tasks.each { |block| self.instance_exec(app, &block) }
+ self.class.rake_tasks.each { |block| instance_exec(app, &block) }
- # load also tasks from all superclasses
+ # Load also tasks from all superclasses
klass = self.class.superclass
+
while klass.respond_to?(:rake_tasks)
- klass.rake_tasks.each { |t| self.instance_exec(app, &t) }
+ klass.rake_tasks.each { |t| instance_exec(app, &t) }
klass = klass.superclass
end
end
-
- def load_generators(app=self)
- self.class.generators.each { |block| block.call(app) }
- end
-
- def railtie_namespace
- @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
- end
end
end
diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb
index e0893f53be..4029984ce9 100644
--- a/railties/test/application/paths_test.rb
+++ b/railties/test/application/paths_test.rb
@@ -50,8 +50,6 @@ module ApplicationTests
assert_path @paths["config/locales"], "config/locales/en.yml"
assert_path @paths["config/environment"], "config/environment.rb"
assert_path @paths["config/environments"], "config/environments/development.rb"
- assert_path @paths["config/routes.rb"], "config/routes.rb"
- assert_path @paths["config/routes"], "config/routes"
assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first
end
diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb
index d1373ba202..396b1849d8 100644
--- a/railties/test/application/routing_test.rb
+++ b/railties/test/application/routing_test.rb
@@ -178,90 +178,7 @@ module ApplicationTests
assert_equal 'WIN', last_response.body
end
- test "routes drawing from config/routes" do
- app_file 'config/routes.rb', <<-RUBY
- AppTemplate::Application.routes.draw do
- draw :external
- end
- RUBY
-
- app_file 'config/routes/external.rb', <<-RUBY
- get ':controller/:action'
- RUBY
-
- controller :success, <<-RUBY
- class SuccessController < ActionController::Base
- def index
- render :text => "success!"
- end
- end
- RUBY
-
- app 'development'
- get '/success/index'
- assert_equal 'success!', last_response.body
- end
-
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
- test "reloads routes when external configuration is changed in #{mode}" do
- controller :foo, <<-RUBY
- class FooController < ApplicationController
- def bar
- render :text => "bar"
- end
-
- def baz
- render :text => "baz"
- end
- end
- RUBY
-
- app_file 'config/routes.rb', <<-RUBY
- AppTemplate::Application.routes.draw do
- draw :external
- end
- RUBY
-
- app_file 'config/routes/external.rb', <<-RUBY
- get 'foo', :to => 'foo#bar'
- RUBY
-
- app(mode)
-
- get '/foo'
- assert_equal 'bar', last_response.body
-
- app_file 'config/routes/external.rb', <<-RUBY
- get 'foo', :to => 'foo#baz'
- RUBY
-
- sleep 0.1
-
- get '/foo'
- assert_equal expected, last_response.body
-
- app_file 'config/routes.rb', <<-RUBY
- AppTemplate::Application.routes.draw do
- draw :external
- draw :other_external
- end
- RUBY
-
- app_file 'config/routes/other_external.rb', <<-RUBY
- get 'win', :to => 'foo#baz'
- RUBY
-
- sleep 0.1
-
- get '/win'
-
- if mode == "development"
- assert_equal expected, last_response.body
- else
- assert_equal 404, last_response.status
- end
- end
-
test "reloads routes when configuration is changed in #{mode}" do
controller :foo, <<-RUBY
class FooController < ApplicationController
diff --git a/railties/test/engine_test.rb b/railties/test/engine_test.rb
index 68406dce4c..addf49cdb6 100644
--- a/railties/test/engine_test.rb
+++ b/railties/test/engine_test.rb
@@ -11,14 +11,4 @@ class EngineTest < ActiveSupport::TestCase
assert !engine.routes?
end
-
- it "does not add more paths to routes on each call" do
- engine = Class.new(Rails::Engine)
-
- engine.routes
- length = engine.routes.draw_paths.length
-
- engine.routes
- assert_equal length, engine.routes.draw_paths.length
- end
end
diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb
index 2b38f0975d..491c45da70 100644
--- a/railties/test/paths_test.rb
+++ b/railties/test/paths_test.rb
@@ -29,7 +29,6 @@ class PathsTest < ActiveSupport::TestCase
test "creating a root level path" do
@root.add "app"
assert_equal ["/foo/bar/app"], @root["app"].to_a
- assert_equal ["/foo/bar/app"], @root["app"].paths
end
test "creating a root level path with options" do
@@ -192,7 +191,6 @@ class PathsTest < ActiveSupport::TestCase
@root["app"] = "/app"
@root["app"].glob = "*.rb"
assert_equal "*.rb", @root["app"].glob
- assert_equal ["/foo/bar/app"], @root["app"].paths
end
test "it should be possible to override a path's default glob without assignment" do
diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb
index 52c7fae6c6..63814f7a04 100644
--- a/railties/test/railties/engine_test.rb
+++ b/railties/test/railties/engine_test.rb
@@ -1098,7 +1098,7 @@ YAML
assert_equal "// App's bar js\n;", last_response.body.strip
# ensure that railties are not added twice
- railties = Rails.application.ordered_railties.map(&:class)
+ railties = Rails.application.send(:ordered_railties).map(&:class)
assert_equal railties, railties.uniq
end