Skip to content

Commit

Permalink
CollectionProxy#exists? should return false for a new model's assoc…
Browse files Browse the repository at this point in the history
…iation with no values. Fixes #39
  • Loading branch information
jturkel committed Jan 17, 2017
1 parent d6203d6 commit dd96602
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### 0.0.11 (unreleased)
* Fix [issue 34](https://github.com/salsify/goldiloader/issues/34) - HABTM associations now honor
the auto_include option.
* Fix [issue 39](https://github.com/salsify/goldiloader/issues/39) - `CollectionProxy#exists?` should return false
for a new model's association with no values.

### 0.0.10
* Fix [issue 13](https://github.com/salsify/goldiloader/issues/13) - Eager load associations with unscope
Expand Down
8 changes: 7 additions & 1 deletion lib/goldiloader/active_record_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ def exists?(*args)
# We don't fully_load the association when arguments are passed to exists? since Rails always
# pushes this query into the database without any caching (and it likely not a common
# scenario worth optimizing).
args.empty? && @association.fully_load? ? size > 0 : super
if args.empty? && @association.fully_load?
size > 0
elsif Goldiloader::Compatibility::RAILS_3
scoped.exists?(*args)
else
scope.exists?(*args)
end
end
end
3 changes: 2 additions & 1 deletion lib/goldiloader/compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Goldiloader
module Compatibility

ACTIVE_RECORD_VERSION = ::Gem::Version.new(::ActiveRecord::VERSION::STRING)
MASS_ASSIGNMENT_SECURITY = ACTIVE_RECORD_VERSION < ::Gem::Version.new('4') || defined?(::ActiveRecord::MassAssignmentSecurity)
RAILS_3 = ACTIVE_RECORD_VERSION < ::Gem::Version.new('4')
MASS_ASSIGNMENT_SECURITY = RAILS_3 || defined?(::ActiveRecord::MassAssignmentSecurity)
ASSOCIATION_FINDER_SQL = ACTIVE_RECORD_VERSION < ::Gem::Version.new('4.1')
UNSCOPE_QUERY_METHOD = ACTIVE_RECORD_VERSION >= ::Gem::Version.new('4.1')
JOINS_EAGER_LOADABLE = ACTIVE_RECORD_VERSION >= ::Gem::Version.new('4.2')
Expand Down
21 changes: 21 additions & 0 deletions spec/goldiloader/goldiloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -763,4 +763,25 @@
end
end
end

describe "CollectionProxy#exists?" do
it "returns true for collections with values" do
expect(parent_tag1.children).to exist
end

it "returns false for collections without values" do
expect(child_tag1.children).not_to exist
end

if Goldiloader::Compatibility::RAILS_3
# Make sure we mimic the broken behavior of Rails 3
it "returns true for new models with empty associations" do
expect(Tag.new.children).to exist
end
else
it "returns false for new models with empty associations" do
expect(Tag.new.children).not_to exist
end
end
end
end

0 comments on commit dd96602

Please sign in to comment.