1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix error when using with_options with lambda.

It was causing error when using `with_options` passing a lambda as its
last argument.

    class User < ActiveRecord::Base
      with_options dependent: :destroy do |assoc|
        assoc.has_many :profiles, -> { where(active: true) }
      end
    end

It was happening because the `option_merger` was taking the last
argument and checking if it was a Hash. This breaks the HasMany usage,
because its last argument can be a Hash or a Proc.

As the behavior described in this test:
https://github.com/rails/rails/blob/master/activesupport/test/option_merger_test.rb#L69
the method will only accept the lambda, this way it will keep the expected behavior. See 9eaa0a34
This commit is contained in:
Lauro Caetano 2014-04-02 19:27:55 -03:00
parent 7531404a44
commit db5d26c9d7
6 changed files with 24 additions and 1 deletions

View file

@ -1,3 +1,9 @@
* Fixed error when using `with_options` with lambda.
Fixes #9805.
*Lauro Caetano*
* Treat blank UUID values as `nil`.
Example:

View file

@ -24,6 +24,8 @@ require 'models/minivan'
require 'models/speedometer'
require 'models/reference'
require 'models/job'
require 'models/college'
require 'models/student'
class HasManyAssociationsTestForReorderWithJoinDependency < ActiveRecord::TestCase
fixtures :authors, :posts, :comments
@ -65,6 +67,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
dev.developer_projects.map(&:project_id).sort
end
def test_has_many_build_with_options
college = College.create(name: 'UFMT')
student = Student.create(active: true, college_id: college.id, name: 'Sarah')
assert_equal college.students, Student.where(active: true, college_id: college.id)
end
def test_create_from_association_should_respect_default_scope
car = Car.create(:name => 'honda')
assert_equal 'honda', car.name

View file

@ -1,5 +1,10 @@
require_dependency 'models/arunit2_model'
require 'active_support/core_ext/object/with_options'
class College < ARUnit2Model
has_many :courses
with_options dependent: :destroy do |assoc|
assoc.has_many :students, -> { where(active: true) }
end
end

View file

@ -1,3 +1,4 @@
class Student < ActiveRecord::Base
has_and_belongs_to_many :lessons
belongs_to :college
end

View file

@ -638,6 +638,8 @@ ActiveRecord::Schema.define do
create_table :students, force: true do |t|
t.string :name
t.boolean :active
t.integer :college_id
end
create_table :subscribers, force: true, id: false do |t|

View file

@ -12,7 +12,7 @@ module ActiveSupport
private
def method_missing(method, *arguments, &block)
if arguments.last.is_a?(Proc)
if arguments.first.is_a?(Proc)
proc = arguments.pop
arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
else