pakyow-presenter/lib/presenter/view_composer.rb in pakyow-presenter-0.9.1 vs pakyow-presenter/lib/presenter/view_composer.rb in pakyow-presenter-0.10.0
- old
+ new
@@ -8,11 +8,12 @@
end
extend Forwardable
def_delegators :template, :title, :title=
- def_delegators :parts, :scope, :prop
+ def_delegators :parts, :prop, :component
+ def_delegators :view, :to_html
attr_reader :store, :path, :page, :partials
def initialize(store, path = nil, opts = {}, &block)
@store = store
@@ -70,11 +71,11 @@
end
def template=(template)
unless template.is_a?(Template)
# get template by name
- template = @store.template(template)
+ template = @store.template(template.to_sym)
end
@template = template
return self
@@ -108,21 +109,47 @@
container = @page.container(name)
return container
end
def parts
+ # create an array to hold the parts
parts = ViewCollection.new
+
+ # add the current template
parts << @template
- @page.each_container { |name, container| parts << container }
- # only include available partials as parts
- available_partials = parts.inject([]) { |sum, part| sum.concat(part.doc.partials.keys) }
- partials.select { |name, partial| available_partials.include?(name) }.each_pair { |name, partial| parts << partial }
-
+ # add each page container
+ @page.each_container do |_, container|
+ parts << container
+ end
+
+ parts.concat(partials_for_parts(parts))
+
return parts
end
+ def scope(name)
+ collection = parts.scope(name)
+
+ if collection.is_a?(ViewVersion)
+ collection = collection.versions.inject(ViewCollection.new) { |c, v| c << v; c }
+ end
+
+ # include partials so nested scopes/props can be bound to
+ collection.each do |view|
+ view.includes(partials)
+ end
+
+ #TODO make sure anytime we return a collection it tries to version
+ # make this a class level helper method on ViewVersion
+ if !collection.is_a?(ViewVersion) && collection.versioned?
+ ViewVersion.new(collection.views)
+ else
+ collection
+ end
+ end
+
private
def build_view
raise MissingTemplate, "No template provided to view composer" if @template.nil?
raise MissingPage, "No page provided to view composer" if @page.nil?
@@ -139,14 +166,31 @@
def remap_partials(partials)
Hash[partials.map { |name, partial_or_path|
if partial_or_path.is_a?(Partial)
partial = partial_or_path
else
- partial = Partial.load(@store.expand_partial_path(path))
+ partial = Partial.load(@store.expand_partial_path(partial_or_path))
end
[name, partial]
}]
+ end
+
+ def partials_for_parts(parts, acc = [])
+ # determine the partials to be included
+ available_partials = parts.inject([]) { |sum, part|
+ sum.concat(part.doc.partials.keys)
+ }
+
+ # add available partials as parts
+ partials.select { |name|
+ available_partials.include?(name)
+ }.each_pair { |_, partial|
+ acc << partial
+ partials_for_parts([partial], acc)
+ }
+
+ return acc
end
end
end
end