Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #autosave to AssociationMatcher #125

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -80,6 +84,11 @@ def conditions(conditions)
self
end

def autosave(autosave)
@options[:autosave] = autosave
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

self
end

def class_name(class_name)
@options[:class_name] = class_name
self
Expand All @@ -93,6 +102,7 @@ def matches?(subject)
through_association_valid? &&
dependent_correct? &&
class_name_correct? &&
autosave_correct? &&
order_correct? &&
conditions_correct? &&
join_table_exists?
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions spec/shoulda/active_record/association_matcher_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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