annkissam / has_siblings

The brother from another mother

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any `nil` parent negates the rest of the scopes

ncliffor opened this issue · comments

This line returns none if any parent is nil. This means that setting up siblings through multiple parents while return an empty collection if one parent is optional, even if two records are identical.

ex.

mother = Mother.create
father = Father.create

child1 = Child.create(mother: mother, father: nil)
child2 = Child.create(mother: mother, father: nil)

child2.siblings
=> []

child1.father = father
child2.father = father

child2.siblings
=> [child1]

suggestion

- return self.class.none if [#{parents.join(",")}].any?(&:nil?)
+ return self.class.none if [#{parents.join(",")}].all?(&:nil?)
# spec

context "with one nil parent" do
  let(:mother) { Mother.create! }
  let(:other_mother) { Mother.create! }
  let(:father) { Father.create! }

  subject!(:child) { Child.create!(mother: mother, father: nil) }
  let!(:sister) { Child.create!(mother: mother, father: nil) }
  let!(:brother) { Child.create!(mother: mother, father: nil) }
  let!(:half_sister) { Child.create!(mother: mother, father: father) }
  let!(:just_a_friend) { Child.create!(mother: other_mother) }

  its(:siblings) { are_expected.to contain_exactly(sister, brother) }
  its(:siblings) { are_expected.to be_a(ActiveRecord::Relation) }

  its(:siblings_through_mother) { are_expected.to contain_exactly(sister, brother, half_sister) }
  its(:siblings_through_mother) { are_expected.to be_a(ActiveRecord::Relation) }
end