Skip to content

Commit

Permalink
Remove self and decendants from the parent feed selector, closing #89.
Browse files Browse the repository at this point in the history
The self link is the only one that is really problematic, but intuitively we don't want users to hang the tree from the tree without doing some thinking beforehand.
  • Loading branch information
bamnet committed May 17, 2012
1 parent 54c0acf commit 2134a23
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
10 changes: 7 additions & 3 deletions app/helpers/feeds_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module FeedsHelper

# Generate a depth first ordered tree.
def dfs_tree(roots = Feed.roots)
def dfs_tree(roots = Feed.roots, break_at = nil)
nodes = []
roots.each do |node|
nodes += [node]
nodes += node.descendants unless node.descendants.empty?
nodes += [node] unless (node == break_at)
nodes += node.descendants unless (node.descendants.empty? || node == break_at)
end
if !break_at.nil?
rejectable = break_at.descendants
nodes = nodes.reject{|n| rejectable.include?(n) || n == break_at}
end
nodes
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/feeds/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
</div>
</div>
<%
feeds = dfs_tree(Feed.roots).collect{|c| [raw("&nbsp;&nbsp;")* c.depth + c.name, c.id ]}
unless feeds.empty?
parent_feeds = dfs_tree(Feed.roots, @feed)
unless parent_feeds.empty?
%>
<div class="clearfix">
<%= f.label t(:parent) %>
<div class="input">
<%= f.select :parent_id, dfs_tree(Feed.roots).collect{|c| [raw("&nbsp;&nbsp;")* c.depth + c.name, c.id ]}, {:include_blank => true }, :class => "span6" %>
<%= f.select :parent_id, parent_feeds.collect{|c| [raw("&nbsp;&nbsp;")* c.depth + c.name, c.id ]}, {:include_blank => true }, :class => "span6" %>
</div>
</div>
<% end %>
Expand Down
25 changes: 25 additions & 0 deletions test/unit/helpers/feeds_helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
require 'test_helper'

class FeedsHelperTest < ActionView::TestCase

test 'default dfs tree' do
tree = dfs_tree()
assert_equal tree.length, Feed.all.length

i = tree.index(feeds(:announcements))
j = tree.index(feeds(:boring_announcements))
assert_equal tree[j+1], feeds(:sleepy_announcements)

if j-i == 1
offset = j+2
else
offset = i+1
end
assert_equal tree[offset], feeds(:important_announcements)
end

test 'excluding dfs tree' do
feed = feeds(:boring_announcements)
tree = dfs_tree(Feed.roots, feed)
assert_equal tree.length, Feed.all.length-2

assert !tree.include?(feeds(:boring_announcements))
assert !tree.include?(feeds(:sleepy_announcements))
end
end

0 comments on commit 2134a23

Please sign in to comment.