aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG.md9
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb10
-rw-r--r--actionpack/lib/action_dispatch/routing/url_for.rb2
-rw-r--r--actionpack/test/controller/url_for_test.rb18
-rw-r--r--actionview/lib/action_view/routing_url_for.rb2
-rw-r--r--activerecord/lib/active_record/integration.rb7
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb11
-rw-r--r--activerecord/test/cases/integration_test.rb6
-rw-r--r--activesupport/CHANGELOG.md11
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute_accessors.rb11
-rw-r--r--activesupport/test/core_ext/class/attribute_accessor_test.rb9
-rw-r--r--guides/source/active_support_core_extensions.md9
-rw-r--r--guides/source/command_line.md2
13 files changed, 95 insertions, 12 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index dea80abfcd..19eb098d16 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,3 +1,12 @@
+* Take a hash with options inside array in #url_for
+
+ Example:
+
+ url_for [:new, :admin, :post, { param: 'value' }]
+ # => http://example.com/admin/posts/new?param=value
+
+ *Andrey Ognevsky*
+
* Add `session#fetch` method
fetch behaves similarly to [Hash#fetch](http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch),
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index f4140f21f5..3d3299afb3 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -168,7 +168,7 @@ module ActionDispatch
end
def normalize_conditions!
- @conditions.merge!(:path_info => path)
+ @conditions[:path_info] = path
constraints.each do |key, condition|
unless segment_keys.include?(key) || key == :controller
@@ -176,12 +176,13 @@ module ActionDispatch
end
end
- @conditions[:required_defaults] = []
+ required_defaults = []
options.each do |key, required_default|
unless segment_keys.include?(key) || IGNORE_OPTIONS.include?(key) || Regexp === required_default
- @conditions[:required_defaults] << key
+ required_defaults << key
end
end
+ @conditions[:required_defaults] = required_defaults
via_all = options.delete(:via) if options[:via] == :all
@@ -195,8 +196,7 @@ module ActionDispatch
end
if via = options[:via]
- list = Array(via).map { |m| m.to_s.dasherize.upcase }
- @conditions.merge!(:request_method => list)
+ @conditions[:request_method] = Array(via).map { |m| m.to_s.dasherize.upcase }
end
end
diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb
index bcebe532bf..4a0ef40873 100644
--- a/actionpack/lib/action_dispatch/routing/url_for.rb
+++ b/actionpack/lib/action_dispatch/routing/url_for.rb
@@ -155,6 +155,8 @@ module ActionDispatch
_routes.url_for(options.symbolize_keys.reverse_merge!(url_options))
when String
options
+ when Array
+ polymorphic_url(options, options.extract_options!)
else
polymorphic_url(options)
end
diff --git a/actionpack/test/controller/url_for_test.rb b/actionpack/test/controller/url_for_test.rb
index 088ad73f2f..d2b4952759 100644
--- a/actionpack/test/controller/url_for_test.rb
+++ b/actionpack/test/controller/url_for_test.rb
@@ -370,6 +370,24 @@ module AbstractController
assert_equal("/c/a?show=false", W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :show => false))
end
+ def test_url_generation_with_array_and_hash
+ with_routing do |set|
+ set.draw do
+ namespace :admin do
+ resources :posts
+ end
+ end
+
+ kls = Class.new { include set.url_helpers }
+ kls.default_url_options[:host] = 'www.basecamphq.com'
+
+ controller = kls.new
+ assert_equal("http://www.basecamphq.com/admin/posts/new?param=value",
+ controller.send(:url_for, [:new, :admin, :post, { param: 'value' }])
+ )
+ end
+ end
+
private
def extract_params(url)
url.split('?', 2).last.split('&').sort
diff --git a/actionview/lib/action_view/routing_url_for.rb b/actionview/lib/action_view/routing_url_for.rb
index f10e7e88ba..33be06cbf7 100644
--- a/actionview/lib/action_view/routing_url_for.rb
+++ b/actionview/lib/action_view/routing_url_for.rb
@@ -83,6 +83,8 @@ module ActionView
super
when :back
_back_url
+ when Array
+ polymorphic_path(options, options.extract_options!)
else
polymorphic_path(options)
end
diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb
index 951db5b756..27576b1e61 100644
--- a/activerecord/lib/active_record/integration.rb
+++ b/activerecord/lib/active_record/integration.rb
@@ -81,6 +81,13 @@ module ActiveRecord
# user.id # => 123
# user_path(user) # => "/users/123-fancy-pants"
#
+ # Values longer than 20 characters will be truncated. The value
+ # is truncated word by word.
+ #
+ # user = User.find_by(name: 'David HeinemeierHansson')
+ # user.id # => 125
+ # user_path(user) # => "/users/125-david"
+ #
# Because the generated param begins with the record's +id+, it is
# suitable for passing to +find+. In a controller, for example:
#
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 3a02bf90e9..d91d6367a3 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -6,11 +6,12 @@ module ActiveRecord
# If no record can be found for all of the listed ids, then RecordNotFound will be raised. If the primary key
# is an integer, find by id coerces its arguments using +to_i+.
#
- # Person.find(1) # returns the object for ID = 1
- # Person.find("1") # returns the object for ID = 1
- # Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
- # Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
- # Person.find([1]) # returns an array for the object with ID = 1
+ # Person.find(1) # returns the object for ID = 1
+ # Person.find("1") # returns the object for ID = 1
+ # Person.find("31-sarah") # returns the object for ID = 31
+ # Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
+ # Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
+ # Person.find([1]) # returns an array for the object with ID = 1
# Person.where("administrator = 1").order("created_on DESC").find(1)
#
# <tt>ActiveRecord::RecordNotFound</tt> will be raised if one or more ids are not found.
diff --git a/activerecord/test/cases/integration_test.rb b/activerecord/test/cases/integration_test.rb
index 8097f6e36e..07ffcef875 100644
--- a/activerecord/test/cases/integration_test.rb
+++ b/activerecord/test/cases/integration_test.rb
@@ -34,6 +34,12 @@ class IntegrationTest < ActiveRecord::TestCase
assert_equal '4-a-a-a-a-a-a-a-a-a', firm.to_param
end
+ def test_to_param_class_method_truncates_edge_case
+ firm = Firm.find(4)
+ firm.name = 'David HeinemeierHansson'
+ assert_equal '4-david', firm.to_param
+ end
+
def test_to_param_class_method_squishes
firm = Firm.find(4)
firm.name = "ab \n" * 100
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index bb66b0ffa2..95253567c0 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,14 @@
+* Unify `cattr_*` interface: allow to pass a block to `cattr_reader`.
+
+ Example:
+
+ class A
+ cattr_reader(:defr) { 'default_reader_value' }
+ end
+ A.defr # => 'default_reader_value'
+
+ *Alexey Chernenkov*
+
* Improved compatibility with the stdlib JSON gem.
Previously, calling `::JSON.{generate,dump}` sometimes causes unexpected
diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
index 34859617c9..6daa828b24 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -29,6 +29,16 @@ class Class
# end
#
# Person.new.hair_colors # => NoMethodError
+ #
+ # Also, you can pass a block to set up the attribute with a default value.
+ #
+ # class Person
+ # cattr_reader :hair_colors do
+ # [:brown, :black, :blonde, :red]
+ # end
+ # end
+ #
+ # Person.hair_colors # => [:brown, :black, :blonde, :red]
def cattr_reader(*syms)
options = syms.extract_options!
syms.each do |sym|
@@ -50,6 +60,7 @@ class Class
end
EOS
end
+ class_variable_set("@@#{sym}", yield) if block_given?
end
end
diff --git a/activesupport/test/core_ext/class/attribute_accessor_test.rb b/activesupport/test/core_ext/class/attribute_accessor_test.rb
index 0d5f39a72b..3bc948f3a6 100644
--- a/activesupport/test/core_ext/class/attribute_accessor_test.rb
+++ b/activesupport/test/core_ext/class/attribute_accessor_test.rb
@@ -8,6 +8,9 @@ class ClassAttributeAccessorTest < ActiveSupport::TestCase
cattr_accessor :bar, :instance_writer => false
cattr_reader :shaq, :instance_reader => false
cattr_accessor :camp, :instance_accessor => false
+ cattr_accessor(:defa) { 'default_accessor_value' }
+ cattr_reader(:defr) { 'default_reader_value' }
+ cattr_writer(:defw) { 'default_writer_value' }
end
@object = @class.new
end
@@ -58,4 +61,10 @@ class ClassAttributeAccessorTest < ActiveSupport::TestCase
end
assert_equal "invalid class attribute name: 1nvalid", exception.message
end
+
+ def test_should_use_default_value_if_block_passed
+ assert_equal 'default_accessor_value', @class.defa
+ assert_equal 'default_reader_value', @class.defr
+ assert_equal 'default_writer_value', @class.class_variable_get('@@defw')
+ end
end
diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md
index 648036fb3f..84a169b3b9 100644
--- a/guides/source/active_support_core_extensions.md
+++ b/guides/source/active_support_core_extensions.md
@@ -1093,6 +1093,15 @@ end
we can access `field_error_proc` in views.
+Also, you can pass a block to `cattr_*` to set up the attribute with a default value:
+
+```ruby
+class MysqlAdapter < AbstractAdapter
+ # Generates class methods to access @@emulate_booleans with default value of true.
+ cattr_accessor(:emulate_booleans) { true }
+end
+```
+
The generation of the reader instance method can be prevented by setting `:instance_reader` to `false` and the generation of the writer instance method can be prevented by setting `:instance_writer` to `false`. Generation of both methods can be prevented by setting `:instance_accessor` to `false`. In all cases, the value must be exactly `false` and not any false value.
```ruby
diff --git a/guides/source/command_line.md b/guides/source/command_line.md
index 1b0b93c3bc..3b80faec7f 100644
--- a/guides/source/command_line.md
+++ b/guides/source/command_line.md
@@ -56,8 +56,6 @@ Rails will set you up with what seems like a huge amount of stuff for such a tin
The `rails server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to access your application through a web browser.
-INFO: WEBrick isn't your only option for serving Rails. We'll get to that [later](#server-with-different-backends).
-
With no further work, `rails server` will run our new shiny Rails app:
```bash