Module: Rooftop::Rails::NestedModel

Defined in:
app/models/concerns/rooftop/rails/nested_model.rb

Overview

This module adds methods for finding by a nested path of slugs, and generating a nested path of slugs

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'app/models/concerns/rooftop/rails/nested_model.rb', line 8

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#nested_path(opts = {}) ⇒ String

Given a field (and optional delimiter), return a path to the current object. e.g. you’d end up with /path/to/page (where this object is ‘page’)

Parameters:

  • the (Symbol)

    field to use to create the path

  • the (String)

    delimiter to use. Defaults to “/”

Returns:

  • (String)

    the path as a string



40
41
42
43
44
45
46
47
# File 'app/models/concerns/rooftop/rails/nested_model.rb', line 40

def nested_path(opts = {})
  options = {delimiter: "/", prefix: ""}
  options.merge!(opts)
  delimiter = options[:delimiter]
  prefix = options[:prefix].empty? ? "" : "#{options[:prefix]}#{delimiter}"
  path = ([slug] + ancestors.collect(&:slug)).reverse.join(delimiter).gsub(prefix,"")
  return path
end