lib/spontaneous/model/page.rb in spontaneous-0.2.0.beta5 vs lib/spontaneous/model/page.rb in spontaneous-0.2.0.beta6

- old
+ new

@@ -6,10 +6,11 @@ require "spontaneous/model/page/locks" require "spontaneous/model/page/page_tree" require "spontaneous/model/page/paths" require "spontaneous/model/page/site_map" require "spontaneous/model/page/site_timestamps" +require "spontaneous/model/page/singleton" module Spontaneous::Model module Page extend Spontaneous::Concern @@ -20,10 +21,12 @@ include PageTree include Paths include SiteMap include SiteTimestamps include Locks + include Singleton + include Spontaneous::Model::Core::ContentHash::PageMethods included do many_to_one_content :parent, :key => :parent_id, :reciprocal => :unordered_children one_to_many_content :unordered_children, :key => :parent_id, :reciprocal => :parent one_to_many_content :content, :key => :page_id, :reciprocal => :page @@ -35,16 +38,26 @@ def page? true end def export(user = nil) - super(user).merge(:title_field => self.title_field.to_s) + super(user).merge(title_field: self.title_field_name.to_s) end - def title_field + def title_field_name :title end + + # Use this to cause instances of the class to render as another content object. + # Useful if you want say a section to render its first child rather than itself + # This method calls #to_page on the result of the block to ensure that the value + # returned is a page & not an inline object. + def renders(&block) + define_method(:renderable) do + instance_eval(&block).try(:to_page) + end + end end def page? true end @@ -58,12 +71,13 @@ else super end end - def children - @ordered_children ||= generate_ordered_children(unordered_children) + def children(reload = false) + @ordered_children = nil if reload + @ordered_children ||= generate_ordered_children(unordered_children(reload: reload)) end def generate_ordered_children(unordered_children) return unordered_children if self.boxes.empty? ordered_pages = unordered_children.sort { |a, b| a.page_order_string <=> b.page_order_string } @@ -104,35 +118,36 @@ def entry @entry ||= resolve_entry end def resolve_entry - owner.all_contents.find { |e| e.id == self.id } + owner.all_contents.wrap_page(self) end def page=(page) end def content_depth 0 end def page_title - if field = self.fields[title_field] + if field = self.fields[title_field_name] field.value end end - def title_field - self.class.title_field + def title_field_name + self.class.title_field_name end def shallow_export(user) hash = super(user).merge({ :path => path, :title => page_title, :slug => slug, + :private => in_private_tree?, :uid => uid }) hash.delete(:label) hash.delete(:name) hash @@ -148,8 +163,20 @@ # PagePieces are == to their target but we must enforce the reverse, # that Pages are == to a PagePiece that encloses them def eql?(obj) super || (Spontaneous::PagePiece === obj && obj.target.eql?(self)) + end + + # Ensure that the *_inline versions of these methods render the entry + # (i.e. a PagePiece) not the page instance itself. This avoids a + # 'page within page' moment when you try to load a page and then + # render it directly within a template. + def render_inline(format = :html, params = {}, parent_context = nil) + entry.render_inline(format, params, parent_context) + end + + def render_inline_using(renderer, format = :html, params = {}, parent_context = nil) + entry.render_inline_using(renderer, format, params, parent_context) end end end