Skip to content

Commit

Permalink
rubocop spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Oct 23, 2024
1 parent b4f288a commit 333714c
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 91 deletions.
11 changes: 6 additions & 5 deletions ancestry.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

lib = File.expand_path('../lib/', __FILE__)
$:.unshift lib unless $:.include?(lib)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'ancestry/version'

Gem::Specification.new do |s|
Expand All @@ -24,6 +24,7 @@ EOF
"changelog_uri" => "https://github.com/stefankroes/ancestry/blob/master/CHANGELOG.md",
"source_code_uri" => "https://github.com/stefankroes/ancestry/",
"bug_tracker_uri" => "https://github.com/stefankroes/ancestry/issues",
"rubygems_mfa_required" => "true"
}
s.version = Ancestry::VERSION

Expand All @@ -32,19 +33,19 @@ EOF
s.homepage = 'https://github.com/stefankroes/ancestry'
s.license = 'MIT'

s.files = Dir[
s.files = Dir[
"{lib}/**/*",
'CHANGELOG.md',
'MIT-LICENSE',
'README.md'
]
s.require_paths = ["lib"]
s.required_ruby_version = '>= 2.5'

s.required_ruby_version = '>= 2.5'
s.add_runtime_dependency 'activerecord', '>= 5.2.6'
s.add_development_dependency 'appraisal'
s.add_development_dependency 'minitest'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'yard'
end
61 changes: 33 additions & 28 deletions test/concerns/arrangement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def root_node(model)
end

def middle_node(model)
root_node(model).children.sort_by(&:id).first
root_node(model).children.min_by(&:id)
end

def leaf_node(model)
Expand All @@ -25,7 +25,7 @@ def assert_tree(arranged_nodes, size_at_depth)
assert_equal size_at_depth[1], children.size
assert_equal node.children.sort_by(&:id), children.keys.sort_by(&:id)

assert_tree(children, size_at_depth[1..-1])
assert_tree(children, size_at_depth[1..])
end
end

Expand All @@ -41,7 +41,7 @@ def assert_tree_path(arranged_nodes, expected_ids)
arranged_nodes.each do |node, children|
assert_equal expected_ids[0], node.id

assert_tree_path(children, expected_ids[1..-1])
assert_tree_path(children, expected_ids[1..])
end
end

Expand Down Expand Up @@ -133,40 +133,45 @@ def test_arrange_serializable
AncestryTestDatabase.with_model :depth => 2, :width => 2 do |model, _roots|
col = model.ancestry_column
# materialized path 2 has a slash at the beginning and end
fmt = AncestryTestDatabase.materialized_path2? ? -> (a) { a ? "/#{a}/" : "/" } : -> (a) {a}
result = [{
col=>fmt[nil], "id"=>4, "children"=> [{
col=>fmt["4"], "id"=>6, "children" => []
}, {
col=>fmt["4"], "id"=>5, "children" => []
}]
}, {
col=>fmt[nil], "id"=>1, "children"=> [{
col=>fmt["1"], "id"=>3, "children"=>[]
fmt =
if AncestryTestDatabase.materialized_path2?
->(a) { a ? "/#{a}/" : "/" }
else
->(a) { a }
end
result = [
{
col => fmt[nil], "id" => 4, "children" => [
{col => fmt["4"], "id" => 6, "children" => []},
{col => fmt["4"], "id" => 5, "children" => []}
]
}, {
col=>fmt["1"], "id"=>2, "children"=>[]
}]
}]
col => fmt[nil], "id" => 1, "children" => [
{col => fmt["1"], "id" => 3, "children" => []},
{col => fmt["1"], "id" => 2, "children" => []}
]
}
]

assert_equal model.arrange_serializable(order: "id desc"), result
end
end

def test_arrange_serializable_with_block
AncestryTestDatabase.with_model :depth => 2, :width => 2 do |model, _roots|
expected_result = [{
"id"=>4, "children"=>[{
"id"=>6
}, {
"id"=>5
}]
}, {
"id"=>1, "children"=> [{
"id"=>3
expected_result = [
{
"id" => 4, "children" => [
{"id" => 6},
{"id" => 5}
]
}, {
"id"=>2
}]
}]
"id" => 1, "children" => [
{"id" => 3},
{"id" => 2}
]
}
]
result = model.arrange_serializable(order: "id desc") do |parent, children|
out = {}
out["id"] = parent.id
Expand Down
2 changes: 1 addition & 1 deletion test/concerns/has_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_setup_test_nodes
end

def test_primary_key_is_an_integer
AncestryTestDatabase.with_model(extra_columns: { string_id: :string }) do |model|
AncestryTestDatabase.with_model(extra_columns: {string_id: :string}) do |model|
model.primary_key = :string_id

assert !model.primary_key_is_an_integer?
Expand Down
17 changes: 8 additions & 9 deletions test/concerns/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ def test_has_ancestry_in_after_save
:orphan_strategy => :adopt,
:extra_columns => {:name => :string, :name_path => :string}
) do |model|

model.class_eval do
before_save :before_save_hook

Expand All @@ -35,9 +34,8 @@ def before_save_hook

def test_update_descendants_with_changed_parent_value
AncestryTestDatabase.with_model(
extra_columns: { name: :string, name_path: :string }
extra_columns: {name: :string, name_path: :string}
) do |model|

model.class_eval do
before_save :update_name_path
# this example will only work if the name field is unique across all levels
Expand Down Expand Up @@ -78,9 +76,9 @@ def update_descendants_hook(descendants_clause, old_ancestry, new_ancestry)
assert_equal("changed", m5.reload.name_path)
assert_equal([m5.id], m2.reload.ancestor_ids)
assert_equal("changed/child", m2.reload.name_path)
assert_equal([m5.id,m2.id], m3.reload.ancestor_ids)
assert_equal([m5.id, m2.id], m3.reload.ancestor_ids)
assert_equal("changed/child/grandchild", m3.reload.name_path)
assert_equal([m5.id,m2.id,m3.id], m4.reload.ancestor_ids)
assert_equal([m5.id, m2.id, m3.id], m4.reload.ancestor_ids)
assert_equal("changed/child/grandchild/grandchild's grand", m4.reload.name_path)
end
end
Expand Down Expand Up @@ -134,15 +132,16 @@ def before_hook
# see f94b22ba https://github.com/stefankroes/ancestry/pull/263
def test_node_creation_in_after_commit
AncestryTestDatabase.with_model do |model|
children=[]
children = []
model.instance_eval do
attr_accessor :idx
self.after_commit do
children << self.children.create!(:idx => self.idx - 1) if self.idx > 0

after_commit do
children << self.children.create!(:idx => idx - 1) if idx > 0
end
end
model.create!(:idx => 3)
assert_equal [1,2,3], children.first.ancestor_ids
assert_equal [1, 2, 3], children.first.ancestor_ids
end
end
end
6 changes: 3 additions & 3 deletions test/concerns/integrity_checking_and_restoration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ def test_integrity_checking
end
end

def assert_integrity_restoration model
def assert_integrity_restoration(model)
assert_raise Ancestry::AncestryIntegrityException do
model.check_ancestry_integrity!
end
model.restore_ancestry_integrity!
assert_nothing_raised do
model.check_ancestry_integrity!
end
assert model.all.any? {|node| node.has_parent? }, "Expected some nodes not to be roots"
assert_equal model.count, model.roots.collect {|node| node.descendants.count + 1 }.sum
assert model.all.any?(&:has_parent?), "Expected some nodes not to be roots"
assert_equal model.count, model.roots.collect { |node| node.descendants.count + 1 }.sum
end

def test_integrity_restoration
Expand Down
10 changes: 5 additions & 5 deletions test/concerns/materialized_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ def test_ancestry_column_values

# saved
node.save!
assert_ancestry node, nil, child: "#{node.id}"
assert_ancestry node, nil, child: node.id.to_s

# changed
node.ancestor_ids = [root.id]
assert_ancestry node, "#{root.id}", db: nil, child: "#{node.id}"
assert_ancestry node, root.id.to_s, db: nil, child: node.id.to_s

# changed saved
node.save!
assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}"
assert_ancestry node, root.id.to_s, child: "#{root.id}/#{node.id}"

# reloaded
node.reload
assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}"
assert_ancestry node, root.id.to_s, child: "#{root.id}/#{node.id}"

# fresh node
node = model.find(node.id)
assert_ancestry node, "#{root.id}", child: "#{root.id}/#{node.id}"
assert_ancestry node, root.id.to_s, child: "#{root.id}/#{node.id}"
end
end

Expand Down
34 changes: 17 additions & 17 deletions test/concerns/orphan_strategies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_setting_invalid_orphan_strategy
end

