Skip to content

Commit

Permalink
Merge pull request #536 from kbrock/missing_parent
Browse files Browse the repository at this point in the history
Missing parent
  • Loading branch information
kbrock committed Apr 20, 2021
2 parents 2ff497e + 3171caf commit 7f78f81
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ language: ruby
cache: bundler

rvm:
- 2.6
# rails 5.2 ...2.6 (prefer 2.5)
# rails 6.0 2.5...3.0 (prefer 2.6)
# rails 6.1 2.5..3.0 (prefer 3.0)
# rails 6.2 2.5..3.0 (prefer 3.0)
- 2.5
- 3.0

env:
Expand All @@ -18,13 +22,13 @@ gemfile:
jobs:
include:
- env: DB=pg
rvm: 2.6
rvm: 2.5
gemfile: gemfiles/gemfile_52.gemfile
- env: DB=mysql2
rvm: 2.6
rvm: 2.5
gemfile: gemfiles/gemfile_52.gemfile
- env: DB=sqlite3
rvm: 2.6
rvm: 2.5
gemfile: gemfiles/gemfile_52.gemfile

services:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'

gemspec

gem "activerecord", '~> 5.2.4.5'
gem "activerecord", "~> 6.0.3"
gem "coveralls", require: false
gem "mysql2"
gem "pg"
Expand Down
6 changes: 5 additions & 1 deletion lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def parent_id
alias :parent_id? :ancestors?

def parent
unscoped_find(parent_id) if has_parent?
if has_parent?
unscoped_where do |scope|
scope.find_by id: parent_id
end
end
end

def parent_of?(node)
Expand Down
37 changes: 37 additions & 0 deletions test/concerns/relations_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative '../environment'

class RelationsTest < ActiveSupport::TestCase
def test_root_found
AncestryTestDatabase.with_model do |model|
parent = model.create
child = model.create!(:ancestor_ids => [parent.id])
assert_equal(parent, child.root)
end
end

def test_parent_not_found
AncestryTestDatabase.with_model do |model|
record = model.create
# setting the parent_id to something not valid
record.update_attribute(:ancestor_ids, [record.id + 1])
assert_nil record.root
end
end

def test_parent_found
AncestryTestDatabase.with_model do |model|
parent = model.create
child = model.create!(:ancestor_ids => [parent.id])
assert_equal(parent, child.parent)
end
end

def test_parent_not_found
AncestryTestDatabase.with_model do |model|
record = model.create
# setting the parent_id to something not valid
record.update_attribute(:ancestor_ids, [record.id + 1])
assert_nil record.parent
end
end
end

0 comments on commit 7f78f81

Please sign in to comment.