thoughtbot/shoulda-matchers — pull request #98 by bloudermilk (2012-04-11 19:04:05+00:00)
Title: Adds accept_nested_attributes_for matcher

Hey @gabebw,

As per our discussion in #94 I've added the `accept_nested_attributes_for` matcher to the `Should::Matchers::ActiveRecord` module, along with a suite of passing specs. As you'll discover, my fork currently supports the options `:allow_destroy`, `:limit`, and `:update_only`. The only missing option is `:reject_if`, which I wanted to get your feedback on before writing. See the example below:

```ruby
class Person < ActiveRecord::Base
SOME_PROC = proc {|attrs| !attrs["lol"] }

has_many :cats, reject_if: :all_blank
has_many :dogs, reject_if: :my_custom_method
has_many :problems, reject_if: SOME_PROC
has_many :birds, reject_if: proc {|attrs| !attrs["foo"] }
end

describe Person do
it { should accept_nested_attributes_for(:cats).reject_if(:all_blank) }
it { should accept_nested_attributes_for(:dogs).reject_if(:my_custom_method) }
it { should accept_nested_attributes_for(:problems).reject_if(Person::SOME_PROC) }
it { should accept_nested_attributes_for(:birds).reject_if(Proc) }
end
```

There are four distinct ways to implement `:reject_if` from the user's perspective. As you can imagine, the latter case is the problematic one since the user cannot specify the *exact* proc to be called when writing their spec. In this case we can provide some level of support (checking for any instance of `Proc`), it's just a matter of how we want the syntax to look. Here's a few alternatives I've been playing around with:

```ruby
it { should accept_nested_attributes_for(:birds).reject_if(Proc) }
it { should accept_nested_attributes_for(:birds).reject_if_proc }
it { should accept_nested_attributes_for(:birds).reject_if(true) }
it { should accept_nested_attributes_for(:birds).reject_if }
```

Let me know what you think. Thanks for your time.

-B