Skip to content
Browse files

Merge branch 'master' of github.com:lifo/docrails

  • Loading branch information...
2 parents f6fa6cf + dc364fd commit 4a1207d54077348b67fad95ffde5710cf0be31bd Neeraj Singh committed Jul 8, 2010
Showing with 855 additions and 545 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 actionmailer/lib/action_mailer/base.rb
  3. +1 −0 actionmailer/test/base_test.rb
  4. +7 −5 actionmailer/test/old_base/url_test.rb
  5. +1 −1 actionpack/actionpack.gemspec
  6. +2 −2 actionpack/lib/abstract_controller/rendering.rb
  7. +0 −6 actionpack/lib/action_controller/base.rb
  8. +5 −5 actionpack/lib/action_controller/metal/url_for.rb
  9. +0 −1 actionpack/lib/action_controller/railtie.rb
  10. +5 −0 actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
  11. +5 −7 actionpack/lib/action_dispatch/routing.rb
  12. +4 −4 actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
  13. +84 −84 actionpack/lib/action_dispatch/routing/mapper.rb
  14. +41 −35 actionpack/lib/action_dispatch/routing/route_set.rb
  15. +1 −1 actionpack/lib/action_dispatch/routing/url_for.rb
  16. +1 −1 actionpack/lib/action_view/base.rb
  17. +1 −1 actionpack/lib/action_view/helpers/url_helper.rb
  18. +5 −5 actionpack/lib/action_view/test_case.rb
  19. +14 −11 actionpack/test/abstract_unit.rb
  20. +20 −0 actionpack/test/controller/mime_responds_test.rb
  21. +1 −1 actionpack/test/controller/url_for_test.rb
  22. +268 −9 actionpack/test/dispatch/routing_test.rb
  23. +22 −0 actionpack/test/dispatch/session/cookie_store_test.rb
  24. +8 −8 actionpack/test/dispatch/url_generation_test.rb
  25. +27 −2 actionpack/test/template/url_helper_test.rb
  26. +1 −0 activemodel/lib/active_model/errors.rb
  27. +11 −4 activerecord/lib/active_record/associations.rb
  28. +6 −1 activerecord/lib/active_record/associations/association_collection.rb
  29. +6 −5 activerecord/lib/active_record/base.rb
  30. +1 −1 activerecord/lib/active_record/named_scope.rb
  31. +3 −3 activerecord/lib/active_record/observer.rb
  32. +1 −1 activerecord/lib/active_record/railties/databases.rake
  33. +6 −0 activerecord/test/cases/nested_attributes_test.rb
  34. +0 −1 activesupport/lib/active_support/all.rb
  35. +2 −2 activesupport/lib/active_support/callbacks.rb
  36. +26 −31 activesupport/lib/active_support/core_ext/class/subclasses.rb
  37. +2 −1 activesupport/lib/active_support/core_ext/date/calculations.rb
  38. +3 −13 activesupport/lib/active_support/core_ext/date/conversions.rb
  39. +14 −0 activesupport/lib/active_support/core_ext/date/zones.rb
  40. +1 −0 activesupport/lib/active_support/core_ext/date_time/calculations.rb
  41. +1 −0 activesupport/lib/active_support/core_ext/date_time/conversions.rb
  42. +2 −0 activesupport/lib/active_support/core_ext/date_time/zones.rb
  43. +1 −1 activesupport/lib/active_support/core_ext/object.rb
  44. +0 −11 activesupport/lib/active_support/core_ext/object/extending.rb
  45. +19 −0 activesupport/lib/active_support/core_ext/object/to_json.rb
  46. +5 −0 activesupport/lib/active_support/core_ext/time/calculations.rb
  47. +0 −5 activesupport/lib/active_support/core_ext/time/zones.rb
  48. +3 −2 activesupport/lib/active_support/deprecation/behaviors.rb
  49. +3 −2 activesupport/lib/active_support/deprecation/reporting.rb
  50. +14 −10 activesupport/lib/active_support/descendants_tracker.rb
  51. +5 −6 activesupport/lib/active_support/file_update_checker.rb
  52. +1 −0 activesupport/lib/active_support/i18n_railtie.rb
  53. +10 −32 activesupport/lib/active_support/json/encoding.rb
  54. +2 −4 activesupport/lib/active_support/json/variable.rb
  55. +1 −0 activesupport/lib/active_support/time.rb
  56. +6 −6 activesupport/lib/active_support/values/time_zone.rb
  57. +19 −21 activesupport/test/core_ext/class_test.rb
  58. +0 −49 activesupport/test/core_ext/object_and_class_ext_test.rb
  59. +20 −0 activesupport/test/deprecation_test.rb
  60. +5 −3 activesupport/test/descendants_tracker_test.rb
  61. +4 −2 activesupport/test/json/encoding_test.rb
  62. +1 −0 activesupport/test/ordered_hash_test.rb
  63. +1 −1 rails.gemspec
  64. +29 −52 railties/guides/source/active_support_core_extensions.textile
  65. +10 −0 railties/guides/source/association_basics.textile
  66. +29 −49 railties/guides/source/initialization.textile
  67. +6 −0 railties/guides/source/routing.textile
  68. +9 −14 railties/lib/rails/application.rb
  69. +5 −5 railties/lib/rails/commands.rb
  70. +1 −1 railties/lib/rails/commands/application.rb
  71. +1 −1 railties/lib/rails/commands/runner.rb
  72. +1 −1 railties/lib/rails/generators/rails/app/templates/Rakefile
  73. +1 −1 railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt
  74. +2 −2 railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt
  75. +1 −1 railties/lib/rails/info_routes.rb
  76. +1 −1 railties/lib/rails/tasks/middleware.rake
  77. +1 −1 railties/lib/rails/tasks/routes.rake
  78. +1 −1 railties/railties.gemspec
  79. +20 −1 railties/test/application/initializers/frameworks_test.rb
  80. +1 −1 railties/test/application/initializers/i18n_test.rb
  81. +2 −2 railties/test/application/rake_test.rb
  82. +1 −1 railties/test/application/runner_test.rb
  83. +1 −1 railties/test/rails_info_controller_test.rb
