Skip to content

Commit

Permalink
Show "node also part of ways" as nested lists on way pages
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Dec 6, 2024
1 parent fe81ac3 commit 6aab004
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 9 deletions.
23 changes: 17 additions & 6 deletions app/views/browse/_way.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@
<ul class="list-unstyled">
<% way.way_nodes.each do |wn| %>
<li>
<%= element_single_current_link "node", wn.node %>
<% related_ways = wn.node.ways.reject { |w| w.id == wn.way_id } %>
<% if related_ways.size > 0 then %>
(<%= t ".also_part_of_html",
:count => related_ways.size,
:related_ways => to_sentence(related_ways.map { |w| element_single_current_link "way", w }) %>)
<% related_ways = wn.node.ways.uniq.sort.reject { |w| w.id == wn.way_id } %>
<% if related_ways.size.zero? %>
<%= element_single_current_link "node", wn.node %>
<% else %>
<div>
<%= element_single_current_link "node", wn.node %>
</div>
<div class="ms-4">
<%= t ".also_part_of_ways", :count => related_ways.size %>
<ul class="list-unstyled">
<% related_ways.each do |related_way| %>
<li>
<%= element_single_current_link "way", related_way %>
</li>
<% end %>
</ul>
</div>
<% end %>
</li>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ en:
nodes_count:
one: "%{count} node"
other: "%{count} nodes"
also_part_of_html:
one: "part of way %{related_ways}"
other: "part of ways %{related_ways}"
also_part_of_ways:
one: "also part of way:"
other: "also part of ways:"
relation:
title_html: "Relation: %{name}"
history_title_html: "Relation History: %{name}"
Expand Down
124 changes: 124 additions & 0 deletions test/controllers/ways_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,128 @@ def test_show_timeout
assert_dom "h2", "Timeout Error"
assert_dom "p", /#{Regexp.quote("the way with the id #{way.id}")}/
end

def test_show_nodes
way = create(:way_with_nodes, :nodes_count => 2)

get way_path(way, :xhr => true)
assert_response :success
assert_dom "ul:has(> li a[href='#{node_path way.nodes[0]}']):has(> li a[href='#{node_path way.nodes[0]}'])", :count => 1 do
assert_dom "> li", :count => 2 do |list_items|
assert_dom list_items[0], "a[href='#{node_path way.nodes[0]}']"
assert_dom list_items[1], "a[href='#{node_path way.nodes[1]}']"
end
end
end

##
# shows another way that shares a node
#
# (b)--1--(a)--2--(c)
#
def test_show_nodes_also_part_of_way
way1 = create(:way)
way2 = create(:way)
node_a = create(:node)
node_b = create(:node)
node_c = create(:node)
create(:way_node, :way => way1, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way1, :node => node_b, :sequence_id => 2)
create(:way_node, :way => way2, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way2, :node => node_c, :sequence_id => 2)

get way_path(way1, :xhr => true)
assert_response :success
assert_dom "ul:has(> li a[href='#{node_path node_a}']):has(> li a[href='#{node_path node_b}'])", :count => 1 do
assert_dom "> li", :count => 2 do |list_items|
assert_dom list_items[0], "a[href='#{node_path node_a}']"
assert_dom list_items[1], "a[href='#{node_path node_b}']"
assert_dom list_items[1], "ul", :count => 0
assert_dom list_items[0], "ul", :count => 1 do
assert_dom "> li", :count => 1 do |sub_list_items|
assert_dom sub_list_items[0], "a[href='#{way_path way2}']"
end
end
end
end
end

##
# node shared with a looped way shouldn't cause that way appear twice in the "part of ways" sublist
#
# (b)--1--(a)--2--(c)
# \ /
# \ /
# (d)
#
def test_show_nodes_also_part_of_looped_way
way1 = create(:way)
way2 = create(:way)
node_a = create(:node)
node_b = create(:node)
node_c = create(:node)
node_d = create(:node)
create(:way_node, :way => way1, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way1, :node => node_b, :sequence_id => 2)
create(:way_node, :way => way2, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way2, :node => node_c, :sequence_id => 2)
create(:way_node, :way => way2, :node => node_d, :sequence_id => 3)
create(:way_node, :way => way2, :node => node_a, :sequence_id => 4)

get way_path(way1, :xhr => true)
assert_response :success
assert_dom "ul:has(> li a[href='#{node_path node_a}']):has(> li a[href='#{node_path node_b}'])", :count => 1 do
assert_dom "> li", :count => 2 do |list_items|
assert_dom list_items[0], "a[href='#{node_path node_a}']"
assert_dom list_items[1], "a[href='#{node_path node_b}']"
assert_dom list_items[1], "ul", :count => 0
assert_dom list_items[0], "ul", :count => 1 do
assert_dom "> li", :count => 1 do |sub_list_items|
assert_dom sub_list_items[0], "a[href='#{way_path way2}']"
end
end
end
end
end

##
# shows two ways that share a node
#
# (b)--1--(a)--2--(c)
# \
# 3
# \
# (d)
#
def test_show_nodes_also_part_of_2_ways
way1 = create(:way)
way2 = create(:way)
way3 = create(:way)
node_a = create(:node)
node_b = create(:node)
node_c = create(:node)
node_d = create(:node)
create(:way_node, :way => way1, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way1, :node => node_b, :sequence_id => 2)
create(:way_node, :way => way2, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way2, :node => node_c, :sequence_id => 2)
create(:way_node, :way => way3, :node => node_a, :sequence_id => 1)
create(:way_node, :way => way3, :node => node_d, :sequence_id => 2)

get way_path(way1, :xhr => true)
assert_response :success
assert_dom "ul:has(> li a[href='#{node_path node_a}']):has(> li a[href='#{node_path node_b}'])", :count => 1 do
assert_dom "> li", :count => 2 do |list_items|
assert_dom list_items[0], "a[href='#{node_path node_a}']"
assert_dom list_items[1], "a[href='#{node_path node_b}']"
assert_dom list_items[1], "ul", :count => 0
assert_dom list_items[0], "ul", :count => 1 do
assert_dom "> li", :count => 2 do |sub_list_items|
assert_dom sub_list_items[0], "a[href='#{way_path way2}']"
assert_dom sub_list_items[1], "a[href='#{way_path way3}']"
end
end
end
end
end
end

0 comments on commit 6aab004

Please sign in to comment.