Class: Collavre::Creatives::BreadcrumbResolver
- Inherits:
-
Object
- Object
- Collavre::Creatives::BreadcrumbResolver
- Defined in:
- app/services/collavre/creatives/breadcrumb_resolver.rb
Overview
Resolves the ancestor path (breadcrumb) for a set of creatives in a small, fixed number of queries — used to annotate flat search results in the creative picker popup with “where it lives” context.
Returns a hash: { creative_id (Integer) => [{ id:, description:, restricted: }, …] } where the array is ordered from root-most ancestor down to the immediate parent (self is excluded). Ancestors the user cannot read — or that are archived while archived rows aren’t being shown — are kept in the path (so depth is preserved) but their text is masked with ‘restricted: true` and a nil description, mirroring how the full-page tree drops inaccessible ancestors after permission filtering. Archived ancestors are masked because the picker browse endpoints hide archived rows unless show_archived, so a jump to such a crumb would expand a node that never renders.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(creative_ids, user: nil, include_archived: false) ⇒ BreadcrumbResolver
constructor
A new instance of BreadcrumbResolver.
Constructor Details
#initialize(creative_ids, user: nil, include_archived: false) ⇒ BreadcrumbResolver
Returns a new instance of BreadcrumbResolver.
17 18 19 20 21 |
# File 'app/services/collavre/creatives/breadcrumb_resolver.rb', line 17 def initialize(creative_ids, user: nil, include_archived: false) @ids = Array(creative_ids).map { |id| id.to_s.to_i }.uniq.reject(&:zero?) @user = user @include_archived = include_archived end |
Instance Method Details
#call ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/services/collavre/creatives/breadcrumb_resolver.rb', line 23 def call return {} if @ids.empty? # One query: every (descendant, ancestor, distance) pair, excluding self. rows = CreativeHierarchy .where(descendant_id: @ids) .where("generations > 0") .pluck(:descendant_id, :ancestor_id, :generations) return {} if rows.empty? ancestor_ids = rows.map { |_d, a, _g| a }.uniq accessible = accessible_ancestor_ids(ancestor_ids) records = ancestor_records(accessible) renderable = renderable_ancestor_ids(accessible, records) grouped = Hash.new { |h, k| h[k] = [] } rows.each { |descendant_id, ancestor_id, generations| grouped[descendant_id] << [ generations, ancestor_id ] } grouped.transform_values do |pairs| # Larger generation distance = closer to the root, so order descending. pairs.sort_by { |generations, _ancestor_id| -generations }.map do |_generations, ancestor_id| if renderable.include?(ancestor_id) { id: ancestor_id, description: ancestor_label(records[ancestor_id]) } else { id: ancestor_id, description: nil, restricted: true } end end end end |