View
1 .gitignore
@@ -1,6 +1,7 @@
*.gem
pkg
.bundle
+Gemfile.lock
debug.log
doc/rdoc
activemodel/doc
View
4 actionmailer/lib/action_mailer/base.rb
@@ -737,13 +737,13 @@ def deprecated_url_options
raise "You can no longer call ActionMailer::Base.default_url_options " \
"directly. You need to set config.action_mailer.default_url_options. " \
"If you are using ActionMailer standalone, you need to include the " \
- "url_helpers of a router directly."
+ "routing url_helpers directly."
end
end
# This module will complain if the user tries to set default_url_options
# directly instead of through the config object. In Action Mailer's Railtie,
- # we include the url_helpers of the router, which will override this module
+ # we include the router's url_helpers, which will override this module.
extend DeprecatedUrlOptions
ActiveSupport.run_load_hooks(:action_mailer, self)
View
1 actionmailer/test/base_test.rb
@@ -1,5 +1,6 @@
# encoding: utf-8
require 'abstract_unit'
+require 'active_support/time'
class BaseTest < ActiveSupport::TestCase
# TODO Add some tests for implicity layout render and url helpers
View
12 actionmailer/test/old_base/url_test.rb
@@ -28,7 +28,7 @@ def signed_up_with_url(recipient)
end
end
-class ActionMailerUrlTest < Test::Unit::TestCase
+class ActionMailerUrlTest < ActionMailer::TestCase
def encode( text, charset="UTF-8" )
quoted_printable( text, charset )
@@ -57,10 +57,12 @@ def teardown
def test_signed_up_with_url
UrlTestMailer.delivery_method = :test
-
- AppRoutes.draw do |map|
- map.connect ':controller/:action/:id'
- map.welcome 'welcome', :controller=>"foo", :action=>"bar"
+
+ assert_deprecated do
+ AppRoutes.draw do |map|
+ map.connect ':controller/:action/:id'
+ map.welcome 'welcome', :controller=>"foo", :action=>"bar"
+ end
end
expected = new_mail
View
2 actionpack/actionpack.gemspec
@@ -27,5 +27,5 @@ Gem::Specification.new do |s|
s.add_dependency('rack-test', '~> 0.5.4')
#s.add_dependency('rack-mount', '~> 0.6.6')
s.add_dependency('tzinfo', '~> 0.3.16')
- s.add_dependency('erubis', '~> 2.6.5')
+ s.add_dependency('erubis', '~> 2.6.6')
end
View
4 actionpack/lib/abstract_controller/rendering.rb
@@ -50,8 +50,8 @@ def view_context_class
if controller.respond_to?(:_helpers)
include controller._helpers
- if controller.respond_to?(:_router)
- include controller._router.url_helpers
+ if controller.respond_to?(:_routes)
+ include controller._routes.url_helpers
end
# TODO: Fix RJS to not require this
View
6 actionpack/lib/action_controller/base.rb
@@ -60,17 +60,11 @@ def self.without_modules(*modules)
include ActionController::Compatibility
def self.inherited(klass)
- ::ActionController::Base.subclasses << klass.to_s
super
klass.helper :all
end
- def self.subclasses
- @subclasses ||= []
- end
-
config_accessor :asset_host, :asset_path
-
ActiveSupport.run_load_hooks(:action_controller, self)
end
end
View
10 actionpack/lib/action_controller/metal/url_for.rb
@@ -12,17 +12,17 @@ def url_options
).merge(:script_name => request.script_name)
end
- def _router
- raise "In order to use #url_for, you must include the helpers of a particular " \
- "router. For instance, `include Rails.application.routes.url_helpers"
+ def _routes
+ raise "In order to use #url_for, you must include routing helpers explicitly. " \
+ "For instance, `include Rails.application.routes.url_helpers"
end
module ClassMethods
def action_methods
@action_methods ||= begin
- super - _router.named_routes.helper_names
+ super - _routes.named_routes.helper_names
end
end
end
end
-end
+end
View
1 actionpack/lib/action_controller/railtie.rb
@@ -2,7 +2,6 @@
require "action_controller"
require "action_dispatch/railtie"
require "action_view/railtie"
-require "active_support/core_ext/class/subclasses"
require "active_support/deprecation/proxy_wrappers"
require "active_support/deprecation"
View
5 actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -64,6 +64,11 @@ def []=(key, value)
super(key.to_s, value)
end
+ def clear
+ load_for_write!
+ super
+ end
+
def to_hash
load_for_read!
h = {}.replace(self)
View
12 actionpack/lib/action_dispatch/routing.rb
@@ -31,7 +31,7 @@ module ActionDispatch
# Think of creating routes as drawing a map for your requests. The map tells
# them where to go based on some predefined pattern:
#
- # AppName::Applications.routes.draw do |map|
+ # AppName::Application.routes.draw do |map|
# Pattern 1 tells some request to go to one place
# Pattern 2 tell them to go to another
# ...
@@ -62,7 +62,7 @@ module ActionDispatch
#
# redirect_to show_item_path(:id => 25)
#
- # Use <tt>root</tt> as a shorthand to name a route for the root path "".
+ # Use <tt>root</tt> as a shorthand to name a route for the root path "/".
#
# # In routes.rb
# root :to => 'blogs#index'
@@ -72,7 +72,7 @@ module ActionDispatch
#
# # and provide these named routes
# root_url # => 'http://www.example.com/'
- # root_path # => ''
+ # root_path # => '/'
#
# Note: when using +controller+, the route is simply named after the
# method you call on the block parameter rather than map.
@@ -91,9 +91,7 @@ module ActionDispatch
#
# Routes can generate pretty URLs. For example:
#
- # match '/articles/:year/:month/:day', :constraints => {
- # :controller => 'articles',
- # :action => 'find_by_date',
+ # match '/articles/:year/:month/:day' => 'articles#find_by_id', :constraints => {
# :year => /\d{4}/,
# :month => /\d{1,2}/,
# :day => /\d{1,2}/
@@ -167,7 +165,7 @@ module ActionDispatch
#
# You can reload routes if you feel you must:
#
- # Rails::Application.reload_routes!
+ # Rails.application.reload_routes!
#
# This will clear all named routes and reload routes.rb if the file has been modified from
# last load. To absolutely force reloading, use <tt>reload!</tt>.
View
8 actionpack/lib/action_dispatch/routing/deprecated_mapper.rb
@@ -19,9 +19,9 @@ def controller_constraints
def in_memory_controller_namespaces
namespaces = Set.new
- ActionController::Base.subclasses.each do |klass|
- controller_name = klass.underscore
- namespaces << controller_name.split('/')[0...-1].join('/')
+ ActionController::Base.descendants.each do |klass|
+ next if klass.anonymous?
+ namespaces << klass.name.underscore.split('/')[0...-1].join('/')
end
namespaces.delete('')
namespaces
@@ -31,7 +31,7 @@ def in_memory_controller_namespaces
class DeprecatedMapper #:nodoc:
def initialize(set) #:nodoc:
ActiveSupport::Deprecation.warn "You are using the old router DSL which will be removed in Rails 3.1. " <<
- "Please check how to update your router file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/"
+ "Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/"
@set = set
end
View
168 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -13,6 +13,8 @@ def self.new(app, constraints, request = Rack::Request)
end
end
+ attr_reader :app
+
def initialize(app, constraints, request)
@app, @constraints, @request = app, constraints, request
end
@@ -63,6 +65,16 @@ def extract_path_and_options(args)
end
end
+ if path.match(':controller')
+ raise ArgumentError, ":controller segment is not allowed within a namespace block" if @scope[:module]
+
+ # Add a default constraint for :controller path segments that matches namespaced
+ # controllers with default routes like :controller/:action/:id(.:format), e.g:
+ # GET /admin/products/show/1
+ # => { :controller => 'admin/products', :action => 'show', :id => '1' }
+ options.reverse_merge!(:controller => /.+?/)
+ end
+
path = normalize_path(path)
path_without_format = path.sub(/\(\.:format\)$/, '')
@@ -133,8 +145,11 @@ def default_controller_and_action
defaults[:controller] ||= default_controller
defaults[:action] ||= default_action
- defaults.delete(:controller) if defaults[:controller].blank?
- defaults.delete(:action) if defaults[:action].blank?
+ defaults.delete(:controller) if defaults[:controller].blank? || defaults[:controller].is_a?(Regexp)
+ defaults.delete(:action) if defaults[:action].blank? || defaults[:action].is_a?(Regexp)
+
+ defaults[:controller] = defaults[:controller].to_s if defaults.key?(:controller)
+ defaults[:action] = defaults[:action].to_s if defaults.key?(:action)
if defaults[:controller].blank? && segment_keys.exclude?("controller")
raise ArgumentError, "missing :controller"
@@ -183,15 +198,15 @@ def to
def default_controller
if @options[:controller]
- @options[:controller].to_s
+ @options[:controller]
elsif @scope[:controller]
- @scope[:controller].to_s
+ @scope[:controller]
end
end
def default_action
if @options[:action]
- @options[:action].to_s
+ @options[:action]
end
end
end
@@ -431,20 +446,24 @@ def merge_blocks_scope(parent, child)
end
def merge_options_scope(parent, child)
- (parent || {}).merge(child)
+ (parent || {}).except(*override_keys(child)).merge(child)
end
def merge_shallow_scope(parent, child)
child ? true : false
end
+
+ def override_keys(child)
+ child.key?(:only) || child.key?(:except) ? [:only, :except] : []
+ end
end
module Resources
# CANONICAL_ACTIONS holds all actions that does not need a prefix or
# a path appended since they fit properly in their scope level.
VALID_ON_OPTIONS = [:new, :collection, :member]
CANONICAL_ACTIONS = [:index, :create, :new, :show, :update, :destroy]
- MERGE_FROM_SCOPE_OPTIONS = [:shallow, :constraints]
+ RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except]
class Resource #:nodoc:
DEFAULT_ACTIONS = [:index, :create, :new, :show, :update, :destroy, :edit]
@@ -464,9 +483,9 @@ def default_actions
end
def actions
- if only = options[:only]
+ if only = @options[:only]
Array(only).map(&:to_sym)
- elsif except = options[:except]
+ elsif except = @options[:except]
default_actions - Array(except).map(&:to_sym)
else
default_actions
@@ -489,68 +508,31 @@ def member_name
singular
end
- alias_method :nested_name, :member_name
-
# Checks for uncountable plurals, and appends "_index" if they're.
def collection_name
singular == plural ? "#{plural}_index" : plural
end
- def shallow?
- options[:shallow] ? true : false
- end
-
- def constraints
- options[:constraints] || {}
- end
-
- def id_constraint?
- options[:id] && options[:id].is_a?(Regexp) || constraints[:id] && constraints[:id].is_a?(Regexp)
- end
-
- def id_constraint
- options[:id] || constraints[:id]
- end
-
- def collection_options
- (options || {}).dup.tap do |opts|
- opts.delete(:id)
- opts[:constraints] = options[:constraints].dup if options[:constraints]
- opts[:constraints].delete(:id) if options[:constraints].is_a?(Hash)
- end
- end
-
- def nested_path
- "#{path}/:#{singular}_id"
- end
-
- def nested_options
- {}.tap do |opts|
- opts[:as] = member_name
- opts["#{singular}_id".to_sym] = id_constraint if id_constraint?
- opts[:options] = { :shallow => shallow? } unless options[:shallow].nil?
- end
- end
-
def resource_scope
- [{ :controller => controller }]
+ { :controller => controller }
end
def collection_scope
- [path, collection_options]
+ path
end
def member_scope
- ["#{path}/:id", options]
+ "#{path}/:id"
end
def new_scope(new_path)
- ["#{path}/#{new_path}"]
+ "#{path}/#{new_path}"
end
def nested_scope
- [nested_path, nested_options]
+ "#{path}/:#{singular}_id"
end
+
end
class SingletonResource < Resource #:nodoc:
@@ -559,7 +541,7 @@ class SingletonResource < Resource #:nodoc:
def initialize(entities, options)
@name = entities.to_s
@path = options.delete(:path) || @name
- @controller = (options.delete(:controller) || @name.to_s.pluralize).to_s
+ @controller = (options.delete(:controller) || plural).to_s
@as = options.delete(:as)
@options = options
end
@@ -569,24 +551,10 @@ def member_name
end
alias :collection_name :member_name
- def nested_path
- path
- end
-
- def nested_options
- {}.tap do |opts|
- opts[:as] = member_name
- opts[:options] = { :shallow => shallow? } unless @options[:shallow].nil?
- end
- end
-
- def shallow?
- false
- end
-
def member_scope
- [path, options]
+ path
end
+ alias :nested_scope :member_scope
end
def initialize(*args) #:nodoc:
@@ -693,18 +661,18 @@ def nested
end
with_scope_level(:nested) do
- if parent_resource.shallow?
+ if shallow?
with_exclusive_scope do
if @scope[:shallow_path].blank?
- scope(*parent_resource.nested_scope) { yield }
+ scope(parent_resource.nested_scope, nested_options) { yield }
else
scope(@scope[:shallow_path], :as => @scope[:shallow_prefix]) do
- scope(*parent_resource.nested_scope) { yield }
+ scope(parent_resource.nested_scope, nested_options) { yield }
end
end
end
else
- scope(*parent_resource.nested_scope) { yield }
+ scope(parent_resource.nested_scope, nested_options) { yield }
end
end
end
@@ -723,6 +691,10 @@ def shallow
end
end
+ def shallow?
+ parent_resource.instance_of?(Resource) && @scope[:shallow]
+ end
+
def match(*args)
options = args.extract_options!.dup
options[:anchor] = true unless options.key?(:anchor)
@@ -800,16 +772,17 @@ def apply_common_behavior_for(method, resources, options, &block)
return true
end
- if path_names = options.delete(:path_names)
- scope(:path_names => path_names) do
+ scope_options = options.slice!(*RESOURCE_OPTIONS)
+ unless scope_options.empty?
+ scope(scope_options) do
send(method, resources.pop, options, &block)
end
return true
end
- scope_options = @scope.slice(*MERGE_FROM_SCOPE_OPTIONS).delete_if{ |k,v| v.blank? }
- options.reverse_merge!(scope_options) unless scope_options.empty?
- options.reverse_merge!(@scope[:options]) unless @scope[:options].blank?
+ unless action_options?(options)
+ options.merge!(scope_action_options) if scope_action_options?
+ end
if resource_scope?
nested do
@@ -821,6 +794,18 @@ def apply_common_behavior_for(method, resources, options, &block)
false
end
+ def action_options?(options)
+ options[:only] || options[:except]
+ end
+
+ def scope_action_options?
+ @scope[:options].is_a?(Hash) && (@scope[:options][:only] || @scope[:options][:except])
+ end
+
+ def scope_action_options
+ @scope[:options].slice(:only, :except)
+ end
+
def resource_scope?
[:resource, :resources].include?(@scope[:scope_level])
end
@@ -853,42 +838,57 @@ def with_scope_level(kind, resource = parent_resource)
def resource_scope(resource)
with_scope_level(resource.is_a?(SingletonResource) ? :resource : :resources, resource) do
- scope(*parent_resource.resource_scope) do
+ scope(parent_resource.resource_scope) do
yield
end
end
end
def new_scope
with_scope_level(:new) do
- scope(*parent_resource.new_scope(action_path(:new))) do
+ scope(parent_resource.new_scope(action_path(:new))) do
yield
end
end
end
def collection_scope
with_scope_level(:collection) do
- scope(*parent_resource.collection_scope) do
+ scope(parent_resource.collection_scope) do
yield
end
end
end
def member_scope
with_scope_level(:member) do
- scope(*parent_resource.member_scope) do
+ scope(parent_resource.member_scope) do
yield
end
end
end
+ def nested_options
+ {}.tap do |options|
+ options[:as] = parent_resource.member_name
+ options[:constraints] = { "#{parent_resource.singular}_id".to_sym => id_constraint } if id_constraint?
+ end
+ end
+
+ def id_constraint?
+ @scope[:id].is_a?(Regexp) || (@scope[:constraints] && @scope[:constraints][:id].is_a?(Regexp))
+ end
+
+ def id_constraint
+ @scope[:id] || @scope[:constraints][:id]
+ end
+
def canonical_action?(action, flag)
flag && CANONICAL_ACTIONS.include?(action)
end
def shallow_scoping?
- parent_resource && parent_resource.shallow? && @scope[:scope_level] == :member
+ shallow? && @scope[:scope_level] == :member
end
def path_for_action(action, path)
@@ -932,7 +932,7 @@ def name_for_action(action, as=nil)
collection_name = parent_resource.collection_name
member_name = parent_resource.member_name
name_prefix = "#{name_prefix}_" if name_prefix.present?
- end
+ end
case @scope[:scope_level]
when :collection
View
76 actionpack/lib/action_dispatch/routing/route_set.rb
@@ -26,37 +26,51 @@ def call(env)
return [404, {'X-Cascade' => 'pass'}, []]
end
- controller.action(params[:action]).call(env)
+ dispatch(controller, params[:action], env)
end
def prepare_params!(params)
merge_default_action!(params)
split_glob_param!(params) if @glob_param
end
- def controller(params, raise_error=true)
+ # If this is a default_controller (i.e. a controller specified by the user)
+ # we should raise an error in case it's not found, because it usually means
+ # an user error. However, if the controller was retrieved through a dynamic
+ # segment, as in :controller(/:action), we should simply return nil and
+ # delegate the control back to Rack cascade. Besides, if this is not a default
+ # controller, it means we should respect the @scope[:module] parameter.
+ def controller(params, default_controller=true)
if params && params.key?(:controller)
controller_param = params[:controller]
- unless controller = @controllers[controller_param]
- controller_name = "#{controller_param.camelize}Controller"
- controller = @controllers[controller_param] =
- ActiveSupport::Dependencies.ref(controller_name)
- end
-
- controller.get
+ controller_reference(controller_param)
end
rescue NameError => e
- raise ActionController::RoutingError, e.message, e.backtrace if raise_error
+ raise ActionController::RoutingError, e.message, e.backtrace if default_controller
end
- private
- def merge_default_action!(params)
- params[:action] ||= 'index'
- end
+ private
- def split_glob_param!(params)
- params[@glob_param] = params[@glob_param].split('/').map { |v| URI.unescape(v) }
+ def controller_reference(controller_param)
+ unless controller = @controllers[controller_param]
+ controller_name = "#{controller_param.camelize}Controller"
+ controller = @controllers[controller_param] =
+ ActiveSupport::Dependencies.ref(controller_name)
end
+ controller.get
+ end
+
+ def dispatch(controller, action, env)
+ controller.action(action).call(env)
+ end
+
+ def merge_default_action!(params)
+ params[:action] ||= 'index'
+ end
+
+ def split_glob_param!(params)
+ params[@glob_param] = params[@glob_param].split('/').map { |v| URI.unescape(v) }
+ end
end
# A NamedRouteCollection instance is a collection of named routes, and also
@@ -268,10 +282,10 @@ class << self
# Yes plz - JP
included do
routes.install_helpers(self)
- singleton_class.send(:define_method, :_router) { routes }
+ singleton_class.send(:define_method, :_routes) { routes }
end
- define_method(:_router) { routes }
+ define_method(:_routes) { routes }
end
helpers
@@ -302,7 +316,6 @@ def initialize(options, recall, set, extras = false)
@extras = extras
normalize_options!
- normalize_recall!
normalize_controller_action_id!
use_relative_controller!
controller.sub!(%r{^/}, '') if controller
@@ -319,7 +332,11 @@ def current_controller
def use_recall_for(key)
if @recall[key] && (!@options.key?(key) || @options[key] == @recall[key])
- @options[key] = @recall.delete(key)
+ if named_route_exists?
+ @options[key] = @recall.delete(key) if segment_keys.include?(key)
+ else
+ @options[key] = @recall.delete(key)
+ end
end
end
@@ -343,15 +360,6 @@ def normalize_options!
end
end
- def normalize_recall!
- # If the target route is not a standard route then remove controller and action
- # from the options otherwise they will appear in the url parameters
- if block_or_proc_route_target?
- recall.delete(:controller) unless segment_keys.include?(:controller)
- recall.delete(:action) unless segment_keys.include?(:action)
- end
- end
-
# This pulls :controller, :action, and :id out of the recall.
# The recall key is only used if there is no key in the options
# or if the key in the options is identical. If any of
@@ -424,12 +432,8 @@ def named_route_exists?
named_route && set.named_routes[named_route]
end
- def block_or_proc_route_target?
- named_route_exists? && !set.named_routes[named_route].app.is_a?(Dispatcher)
- end
-
def segment_keys
- named_route_exists? ? set.named_routes[named_route].segment_keys : []
+ set.named_routes[named_route].segment_keys
end
end
@@ -474,7 +478,7 @@ def url_for(options)
path_options = yield(path_options) if block_given?
path = generate(path_options, path_segments || {})
- # ROUTES TODO: This can be called directly, so script_name should probably be set in the router
+ # ROUTES TODO: This can be called directly, so script_name should probably be set in the routes
rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor]
@@ -506,6 +510,8 @@ def recognize_path(path, environment = {})
end
dispatcher = route.app
+ dispatcher = dispatcher.app while dispatcher.is_a?(Mapper::Constraints)
+
if dispatcher.is_a?(Dispatcher) && dispatcher.controller(params, false)
dispatcher.prepare_params!(params)
return params
View
2 actionpack/lib/action_dispatch/routing/url_for.rb
@@ -128,7 +128,7 @@ def url_for(options = nil)
when String
options
when nil, Hash
- _router.url_for(url_options.merge((options || {}).symbolize_keys))
+ _routes.url_for(url_options.merge((options || {}).symbolize_keys))
else
polymorphic_url(options)
end
View
2 actionpack/lib/action_view/base.rb
@@ -173,7 +173,7 @@ module Subclasses
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
class_attribute :helpers
- class_attribute :_router
+ class_attribute :_routes
class << self
delegate :erb_trim_mode=, :to => 'ActionView::Template::Handlers::ERB'
View
2 actionpack/lib/action_view/helpers/url_helper.rb
@@ -12,7 +12,7 @@ module Helpers #:nodoc:
# and controllers.
module UrlHelper
# This helper may be included in any class that includes the
- # URL helpers of a router (router.url_helpers). Some methods
+ # URL helpers of a routes (routes.url_helpers). Some methods
# provided here will only work in the4 context of a request
# (link_to_unless_current, for instance), which must be provided
# as a method called #request on the context.
View
10 actionpack/lib/action_view/test_case.rb
@@ -151,7 +151,7 @@ def view
@view ||= begin
view = ActionView::Base.new(ActionController::Base.view_paths, {}, @controller)
view.singleton_class.send :include, _helpers
- view.singleton_class.send :include, @controller._router.url_helpers
+ view.singleton_class.send :include, @controller._routes.url_helpers
view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
view.extend(Locals)
view.locals = self.locals
@@ -192,13 +192,13 @@ def _assigns
end
end
- def _router
- @controller._router if @controller.respond_to?(:_router)
+ def _routes
+ @controller._routes if @controller.respond_to?(:_routes)
end
def method_missing(selector, *args)
- if @controller.respond_to?(:_router) &&
- @controller._router.named_routes.helpers.include?(selector)
+ if @controller.respond_to?(:_routes) &&
+ @controller._routes.named_routes.helpers.include?(selector)
@controller.__send__(selector, *args)
else
super
View
25 actionpack/test/abstract_unit.rb
@@ -41,7 +41,7 @@
module Rails
end
-# Monkey patch the old router initialization to be silenced.
+# Monkey patch the old routes initialization to be silenced.
class ActionDispatch::Routing::DeprecatedMapper
def initialize_with_silencer(*args)
ActiveSupport::Deprecation.silence { initialize_without_silencer(*args) }
@@ -182,13 +182,16 @@ def self.build_app(routes = nil)
self.app = build_app
- class StubDispatcher
- def self.new(*args)
- lambda { |env|
- params = env['action_dispatch.request.path_parameters']
- controller, action = params[:controller], params[:action]
- [200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]]
- }
+ # Stub Rails dispatcher so it does not get controller references and
+ # simply return the controller#action as Rack::Body.
+ class StubDispatcher < ::ActionDispatch::Routing::RouteSet::Dispatcher
+ protected
+ def controller_reference(controller_param)
+ controller_param
+ end
+
+ def dispatch(controller, action, env)
+ [200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]]
end
end
@@ -275,9 +278,9 @@ def assert_header(name, value)
class ActionController::Base
def self.test_routes(&block)
- router = ActionDispatch::Routing::RouteSet.new
- router.draw(&block)
- include router.url_helpers
+ routes = ActionDispatch::Routing::RouteSet.new
+ routes.draw(&block)
+ include routes.url_helpers
end
end
View
20 actionpack/test/controller/mime_responds_test.rb
@@ -36,6 +36,15 @@ def html_or_xml
type.all { render :text => "Nothing" }
end
end
+
+ def json_xml_or_html
+ respond_to do |type|
+ type.json { render :text => 'JSON' }
+ type.xml { render :xml => 'XML' }
+ type.html { render :text => 'HTML' }
+ end
+ end
+
def forced_xml
request.format = :xml
@@ -364,6 +373,17 @@ def test_handle_any_any_xml
get :handle_any_any
assert_equal 'Whatever you ask for, I got it', @response.body
end
+
+ def test_browser_check_with_any_any
+ @request.accept = "application/json, application/xml"
+ get :json_xml_or_html
+ assert_equal 'JSON', @response.body
+
+ @request.accept = "application/json, application/xml, */*"
+ get :json_xml_or_html
+ assert_equal 'HTML', @response.body
+ end
+
def test_rjs_type_skips_layout
@request.accept = "text/javascript"
View
2 actionpack/test/controller/url_for_test.rb
@@ -120,7 +120,7 @@ def test_trailing_slash_with_params
def test_relative_url_root_is_respected
# ROUTES TODO: Tests should not have to pass :relative_url_root directly. This
- # should probably come from the router.
+ # should probably come from routes.
add_host!
assert_equal('https://www.basecamphq.com/subdir/c/a/i',
View
277 actionpack/test/dispatch/routing_test.rb
@@ -45,7 +45,10 @@ def self.matches?(request)
match 'account/logout' => redirect("/logout"), :as => :logout_redirect
match 'account/login', :to => redirect("/login")
- match 'account/overview'
+ constraints(lambda { |req| true }) do
+ match 'account/overview'
+ end
+
match '/account/nested/overview'
match 'sign_in' => "sessions#new"
@@ -298,8 +301,8 @@ def self.matches?(request)
resource :dashboard, :constraints => { :ip => /192\.168\.1\.\d{1,3}/ }
+ resource :token, :module => :api
scope :module => :api do
- resource :token
resources :errors, :shallow => true do
resources :notices
end
@@ -310,11 +313,6 @@ def self.matches?(request)
match '/' => 'mes#index'
end
- namespace :private do
- root :to => redirect('/private/index')
- match "index", :to => 'private#index'
- end
-
get "(/:username)/followers" => "followers#index"
get "/groups(/user/:username)" => "groups#index"
get "(/user/:username)/photos" => "photos#index"
@@ -325,7 +323,7 @@ def self.matches?(request)
end
end
- match "whatever/:controller(/:action(/:id))"
+ match "whatever/:controller(/:action(/:id))", :id => /\d+/
resource :profile do
get :settings
@@ -348,6 +346,63 @@ def self.matches?(request)
end
end
+ namespace :private do
+ root :to => redirect('/private/index')
+ match "index", :to => 'private#index'
+ end
+
+ scope :only => [:index, :show] do
+ namespace :only do
+ resources :clubs do
+ resources :players
+ resource :chairman
+ end
+ end
+ end
+
+ scope :except => [:new, :create, :edit, :update, :destroy] do
+ namespace :except do
+ resources :clubs do
+ resources :players
+ resource :chairman
+ end
+ end
+ end
+
+ scope :only => :show do
+ namespace :only do
+ resources :sectors, :only => :index do
+ resources :companies do
+ scope :only => :index do
+ resources :divisions
+ end
+ scope :except => [:show, :update, :destroy] do
+ resources :departments
+ end
+ end
+ resource :leader
+ resources :managers, :except => [:show, :update, :destroy]
+ end
+ end
+ end
+
+ scope :except => :index do
+ namespace :except do
+ resources :sectors, :except => [:show, :update, :destroy] do
+ resources :companies do
+ scope :except => [:show, :update, :destroy] do
+ resources :divisions
+ end
+ scope :only => :index do
+ resources :departments
+ end
+ end
+ resource :leader
+ resources :managers, :only => :index
+ end
+ end
+ end
+
match '/:locale/*file.:format', :to => 'files#show', :file => /path\/to\/existing\/file/
end
end
@@ -470,6 +525,18 @@ def test_namespace_redirect
end
end
+ def test_namespace_with_controller_segment
+ assert_raise(ArgumentError) do
+ self.class.stub_controllers do |routes|
+ routes.draw do
+ namespace :admin do
+ match '/:controller(/:action(/:id(.:format)))'
+ end
+ end
+ end
+ end
+ end
+
def test_session_singleton_resource
with_test_routes do
get '/session'
@@ -1240,7 +1307,7 @@ def test_resource_constraints
assert_equal 'pass', @response.headers['X-Cascade']
get '/products/0001/images'
assert_equal 'images#index', @response.body
- get '/products/0001/images/1'
+ get '/products/0001/images/0001'
assert_equal 'images#show', @response.body
get '/dashboard', {}, {'REMOTE_ADDR' => '10.0.0.100'}
@@ -1285,6 +1352,22 @@ def test_url_generator_for_generic_route
end
end
+ def test_url_generator_for_namespaced_generic_route
+ with_test_routes do
+ get 'whatever/foo/bar/show'
+ assert_equal 'foo/bar#show', @response.body
+
+ get 'whatever/foo/bar/show/1'
+ assert_equal 'foo/bar#show', @response.body
+
+ assert_equal 'http://www.example.com/whatever/foo/bar/show',
+ url_for(:controller => "foo/bar", :action => "show")
+
+ assert_equal 'http://www.example.com/whatever/foo/bar/show/1',
+ url_for(:controller => "foo/bar", :action => "show", :id => '1')
+ end
+ end
+
def test_assert_recognizes_account_overview
with_test_routes do
assert_recognizes({:controller => "account", :action => "overview"}, "/account/overview")
@@ -1628,6 +1711,182 @@ def test_constraints_are_merged_from_scope
end
end
+ def test_only_should_be_read_from_scope
+ with_test_routes do
+ get '/only/clubs'
+ assert_equal 'only/clubs#index', @response.body
+ assert_equal '/only/clubs', only_clubs_path
+
+ get '/only/clubs/1/edit'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { edit_only_club_path(:id => '1') }
+
+ get '/only/clubs/1/players'
+ assert_equal 'only/players#index', @response.body
+ assert_equal '/only/clubs/1/players', only_club_players_path(:club_id => '1')
+
+ get '/only/clubs/1/players/2/edit'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { edit_only_club_player_path(:club_id => '1', :id => '2') }
+
+ get '/only/clubs/1/chairman'
+ assert_equal 'only/chairmen#show', @response.body
+ assert_equal '/only/clubs/1/chairman', only_club_chairman_path(:club_id => '1')
+
+ get '/only/clubs/1/chairman/edit'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { edit_only_club_chairman_path(:club_id => '1') }
+ end
+ end
+
+ def test_except_should_be_read_from_scope
+ with_test_routes do
+ get '/except/clubs'
+ assert_equal 'except/clubs#index', @response.body
+ assert_equal '/except/clubs', except_clubs_path
+
+ get '/except/clubs/1/edit'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { edit_except_club_path(:id => '1') }
+
+ get '/except/clubs/1/players'
+ assert_equal 'except/players#index', @response.body
+ assert_equal '/except/clubs/1/players', except_club_players_path(:club_id => '1')
+
+ get '/except/clubs/1/players/2/edit'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { edit_except_club_player_path(:club_id => '1', :id => '2') }
+
+ get '/except/clubs/1/chairman'
+ assert_equal 'except/chairmen#show', @response.body
+ assert_equal '/except/clubs/1/chairman', except_club_chairman_path(:club_id => '1')
+
+ get '/except/clubs/1/chairman/edit'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { edit_except_club_chairman_path(:club_id => '1') }
+ end
+ end
+
+ def test_only_option_should_override_scope
+ with_test_routes do
+ get '/only/sectors'
+ assert_equal 'only/sectors#index', @response.body
+ assert_equal '/only/sectors', only_sectors_path
+
+ get '/only/sectors/1'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { only_sector_path(:id => '1') }
+ end
+ end
+
+ def test_only_option_should_not_inherit
+ with_test_routes do
+ get '/only/sectors/1/companies/2'
+ assert_equal 'only/companies#show', @response.body
+ assert_equal '/only/sectors/1/companies/2', only_sector_company_path(:sector_id => '1', :id => '2')
+
+ get '/only/sectors/1/leader'
+ assert_equal 'only/leaders#show', @response.body
+ assert_equal '/only/sectors/1/leader', only_sector_leader_path(:sector_id => '1')
+ end
+ end
+
+ def test_except_option_should_override_scope
+ with_test_routes do
+ get '/except/sectors'
+ assert_equal 'except/sectors#index', @response.body
+ assert_equal '/except/sectors', except_sectors_path
+
+ get '/except/sectors/1'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { except_sector_path(:id => '1') }
+ end
+ end
+
+ def test_except_option_should_not_inherit
+ with_test_routes do
+ get '/except/sectors/1/companies/2'
+ assert_equal 'except/companies#show', @response.body
+ assert_equal '/except/sectors/1/companies/2', except_sector_company_path(:sector_id => '1', :id => '2')
+
+ get '/except/sectors/1/leader'
+ assert_equal 'except/leaders#show', @response.body
+ assert_equal '/except/sectors/1/leader', except_sector_leader_path(:sector_id => '1')
+ end
+ end
+
+ def test_except_option_should_override_scoped_only
+ with_test_routes do
+ get '/only/sectors/1/managers'
+ assert_equal 'only/managers#index', @response.body
+ assert_equal '/only/sectors/1/managers', only_sector_managers_path(:sector_id => '1')
+
+ get '/only/sectors/1/managers/2'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { only_sector_manager_path(:sector_id => '1', :id => '2') }
+ end
+ end
+
+ def test_only_option_should_override_scoped_except
+ with_test_routes do
+ get '/except/sectors/1/managers'
+ assert_equal 'except/managers#index', @response.body
+ assert_equal '/except/sectors/1/managers', except_sector_managers_path(:sector_id => '1')
+
+ get '/except/sectors/1/managers/2'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { except_sector_manager_path(:sector_id => '1', :id => '2') }
+ end
+ end
+
+ def test_only_scope_should_override_parent_scope
+ with_test_routes do
+ get '/only/sectors/1/companies/2/divisions'
+ assert_equal 'only/divisions#index', @response.body
+ assert_equal '/only/sectors/1/companies/2/divisions', only_sector_company_divisions_path(:sector_id => '1', :company_id => '2')
+
+ get '/only/sectors/1/companies/2/divisions/3'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { only_sector_company_division_path(:sector_id => '1', :company_id => '2', :id => '3') }
+ end
+ end
+
+ def test_except_scope_should_override_parent_scope
+ with_test_routes do
+ get '/except/sectors/1/companies/2/divisions'
+ assert_equal 'except/divisions#index', @response.body
+ assert_equal '/except/sectors/1/companies/2/divisions', except_sector_company_divisions_path(:sector_id => '1', :company_id => '2')
+
+ get '/except/sectors/1/companies/2/divisions/3'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { except_sector_company_division_path(:sector_id => '1', :company_id => '2', :id => '3') }
+ end
+ end
+
+ def test_except_scope_should_override_parent_only_scope
+ with_test_routes do
+ get '/only/sectors/1/companies/2/departments'
+ assert_equal 'only/departments#index', @response.body
+ assert_equal '/only/sectors/1/companies/2/departments', only_sector_company_departments_path(:sector_id => '1', :company_id => '2')
+
+ get '/only/sectors/1/companies/2/departments/3'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { only_sector_company_department_path(:sector_id => '1', :company_id => '2', :id => '3') }
+ end
+ end
+
+ def test_only_scope_should_override_parent_except_scope
+ with_test_routes do
+ get '/except/sectors/1/companies/2/departments'
+ assert_equal 'except/departments#index', @response.body
+ assert_equal '/except/sectors/1/companies/2/departments', except_sector_company_departments_path(:sector_id => '1', :company_id => '2')
+
+ get '/except/sectors/1/companies/2/departments/3'
+ assert_equal 'Not Found', @response.body
+ assert_raise(NoMethodError) { except_sector_company_department_path(:sector_id => '1', :company_id => '2', :id => '3') }
+ end
+ end
+
private
def with_test_routes
yield
View
22 actionpack/test/dispatch/session/cookie_store_test.rb
@@ -30,6 +30,11 @@ def get_session_id
render :text => "id: #{request.session_options[:id]}"
end
+ def call_session_clear
+ session.clear
+ head :ok
+ end
+
def call_reset_session
reset_session
head :ok
@@ -175,6 +180,23 @@ def test_getting_from_nonexistent_session
end
end
+ def test_setting_session_value_after_session_clear
+ with_test_route_set do
+ get '/set_session_value'
+ assert_response :success
+ session_payload = response.body
+ assert_equal "_myapp_session=#{response.body}; path=/; HttpOnly",
+ headers['Set-Cookie']
+
+ get '/call_session_clear'
+ assert_response :success
+
+ get '/get_session_value'
+ assert_response :success
+ assert_equal 'foo: nil', response.body
+ end
+ end
+
def test_persistent_session_id
with_test_route_set do
cookies[SessionKey] = SignedBar
View
16 actionpack/test/dispatch/url_generation_test.rb
@@ -2,24 +2,24 @@
module TestUrlGeneration
class WithMountPoint < ActionDispatch::IntegrationTest
- Router = ActionDispatch::Routing::RouteSet.new
- Router.draw { match "/foo", :to => "my_route_generating#index", :as => :foo }
+ Routes = ActionDispatch::Routing::RouteSet.new
+ Routes.draw { match "/foo", :to => "my_route_generating#index", :as => :foo }
class ::MyRouteGeneratingController < ActionController::Base
- include Router.url_helpers
+ include Routes.url_helpers
def index
render :text => foo_path
end
end
- include Router.url_helpers
+ include Routes.url_helpers
- def _router
- Router
+ def _routes
+ Routes
end
def app
- Router
+ Routes
end
test "generating URLS normally" do
@@ -30,7 +30,7 @@ def app
assert_equal "/bar/foo", foo_path(:script_name => "/bar")
end
- test "the request's SCRIPT_NAME takes precedence over the router's" do
+ test "the request's SCRIPT_NAME takes precedence over the routes'" do
get "/foo", {}, 'SCRIPT_NAME' => "/new"
assert_equal "/new/foo", response.body
end
View
29 actionpack/test/template/url_helper_test.rb
@@ -406,6 +406,14 @@ def sort_query_string_params(uri)
class UrlHelperControllerTest < ActionController::TestCase
class UrlHelperController < ActionController::Base
test_routes do |map|
+ match 'url_helper_controller_test/url_helper/show/:id',
+ :to => 'url_helper_controller_test/url_helper#show',
+ :as => :show
+
+ match 'url_helper_controller_test/url_helper/profile/:name',
+ :to => 'url_helper_controller_test/url_helper#show',
+ :as => :profile
+
match 'url_helper_controller_test/url_helper/show_named_route',
:to => 'url_helper_controller_test/url_helper#show_named_route',
:as => :show_named_route
@@ -418,6 +426,14 @@ class UrlHelperController < ActionController::Base
:as => :normalize_recall_params
end
+ def show
+ if params[:name]
+ render :inline => 'ok'
+ else
+ redirect_to profile_path(params[:id])
+ end
+ end
+
def show_url_for
render :inline => "<%= url_for :controller => 'url_helper_controller_test/url_helper', :action => 'show_url_for' %>"
end
@@ -484,15 +500,24 @@ def default_url_options(options = nil)
assert_equal 'http://testtwo.host/url_helper_controller_test/url_helper/show_named_route', @response.body
end
- def test_recall_params_should_be_normalized_when_using_block_route
+ def test_recall_params_should_be_normalized
get :normalize_recall_params
assert_equal '/url_helper_controller_test/url_helper/normalize_recall_params', @response.body
end
- def test_recall_params_should_not_be_changed_when_using_normal_route
+ def test_recall_params_should_not_be_changed
get :recall_params_not_changed
assert_equal '/url_helper_controller_test/url_helper/show_url_for', @response.body
end
+
+ def test_recall_params_should_normalize_id
+ get :show, :id => '123'
+ assert_equal 302, @response.status
+ assert_equal 'http://test.host/url_helper_controller_test/url_helper/profile/123', @response.location
+
+ get :show, :name => '123'
+ assert_equal 'ok', @response.body
+ end
end
class TasksController < ActionController::Base
View
1 activemodel/lib/active_model/errors.rb
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
require 'active_support/core_ext/array/wrap'
+require 'active_support/core_ext/array/conversions'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash/reverse_merge'
View
15 activerecord/lib/active_record/associations.rb
@@ -770,15 +770,20 @@ module ClassMethods
# Objects will be in addition destroyed if they're associated with <tt>:dependent => :destroy</tt>,
# and deleted if they're associated with <tt>:dependent => :delete_all</tt>.
# [collection=objects]
- # Replaces the collections content by deleting and adding objects as appropriate.
+ # Replaces the collections content by deleting and adding objects as appropriate. If the <tt>:through</tt>
+ # option is true callbacks in the join models are triggered except destroy callbacks, since deletion is
+ # direct.
# [collection_singular_ids]
# Returns an array of the associated objects' ids
# [collection_singular_ids=ids]
- # Replace the collection with the objects identified by the primary keys in +ids+
+ # Replace the collection with the objects identified by the primary keys in +ids+. This
+ # method loads the models and calls <tt>collection=</tt>. See above.
# [collection.clear]
# Removes every object from the collection. This destroys the associated objects if they
# are associated with <tt>:dependent => :destroy</tt>, deletes them directly from the
# database if <tt>:dependent => :delete_all</tt>, otherwise sets their foreign keys to +NULL+.
+ # If the <tt>:through</tt> option is true no destroy callbacks are invoked on the join models.
+ # Join models are directly deleted.
# [collection.empty?]
# Returns +true+ if there are no associated objects.
# [collection.size]
@@ -871,9 +876,11 @@ module ClassMethods
# [:as]
# Specifies a polymorphic interface (See <tt>belongs_to</tt>).
# [:through]
- # Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt> and <tt>:foreign_key</tt>
+ # Specifies a join model through which to perform the query. Options for <tt>:class_name</tt> and <tt>:foreign_key</tt>
# are ignored, as the association uses the source reflection. You can only use a <tt>:through</tt> query through a <tt>belongs_to</tt>
- # <tt>has_one</tt> or <tt>has_many</tt> association on the join model.
+ # <tt>has_one</tt> or <tt>has_many</tt> association on the join model. The collection of join models can be managed via the collection
+ # API. For example, new join models are created for newly associated objects, and if some are gone their rows are deleted (directly,
+ # no destroy callbacks are triggered).
# [:source]
# Specifies the source association name used by <tt>has_many :through</tt> queries. Only use it if the name cannot be
# inferred from the association. <tt>has_many :subscribers, :through => :subscriptions</tt> will look for either <tt>:subscribers</tt> or
View
7 activerecord/lib/active_record/associations/association_collection.rb
@@ -393,7 +393,12 @@ def load_target
@target = find_target.map do |f|
i = @target.index(f)
t = @target.delete_at(i) if i
- (t && t.changed?) ? t : f
+ if t && t.changed?
+ t
+ else
+ f.mark_for_destruction if t && t.marked_for_destruction?
+ f
+ end
end + @target
else
@target = find_target
View
11 activerecord/lib/active_record/base.rb
@@ -898,11 +898,6 @@ def sti_name
store_full_sti_class ? name : name.demodulize
end
- def relation
- @relation ||= Relation.new(self, arel_table)
- finder_needs_type_condition? ? @relation.where(type_condition) : @relation
- end
-
def arel_table
@arel_table ||= Arel::Table.new(table_name, :engine => arel_engine)
end
@@ -943,6 +938,12 @@ def scoped_methods #:nodoc:
end
private
+
+ def relation #:nodoc:
+ @relation ||= Relation.new(self, arel_table)
+ finder_needs_type_condition? ? @relation.where(type_condition) : @relation
+ end
+
# Finder methods must instantiate through this method to work with the
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.
View
2 activerecord/lib/active_record/named_scope.rb
@@ -29,7 +29,7 @@ def scoped(options = nil)
if options.present?
scoped.apply_finder_options(options)
else
- current_scoped_methods ? unscoped.merge(current_scoped_methods) : unscoped.clone
+ current_scoped_methods ? relation.merge(current_scoped_methods) : relation.clone
end
end
View
6 activerecord/lib/active_record/observer.rb
@@ -94,7 +94,7 @@ class Observer < ActiveModel::Observer
def initialize
super
- observed_subclasses.each { |klass| add_observer!(klass) }
+ observed_descendants.each { |klass| add_observer!(klass) }
end
def self.method_added(method)
@@ -108,8 +108,8 @@ def self.method_added(method)
protected
- def observed_subclasses
- observed_classes.sum([]) { |klass| klass.send(:descendants) }
+ def observed_descendants
+ observed_classes.sum([]) { |klass| klass.descendants }
end
def observe_callbacks?
View
2 activerecord/lib/active_record/railties/databases.rake
@@ -1,7 +1,7 @@
namespace :db do
task :load_config => :rails_env do
require 'active_record'
- ActiveRecord::Base.configurations = Rails::Application.config.database_configuration
+ ActiveRecord::Base.configurations = Rails.application.config.database_configuration
end
namespace :create do
View
6 activerecord/test/cases/nested_attributes_test.rb
@@ -489,6 +489,12 @@ def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name
end
+ def test_should_not_remove_scheduled_destroys_when_loading_association
+ @pirate.reload
+ @pirate.send(association_setter, [{ :id => @child_1.id, :_destroy => '1' }])
+ assert @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.marked_for_destruction?
+ end
+
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
@child_1.stubs(:id).returns('ABC1X')
@child_2.stubs(:id).returns('ABC2X')
View
1 activesupport/lib/active_support/all.rb
@@ -1,4 +1,3 @@
require 'active_support'
require 'active_support/time'
require 'active_support/core_ext'
-require 'active_support/json'
View
4 activesupport/lib/active_support/callbacks.rb
@@ -432,7 +432,7 @@ def __update_callbacks(name, filters = [], block = nil) #:nodoc:
options = filters.last.is_a?(Hash) ? filters.pop : {}
filters.unshift(block) if block
- ([self] + self.descendants).each do |target|
+ ([self] + ActiveSupport::DescendantsTracker.descendants(self)).each do |target|
chain = target.send("_#{name}_callbacks")
yield chain, type, filters, options
target.__define_runner(name)
@@ -506,7 +506,7 @@ def skip_callback(name, *filter_list, &block)
def reset_callbacks(symbol)
callbacks = send("_#{symbol}_callbacks")
- self.descendants.each do |target|
+ ActiveSupport::DescendantsTracker.descendants(self).each do |target|
chain = target.send("_#{symbol}_callbacks")
callbacks.each { |c| chain.delete(c) }
target.__define_runner(symbol)
View
57 activesupport/lib/active_support/core_ext/class/subclasses.rb
@@ -2,54 +2,49 @@
require 'active_support/core_ext/module/reachable'
class Class #:nodoc:
- # Returns an array with the names of the subclasses of +self+ as strings.
- #
- # Integer.subclasses # => ["Bignum", "Fixnum"]
- def subclasses
- Class.subclasses_of(self).map { |o| o.to_s }
- end
-
# Rubinius
if defined?(Class.__subclasses__)
+ alias :subclasses :__subclasses__
+
def descendants
- subclasses = []
- __subclasses__.each {|k| subclasses << k; subclasses.concat k.descendants }
- subclasses
+ descendants = []
+ __subclasses__.each do |k|
+ descendants << k
+ descendants.concat k.descendants
+ end
+ descendants
end
- else
- # MRI
+ else # MRI
begin
ObjectSpace.each_object(Class.new) {}
def descendants
- subclasses = []
+ descendants = []
ObjectSpace.each_object(class << self; self; end) do |k|
- subclasses << k unless k == self
+ descendants.unshift k unless k == self
end
- subclasses
+ descendants
end
- # JRuby
- rescue StandardError
+ rescue StandardError # JRuby
def descendants
- subclasses = []
+ descendants = []
ObjectSpace.each_object(Class) do |k|
- subclasses << k if k < self
+ descendants.unshift k if k < self
end
- subclasses.uniq!
- subclasses
+ descendants.uniq!
+ descendants
end
end
- end
- # Exclude this class unless it's a subclass of our supers and is defined.
- # We check defined? in case we find a removed class that has yet to be
- # garbage collected. This also fails for anonymous classes -- please
- # submit a patch if you have a workaround.
- def self.subclasses_of(*superclasses) #:nodoc:
- subclasses = []
- superclasses.each do |klass|
- subclasses.concat klass.descendants.select {|k| k.anonymous? || k.reachable?}
+ # Returns an array with the direct children of +self+.
+ #
+ # Integer.subclasses # => [Bignum, Fixnum]
+ def subclasses
+ subclasses, chain = [], descendants
+ chain.each do |k|
+ subclasses << k unless chain.any? { |c| c > k }
+ end
+ subclasses
end
- subclasses
end
end
View
3 activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -1,7 +1,8 @@
require 'date'
require 'active_support/duration'
-require 'active_support/core_ext/time/zones'
require 'active_support/core_ext/object/acts_like'
+require 'active_support/core_ext/date/zones'
+require 'active_support/core_ext/time/zones'
class Date
if RUBY_VERSION < '1.9'
View
16 activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -1,6 +1,6 @@
require 'date'
require 'active_support/inflector'
-require 'active_support/core_ext/time/calculations'
+require 'active_support/core_ext/date/zones'
class Date
DATE_FORMATS = {
@@ -13,10 +13,10 @@ class Date
}
# Ruby 1.9 has Date#to_time which converts to localtime only.
- remove_method :to_time if instance_methods.include?(:to_time)
+ remove_method :to_time if method_defined?(:to_time)
# Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
- remove_method :xmlschema if instance_methods.include?(:xmlschema)
+ remove_method :xmlschema if method_defined?(:xmlschema)
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
#
@@ -81,16 +81,6 @@ def to_date
def to_time(form = :local)
::Time.send("#{form}_time", year, month, day)
end
-
- # Converts Date to a TimeWithZone in the current zone if Time.zone_default is set,
- # otherwise converts Date to a Time via Date#to_time
- def to_time_in_current_zone
- if ::Time.zone_default
- ::Time.zone.local(year, month, day)
- else
- to_time
- end
- end
# Converts a Date instance to a DateTime, where the time is set to the beginning of the day
# and UTC offset is set to 0.
View
14 activesupport/lib/active_support/core_ext/date/zones.rb
@@ -0,0 +1,14 @@
+require 'date'
+require 'active_support/core_ext/time/zones'
+
+class Date
+ # Converts Date to a TimeWithZone in the current zone if Time.zone_default is set,
+ # otherwise converts Date to a Time via Date#to_time
+ def to_time_in_current_zone
+ if ::Time.zone_default
+ ::Time.zone.local(year, month, day)
+ else
+ to_time
+ end
+ end
+end
View
1 activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -1,5 +1,6 @@
require 'rational' unless RUBY_VERSION >= '1.9.2'
require 'active_support/core_ext/object/acts_like'
+require 'active_support/core_ext/time/zones'
class DateTime
class << self
View
1 activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -1,6 +1,7 @@
require 'active_support/inflector'
require 'active_support/core_ext/time/conversions'
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
View
2 activesupport/lib/active_support/core_ext/date_time/zones.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/time/zones'
+
class DateTime
# Returns the simultaneous time in <tt>Time.zone</tt>.
#
View
2 activesupport/lib/active_support/core_ext/object.rb
@@ -6,9 +6,9 @@
require 'active_support/core_ext/object/conversions'
require 'active_support/core_ext/object/instance_variables'
require 'active_support/core_ext/object/misc'
-require 'active_support/core_ext/object/extending'
require 'active_support/core_ext/object/returning'
+require 'active_support/core_ext/object/to_json'
require 'active_support/core_ext/object/to_param'
require 'active_support/core_ext/object/to_query'
require 'active_support/core_ext/object/with_options'
View
11 activesupport/lib/active_support/core_ext/object/extending.rb
@@ -1,11 +0,0 @@
-require 'active_support/core_ext/class/subclasses'
-
-class Object
- # Exclude this class unless it's a subclass of our supers and is defined.
- # We check defined? in case we find a removed class that has yet to be
- # garbage collected. This also fails for anonymous classes -- please
- # submit a patch if you have a workaround.
- def subclasses_of(*superclasses) #:nodoc:
- Class.subclasses_of(*superclasses)
- end
-end
View
19 activesupport/lib/active_support/core_ext/object/to_json.rb
@@ -0,0 +1,19 @@