FriendlyID with overridable tree-path slugs

48 Views Asked by At

I'm currently using Ancestry to manage a model structured as a tree, though the exact implementation of the tree doesn't really matter.

Given a model with the following properties:

class Item < ActiveRecord::Base
  extend FriendlyId

  attr_accessor :name :slug, :url_path 

  has_ancestry
  acts_as_list scope: :ancestry

  friendly_id METHOD, use: %i[slugged finders]
end

Where slug is either user-specified, and url_path is the full friendly_id of the Item, which is a combination of the slugs for all ancestral items joined with /

e.g. /foo/bar/baz would point to an item with a slug of baz which has ancestors with the slugs foo and bar

I've tried the following:

class Item < ApplicationRecord
    extend FriendlyId

    friendly_id :path, use: %i[slugged finders]

    acts_as_list scope: :ancestry, top_of_list: 0

    has_ancestry

    def path
        :slug if ancestry.nil?
        (ancestors.collect(&:slug) << slug).join('/')
    end
end

However, when given the same foo => [bar => baz] items, the result is foo-bar-baz rather than foo/bar/baz -- How do I get my URL path to match the tree location with / separators?

0

There are 0 best solutions below