Last active
December 23, 2015 14:09
-
-
Save simeonwillbanks/6646644 to your computer and use it in GitHub Desktop.
Presenter < SimpleDelegator #ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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