def test_orphan_rootify_strategy
AncestryTestDatabase.with_model orphan_strategy: :rootify, :depth => 3, :width => 3 do |model, roots|
AncestryTestDatabase.with_model orphan_strategy: :rootify, :depth => 3, :width => 3 do |_model, roots|
root = roots.first.first
children = root.children.to_a
root.destroy
Expand Down Expand Up @@ -50,7 +50,7 @@ def verify_parent_exists
end

def test_orphan_restrict_strategy
AncestryTestDatabase.with_model orphan_strategy: :restrict, :depth => 3, :width => 3 do |model, roots|
AncestryTestDatabase.with_model orphan_strategy: :restrict, :depth => 3, :width => 3 do |_model, roots|
root = roots.first.first
assert_raise Ancestry::AncestryException do
root.destroy
Expand All @@ -63,11 +63,11 @@ def test_orphan_restrict_strategy

def test_orphan_adopt_strategy
AncestryTestDatabase.with_model orphan_strategy: :adopt do |model|
n1 = model.create! #create a root node
n2 = model.create!(:parent => n1) #create child with parent=root
n3 = model.create!(:parent => n2) #create child with parent=n2, depth = 2
n4 = model.create!(:parent => n2) #create child with parent=n2, depth = 2
n5 = model.create!(:parent => n4) #create child with parent=n4, depth = 3
n1 = model.create! # create a root node
n2 = model.create!(:parent => n1) # create child with parent=root
n3 = model.create!(:parent => n2) # create child with parent=n2, depth = 2
n4 = model.create!(:parent => n2) # create child with parent=n2, depth = 2
n5 = model.create!(:parent => n4) # create child with parent=n4, depth = 3
n2.destroy # delete a node with desecendants
n3.reload
n5.reload
Expand All @@ -84,7 +84,7 @@ def test_orphan_adopt_strategy

# DEPRECATED - please see test_apply_orphan_strategy_none for pattern instead
def test_override_apply_orphan_strategy
AncestryTestDatabase.with_model orphan_strategy: :destroy do |model, roots|
AncestryTestDatabase.with_model orphan_strategy: :destroy do |model, _roots|
root = model.create!
child = model.create!(:parent => root)
model.class_eval do
Expand All @@ -101,7 +101,7 @@ def apply_orphan_strategy
end

def test_apply_orphan_strategy_none
AncestryTestDatabase.with_model orphan_strategy: :none do |model, roots|
AncestryTestDatabase.with_model orphan_strategy: :none do |model, _roots|
root = model.create!
child = model.create!(:parent => root)
model.class_eval do
Expand All @@ -127,7 +127,7 @@ def apply_orphan_strategy_abc
end
end

root = model.create!
root = model.create!
3.times { root.children.create! }
model.create! # a node that is not affected
assert_difference 'model.count', -4 do
Expand All @@ -147,7 +147,7 @@ def apply_orphan_strategy_abc

has_ancestry orphan_strategy: :abc, ancestry_column: AncestryTestDatabase.ancestry_column
end
root = model.create!
root = model.create!
3.times { root.children.create! }
model.create! # a node that is not affected
assert_difference 'model.count', -4 do
Expand All @@ -158,17 +158,17 @@ def apply_orphan_strategy_abc

def test_basic_delete
AncestryTestDatabase.with_model do |model|
n1 = model.create! #create a root node
n2 = model.create!(:parent => n1) #create child with parent=root
n1 = model.create! # create a root node
n2 = model.create!(:parent => n1) # create child with parent=root
n2.destroy!
model.find(n1.id) # parent should exist
model.find(n1.id) # parent should exist

n1 = model.create! #create a root node
n2 = model.create!(:parent => n1) #create child with parent=root
n1 = model.create! # create a root node
n2 = model.create!(:parent => n1) # create child with parent=root
n1.destroy!
assert_nil(model.find_by(:id => n2.id)) # child should not exist

n1 = model.create! #create a root node
n1 = model.create! # create a root node
n1.destroy!
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/concerns/scopes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_order_by_reverse
AncestryTestDatabase.with_model(:width => 1, :depth => 3) do |model, _roots|
child = model.last
assert child
assert_nothing_raised do #IrreversibleOrderError
assert_nothing_raised do # IrreversibleOrderError
assert child.ancestors.last
end
end
Expand Down Expand Up @@ -106,9 +106,9 @@ def test_scoping_in_callbacks
model.class_eval do
define_method :after_create_callback do
# We don't want to be in the #children scope here when creating the child
self.parent
parent
self.parent_id = record.id if record
self.root
root
end
end

Expand Down
Loading

0 comments on commit 333714c

Please sign in to comment.