Module: Rooftop::Rails::NestedModel::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#find_by_nested_path(path, opts = {}) ⇒ Object

Parameters:

  • path (String)

    the path - the last of which will be the thing we find

  • opts (Hash) (defaults to: {})

    options - path delimiter and unescape



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/concerns/rooftop/rails/nested_model.rb', line 19

def find_by_nested_path(path, opts = {})
  options = {delimiter: '/', unescape: false}
  options.merge!(opts)
  path = CGI::unescape(path) if options[:unescape]
  delimiter = options[:delimiter]
  slug = "#{options[:prefix]}#{path}".gsub(/^\//, '').split(delimiter).last
  entities = where(slug: slug)
  entity = entities.to_a.find {|e| e.nested_path == path}
  if entity.nil?
    raise Rooftop::RecordNotFoundError, "Couldn't find #{self} with slug #{slug}"
  else
    return entity
  end
end