Skip to content

Commit bd9bb2a

Browse files
authored
Merge pull request #344 from kbrock/satan_spawn
2 parents fb93acb + 7ede5da commit bd9bb2a

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/active_hash/conditions.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def initialize(conditions = [])
77
@conditions = conditions
88
end
99

10+
def initialize_copy(orig)
11+
super
12+
@conditions = @conditions.dup
13+
end
14+
1015
def matches?(record)
1116
conditions.all? do |condition|
1217
condition.matches?(record)

lib/active_hash/relation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(klass, all_records, conditions = nil, order_values = nil)
1919
end
2020

2121
def where(conditions_hash = :chain)
22-
return WhereChain.new(self) if conditions_hash == :chain
22+
return WhereChain.new(spawn) if conditions_hash == :chain
2323

2424
spawn.where!(conditions_hash)
2525
end
@@ -80,7 +80,7 @@ def invert_where!
8080
end
8181

8282
def spawn
83-
self.class.new(klass, all_records, conditions, order_values)
83+
self.class.new(klass, all_records, conditions.dup, order_values)
8484
end
8585

8686
def order!(*options)

spec/active_hash/base_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ class Region < ActiveHash::Base
497497
it "filters records for multiple conditions" do
498498
expect(Country.where.not(:id => 1, :name => 'Mexico')).to match_array([Country.find(2)])
499499
end
500+
501+
it "does not mutate the parent relation" do
502+
english = Country.where(language: 'English')
503+
english.where.not(name: 'US')
504+
expect(english.map(&:name)).to match_array(%w[US Canada])
505+
end
500506
end
501507

502508
describe ".find_by" do
@@ -574,6 +580,14 @@ class Region < ActiveHash::Base
574580
it "doesn't finds nil records when searching for ''" do
575581
expect(Country.find_by(:language => '')).to be_nil
576582
end
583+
584+
it "does not mutate the relation when called multiple times" do
585+
countries = Country.all
586+
expect(countries.find_by(id: 1).name).to eq("US")
587+
expect(countries.find_by(id: 2).name).to eq("Canada")
588+
expect(countries.find_by(id: 1).name).to eq("US")
589+
expect(countries.length).to eq(4)
590+
end
577591
end
578592

579593
describe ".find_by!" do

0 commit comments

Comments
 (0)