Module: Slices::Tree::ClassMethods

Defined in:
lib/slices/tree.rb

Instance Method Summary collapse

Instance Method Details

#find_by_path(path) ⇒ Page Also known as: f

Finds a page by it’s path, a Page::NotFound is raised if the page can’t be found.

Returns:



68
69
70
# File 'lib/slices/tree.rb', line 68

def find_by_path(path)
  first(conditions: { path: path }) || (raise Page::NotFound.new(path))
end

#homePage

Get the home page.

Returns:



59
60
61
# File 'lib/slices/tree.rb', line 59

def home
  find_by_path('/')
end

#path_from_attributes(attributes, parent = nil) ⇒ String

Generate a path from attributes.

Parameters:

  • attributes (Hash)
  • parent (Page) (defaults to: nil)

Options Hash (attributes):

  • :parent_path (String)

    Path to parent page

  • :parent (Page)

    Parent page

  • :name (String)

    Page name

  • :permalink (String)

    Permalink

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
# File 'lib/slices/tree.rb', line 83

def path_from_attributes(attributes, parent = nil)
  parent_path = if parent
                  parent.path
                else
                  attributes[:parent_path] || attributes[:parent].try(:path).to_s
                end
  permalink = attributes.delete(:permalink) || attributes[:name]
  File.join(parent_path, permalink.to_url)
end