Skip to content

Commit

Permalink
Extracted Node visibility check into Node.visible? method
Browse files Browse the repository at this point in the history
  • Loading branch information
damisul committed Aug 8, 2024
1 parent 722113a commit a1aef11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions app/services/generate_toc_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def add_child(child, seqno)
@children << [child, seqno] unless child.nil? || @children.any? { |ch, _seqno| ch == child }
end

# Checks if given Node should be displayed in TOC tree for given authority and role combination
def visible?(role, authority_id)
if item.involved_authorities.any? { |ia| ia.role == role && ia.authority_id == authority_id }
# authority is specified on collection level with given role
true
else
# or collection contains other items where given authority has given role (will be recursive check)
children_by_role(role, authority_id).present?
end
end

# Returns array of child elements (Manifestations or Nodes) where given author is invovled with given role
def children_by_role(role, authority_id)
@children_by_role ||= {}
Expand All @@ -27,15 +38,7 @@ def children_by_role(role, authority_id)
child.involved_authorities_by_role(role).any? { |a| a.id == authority_id }
else
# child is a node representing other collection
collection = child.item

if collection.involved_authorities.any? { |ia| ia.role == role && ia.authority_id == authority_id }
# authority is specified on collection level with given role
true
else
# or collection contains other items where given authority has given role (will be recursive check)
child.children_by_role(role, authority_id).present?
end
child.visible?(role, authority_id)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/authors/_toc_by_role.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
:ruby
top_level_by_role = top_nodes.select { |node| node.children_by_role(role, authority_id).present? }
top_level_by_role = top_nodes.select { |node| node.visible?(role, authority_id) }
.sort_by { |node| [node.item.uncollected? ? 1 : 0, node.item.created_at] }
%h3= title
%ol
Expand Down

0 comments on commit a1aef11

Please sign in to comment.