Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add parent query methods #97

Closed
wants to merge 1 commit into from

2 participants

@sj26

While adding ancestry to some STI classes I found myself wanting to add a validation to restrict non-leaf nodes:

class Leaf < ActiveRecord::Base
  has_ancestry

  validate :parent_is_leaf_group, if: :parent?

  def parent_is_leaf_group
    errors.add :parent, "is not leaf group" unless parent.is_a? LeafGroup
  end
end

class LeafGroup < Leaf
  # Things Leaf can't have...
end

Sadly, my friend the query attribute method (parent?) wasn't available—so I added it. It adds greater semantic value than if: :parent_id in situations like this, while being more efficient than if: :parent.

Tests were added also, but one test on master was already failing.

@xymbol

@sj26 I would probably rename the method to avoid confusion between having and being a parent node.

@sj26

It's meant to reflect the attribute query methods which already exist for most attributes and associations on activemodels, but I'm open to has_parent? and is_parent? or something.

@StefanH StefanH referenced this pull request from a commit
Stefan Henzen Add attribute query method for parent id
Based on #97 by sj26
closes #97
8650a52
@StefanH StefanH closed this pull request from a commit
Stefan Henzen Add attribute query method for parent id
Based on #97 by sj26
closes #97
8650a52
@StefanH StefanH closed this in 8650a52
@sj26

Very nice, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2012
  1. @sj26

    Add parent query methods

    sj26 authored
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 0 deletions.
  1. +4 −0 lib/ancestry/instance_methods.rb
  2. +3 −0  test/has_ancestry_test.rb
View
4 lib/ancestry/instance_methods.rb
@@ -122,6 +122,10 @@ def parent
if parent_id.blank? then nil else unscoped_find(parent_id) end
end
+ def parent?
+ parent_id.present?
+ end
+
# Root
def root_id
if ancestor_ids.empty? then id else ancestor_ids.first end
View
3  test/has_ancestry_test.rb
@@ -107,6 +107,7 @@ def test_tree_navigation
# Parent assertions
assert_equal nil, lvl0_node.parent_id
assert_equal nil, lvl0_node.parent
+ assert !lvl0_node.parent?
# Root assertions
assert_equal lvl0_node.id, lvl0_node.root_id
assert_equal lvl0_node, lvl0_node.root
@@ -139,6 +140,7 @@ def test_tree_navigation
# Parent assertions
assert_equal lvl0_node.id, lvl1_node.parent_id
assert_equal lvl0_node, lvl1_node.parent
+ assert lvl1_node.parent?
# Root assertions
assert_equal lvl0_node.id, lvl1_node.root_id
assert_equal lvl0_node, lvl1_node.root
@@ -171,6 +173,7 @@ def test_tree_navigation
# Parent assertions
assert_equal lvl1_node.id, lvl2_node.parent_id
assert_equal lvl1_node, lvl2_node.parent
+ assert lvl2_node.parent?
# Root assertions
assert_equal lvl0_node.id, lvl2_node.root_id
assert_equal lvl0_node, lvl2_node.root
Something went wrong with that request. Please try again.