Skip to content

Instantly share code, notes, and snippets.

@simeonwillbanks
Last active December 23, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simeonwillbanks/6646644 to your computer and use it in GitHub Desktop.
Save simeonwillbanks/6646644 to your computer and use it in GitHub Desktop.
Presenter < SimpleDelegator #ruby
class NodesPresenter < SimpleDelegator
def initialize(id_or_object)
@node = id_or_object.is_a?(Integer) ? NodeDecorator.find(id_or_object) : id_or_object.decorate
super @node
end
def ancestor(key)
NodesPresenter.new(@node.ancestor(key))
end
def ancestors
@ancestors ||= @node.ancestors.collect {|n| NodesPresenter.new(n)}
end
def breadcrumbs
ancestors.select { |a| a.slug.present? }
end
def parent
@parent ||= NodesPresenter.new(@node.parent) if @node.parent.present?
end
def children
@children ||= @node.children.collect {|n| NodesPresenter.new(n)}
end
end
node = NodesPresenter.new(Node.first)
# has_children? delegated to Node model
puts node.children if node.has_children?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment