Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add #autosave to AssociationMatcher #125

Closed
wants to merge 3 commits into from

8 participants

@calebthompson

has_one, has_many, belongs_to, and has_and_belongs_to_many relationships
can have an :autosave option, which causes the relevant associated
records to be automatically saved when their parent is saved.

I've added functionality to test that the association has the correct value for
the :autosave option.

Here's the code for AutosaveAssociation on rails/rails:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb

@calebthompson

Anyone @thoughtbot have a chance to look at this yet?

@mike-burns mike-burns commented on the diff
...shoulda/matchers/active_record/association_matcher.rb
@@ -80,6 +84,11 @@ def conditions(conditions)
self
end
+ def autosave(autosave)
+ @options[:autosave] = autosave
@mike-burns Owner

How about:

def autosave
  @options[:autosave] = true
  self
end

Not sure why you were passing a symbol, either. true.to_s produces the same thing as :true.to_s.

@calebthompson Owner

I'm not really against only having the one option. The only reason I
allowed someone to pass an argument was that they might want to ensure
that the association is not autosaved. If you'd prefer only to have
the one case, I'd be glad to go back and change that to match your
suggestion.

As for the symbols, I am not really sure why I didn't just pass the
literals, either. I'll go back through and change those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@drapergeek
Owner

@calebthompson If you'll rebase this and push up, I'll be happy to merge in.

@drapergeek drapergeek closed this
@drapergeek drapergeek reopened this
@drapergeek
Owner

Didn't mean to close it.

calebthompson added some commits
@calebthompson calebthompson Add failing specs for association_matcher autosave
has_one, has_many, belongs_to, and has_and_belongs_to_many relationships can
have an `:autosave` option, which causes the relevant associated records to be
automatically saved when their parent is saved.

I've added failing specs for passing and failing cases for each of these
association types.

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb
7c582ba
@calebthompson calebthompson Add #autosave to AssociationMatcher 21faf2c
@calebthompson calebthompson Pass boolean literals rather than symbols a842446
@calebthompson

Sure, just making sure it still works.

@drapergeek
Owner

Still doesn't seem to be rebased.

@calebthompson

Ha! Rebased onto calebthompson/shoulda-matchers master instead of thoughtbot/shoulda-matchers.

@drapergeek drapergeek closed this
@KevinSjoberg

The issue is closed but it never got merged, why? This is indeed a option needed.

@sealabcore

Agreed. Merge!

@mkasztelnik

:+1: for this feature to be merged.

@mcmire mcmire was assigned
@mcmire
Owner

I made another PR for this which brings this up to date with master: #420

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 21, 2012
  1. @calebthompson

    Add failing specs for association_matcher autosave

    calebthompson authored calebthompson committed
    has_one, has_many, belongs_to, and has_and_belongs_to_many relationships can
    have an `:autosave` option, which causes the relevant associated records to be
    automatically saved when their parent is saved.
    
    I've added failing specs for passing and failing cases for each of these
    association types.
    
    https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb
  2. @calebthompson

    Add #autosave to AssociationMatcher

    calebthompson authored calebthompson committed
  3. @calebthompson

    Pass boolean literals rather than symbols

    calebthompson authored calebthompson committed
This page is out of date. Refresh to see the latest.
View
24 lib/shoulda/matchers/active_record/association_matcher.rb
@@ -18,6 +18,8 @@ def belong_to(name)
# * <tt>dependent</tt> - tests that the association makes use of the
# dependent option.
# * <tt>:class_name</tt> - tests that the association makes use of the class_name option.
+ # * <tt>:autosave</tt> - tests that the association makes use of the
+ # autosave option.
#
# Example:
# it { should have_many(:friends) }
@@ -36,6 +38,8 @@ def have_many(name)
# * <tt>:dependent</tt> - tests that the association makes use of the
# dependent option.
# * <tt>:class_name</tt> - tests that the association makes use of the class_name option.
+ # * <tt>:autosave</tt> - tests that the association makes use of the
+ # autosave option.
#
# Example:
# it { should have_one(:god) } # unless hindu
@@ -80,6 +84,11 @@ def conditions(conditions)
self
end
+ def autosave(autosave)
+ @options[:autosave] = autosave
@mike-burns Owner

How about:

def autosave
  @options[:autosave] = true
  self
end

Not sure why you were passing a symbol, either. true.to_s produces the same thing as :true.to_s.

@calebthompson Owner

I'm not really against only having the one option. The only reason I
allowed someone to pass an argument was that they might want to ensure
that the association is not autosaved. If you'd prefer only to have
the one case, I'd be glad to go back and change that to match your
suggestion.

As for the symbols, I am not really sure why I didn't just pass the
literals, either. I'll go back through and change those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ self
+ end
+
def class_name(class_name)
@options[:class_name] = class_name
self
@@ -93,6 +102,7 @@ def matches?(subject)
through_association_valid? &&
dependent_correct? &&
class_name_correct? &&
+ autosave_correct? &&
order_correct? &&
conditions_correct? &&
join_table_exists?
@@ -112,6 +122,7 @@ def description
description += " dependent => #{@options[:dependent]}" if @options.key?(:dependent)
description += " class_name => #{@options[:class_name]}" if @options.key?(:class_name)
description += " order => #{@options[:order]}" if @options.key?(:order)
+ description += " autosave => #{@options[:autosave]}" if @options.key?(:autosave)
description
end
@@ -194,6 +205,19 @@ def class_name_correct?
end
end
+ def autosave_correct?
+ if @options.key?(:autosave)
+ if @options[:autosave].to_s == reflection.options[:autosave].to_s
+ true
+ else
+ @missing = "#{@name} should have #{@options[:autosave]} as autosave"
+ false
+ end
+ else
+ true
+ end
+ end
+
def order_correct?
if @options.key?(:order)
if @options[:order].to_s == reflection.options[:order].to_s
View
68 spec/shoulda/active_record/association_matcher_spec.rb
@@ -98,6 +98,23 @@
end
Child.new.should_not @matcher.class_name('TreeChild')
end
+
+ it "should accept an association with a valid :autosave option" do
+ define_model :parent, :adopter => :boolean
+ define_model :child, :parent_id => :integer do
+ belongs_to :parent, :autosave => true
+ end
+ Child.new.should @matcher.autosave(true)
+ end
+
+ it "should reject an association with a valid :autosave option" do
+ define_model :parent, :adopter => :boolean
+ define_model :child, :parent_id => :integer do
+ belongs_to :parent, :autosave => false
+ end
+ Child.new.should_not @matcher.autosave(true)
+ end
+
end
context "have_many" do
@@ -240,6 +257,22 @@
Parent.new.should_not @matcher.class_name('Node')
end
+ it "should accept an association with a valid :autosave option" do
+ define_model :child, :parent_id => :integer
+ define_model :parent do
+ has_many :children, :autosave => true
+ end
+ Parent.new.should @matcher.autosave(true)
+ end
+
+ it "should reject an association with a valid :autosave option" do
+ define_model :child, :parent_id => :integer
+ define_model :parent do
+ has_many :children, :autosave => false
+ end
+ Parent.new.should_not @matcher.autosave(true)
+ end
+
it "should accept an association with a nonstandard reverse foreign key, using :inverse_of" do
define_model :child, :ancestor_id => :integer do
belongs_to :ancestor, :inverse_of => :children, :class_name => :Parent
@@ -364,6 +397,22 @@
Person.new.should_not @matcher.class_name('PersonDetail')
end
+ it "should accept an association with a valid :autosave option" do
+ define_model :detail, :person_id => :integer, :disabled => :boolean
+ define_model :person do
+ has_one :detail, :autosave => true
+ end
+ Person.new.should @matcher.autosave(true)
+ end
+
+ it "should reject an association with a valid :autosave option" do
+ define_model :detail, :person_id => :integer, :disabled => :boolean
+ define_model :person do
+ has_one :detail, :autosave => false
+ end
+ Person.new.should_not @matcher.autosave(true)
+ end
+
end
context "have_and_belong_to_many" do
@@ -445,5 +494,24 @@
Person.new.should_not @matcher.class_name('PersonRelatives')
end
+ it "should accept an association with a valid :autosave option" do
+ define_model :relatives, :adopted => :boolean
+ define_model :person do
+ has_and_belongs_to_many :relatives, :autosave => true
+ end
+ define_model :people_relative, :person_id => :integer,
+ :relative_id => :integer
+ Person.new.should @matcher.autosave(true)
+ end
+
+ it "should reject an association with a bad :autosave option" do
+ define_model :relatives, :adopted => :boolean
+ define_model :person do
+ has_and_belongs_to_many :relatives
+ end
+ define_model :people_relative, :person_id => :integer,
+ :relative_id => :integer
+ Person.new.should_not @matcher.autosave(true)
+ end
end
end
Something went wrong with that request. Please try again.