Skip to content

Commit

Permalink
added parent validation for #89
Browse files Browse the repository at this point in the history
  • Loading branch information
zr2d2 committed May 14, 2012
1 parent 1536755 commit 54c0acf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
23 changes: 15 additions & 8 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ class Feed < ActiveRecord::Base
#Validations
validates :name, :presence => true, :uniqueness => true
validates :group, :presence => true, :associated => true

validate :parent_id_cannot_be_this_feed

def parent_id_cannot_be_this_feed
if !parent_id.blank? and parent_id == id
errors.add(:parent_id, "can't be this feed")
end
end

#Feed Hierachy
belongs_to :parent, :class_name => "Feed"
has_many :children, :class_name => "Feed", :foreign_key => "parent_id"
scope :roots, where(:parent_id => nil)

# Test if this feed is a root feed or not
def is_root?
parent_id.nil?
end

# Collect a list of parent feeds.
# Each feed the monkey stops as he climbs
# up the tree.
Expand All @@ -31,8 +38,8 @@ def ancestors
node, nodes = self, []
nodes << node = node.parent while node.parent
nodes
end
end

# Collect recursive list of child feeds.
# Every feed the monkey could stop by as he
# climbs down a tree.
Expand All @@ -47,13 +54,13 @@ def descendants
} unless node.children.empty?
nodes
end

# Figure out how deep in the tree
# the current feed is. 0 = root
def depth
ancestors.count
end

# The group of feeds who share a common parent.
def self_and_siblings
parent ? parent.children : Feed.roots
Expand All @@ -66,5 +73,5 @@ def self.subscribable(screen, field)
subscriptions = Subscription.where(:screen_id => screen, :field_id => field)
current_feeds = subscriptions.collect{ |s| s.feed }
Feed.all - current_feeds
end
end
end
48 changes: 28 additions & 20 deletions test/unit/feed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,91 @@ def setup
assert feed.errors[:name].any?
assert feed.errors[:group].any?
end

# The feed name must be unique
test "feed is now valid without a unique name" do
feed = Feed.new(:name => feeds(:service).name,
:description => "Another feed.",
:group => groups(:rpitv))

assert feed.invalid?
assert feed.errors[:name].any?
end


# Feed Hierachy Tests

# Verify the root scope returns all root feeds.
test "feed root scope" do
roots = Feed.roots
roots.each do |feed|
assert feed.is_root?
end
end

# A child feed should have a parent
test "feed parent relationship" do
assert_nil feeds(:announcements).parent
assert_equal feeds(:boring_announcements).parent, feeds(:announcements)
end

# A feed should have children
test "feed child relationship" do
assert feeds(:service).children.empty?

assert feeds(:announcements).children.include?(feeds(:boring_announcements))
assert feeds(:announcements).children.include?(feeds(:important_announcements))

assert_equal feeds(:boring_announcements).children, [feeds(:sleepy_announcements)]
end

# A root feed is_root?
test "feed is_root?" do
assert feeds(:service).is_root?
assert !feeds(:boring_announcements).is_root?
end


# A feed cannot be it's own parent
test "that a feed cannot be it's own parent" do
feed = feeds(:boring_announcements)
feed.update_attribute(:parent_id, feed.id)
assert feed.invalid?
assert feed.errors[:parent_id].any?
end

# The ancestor tree is built and in order
test "feed ancestors" do
assert feeds(:service).ancestors.empty?

assert feeds(:announcements).ancestors.empty?

assert_equal feeds(:boring_announcements).ancestors, [feeds(:announcements)]
assert_equal feeds(:sleepy_announcements).ancestors, [feeds(:boring_announcements), feeds(:announcements)]
end

# Descendants are built and in order
test "feed descendants" do
assert feeds(:service).descendants.empty?

assert_equal feeds(:announcements).descendants.size, 3
assert feeds(:announcements).descendants.include?(feeds(:boring_announcements))
assert feeds(:announcements).descendants.include?(feeds(:sleepy_announcements))
assert feeds(:announcements).descendants.include?(feeds(:important_announcements))

feed_list = [feeds(:boring_announcements), feeds(:sleepy_announcements), feeds(:important_announcements)]
assert_equal feeds(:announcements).descendants, feed_list

assert_equal feeds(:boring_announcements).descendants, [feeds(:sleepy_announcements)]
end

# Test feed depth
test "feed depth" do
assert_equal feeds(:service).depth, 0
assert_equal feeds(:announcements).depth, 0

assert_equal feeds(:sleepy_announcements).depth, 2
end

# Self and siblings works for children and roots
test "self and siblings" do
roots = Feed.roots
Expand All @@ -98,11 +106,11 @@ def setup
assert root.self_and_siblings.include?(sibling)
end
end

assert_equal feeds(:boring_announcements).self_and_siblings.size, 2
assert feeds(:boring_announcements).self_and_siblings.include?(feeds(:boring_announcements))
assert feeds(:boring_announcements).self_and_siblings.include?(feeds(:important_announcements))

assert_equal feeds(:sleepy_announcements).self_and_siblings.size, 1
assert feeds(:sleepy_announcements).self_and_siblings.include?(feeds(:sleepy_announcements))
end
Expand Down

0 comments on commit 54c0acf

Please sign in to comment.