From 074326095dc9ad044cb0951342c3d10cd8faba9b Mon Sep 17 00:00:00 2001 From: dinedine Date: Mon, 21 Jun 2010 17:46:17 +0200 Subject: [PATCH] access children of a page in liquid templates (useful for building nav widgets) --- app/models/page.rb | 6 ++- doc/TODO | 4 ++ lib/locomotive/liquid/drops/base.rb | 2 + lib/locomotive/liquid/drops/content.rb | 4 +- lib/locomotive/liquid/drops/page.rb | 15 ++++++ spec/lib/locomotive/liquid/drops/page_spec.rb | 48 +++++++++++++++++++ 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 lib/locomotive/liquid/drops/page.rb create mode 100644 spec/lib/locomotive/liquid/drops/page_spec.rb diff --git a/app/models/page.rb b/app/models/page.rb index 4fb98146..3d7f9df4 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -35,7 +35,7 @@ class Page named_scope :not_found, :where => { :slug => '404', :depth => 0, :published => true } ## behaviours ## - liquid_methods :title, :fullpath + # liquid_methods :title, :fullpath liquify_template :joined_parts ## methods ## @@ -65,6 +65,10 @@ class Page def url "http://#{self.site.domains.first}/#{self.fullpath}.html" end + + def to_liquid(options = {}) + Locomotive::Liquid::Drops::Page.new(self) + end protected diff --git a/doc/TODO b/doc/TODO index 38fc38ba..52bd3742 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,5 +1,6 @@ BOARD: +- access page children in liquid - new custom field types - boolean @@ -16,6 +17,7 @@ BACKLOG: - new custom field types: - file - date + - belongs_to => association - refactoring admin crud (pages + layouts + snippets) - refactor slugify method (use parameterize + create a module) @@ -23,6 +25,8 @@ BACKLOG: - tiny mce or similar for custom field text type. +- sitemap + BUGS: - custom fields: accepts_nested_attributes weird behaviour when creating new content type + adding random fields diff --git a/lib/locomotive/liquid/drops/base.rb b/lib/locomotive/liquid/drops/base.rb index 7a6b6382..b1cc82e7 100644 --- a/lib/locomotive/liquid/drops/base.rb +++ b/lib/locomotive/liquid/drops/base.rb @@ -4,6 +4,8 @@ module Locomotive module Drops class Base < ::Liquid::Drop + @@forbidden_attributes = %w{_id _version _index} + class_inheritable_reader :liquid_attributes write_inheritable_attribute :liquid_attributes, [] attr_reader :source diff --git a/lib/locomotive/liquid/drops/content.rb b/lib/locomotive/liquid/drops/content.rb index 8b219e9e..24e62ef3 100644 --- a/lib/locomotive/liquid/drops/content.rb +++ b/lib/locomotive/liquid/drops/content.rb @@ -2,9 +2,7 @@ module Locomotive module Liquid module Drops class Content < Base - - @@forbidden_attributes = %w{_id _version _index} - + def before_method(meth) return '' if @source.nil? diff --git a/lib/locomotive/liquid/drops/page.rb b/lib/locomotive/liquid/drops/page.rb new file mode 100644 index 00000000..f46ce464 --- /dev/null +++ b/lib/locomotive/liquid/drops/page.rb @@ -0,0 +1,15 @@ +module Locomotive + module Liquid + module Drops + class Page < Base + + liquid_attributes << :title << :fullpath + + def children + @children ||= liquify(*@source.children) + end + + end + end + end +end \ No newline at end of file diff --git a/spec/lib/locomotive/liquid/drops/page_spec.rb b/spec/lib/locomotive/liquid/drops/page_spec.rb new file mode 100644 index 00000000..23fc6a43 --- /dev/null +++ b/spec/lib/locomotive/liquid/drops/page_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe Locomotive::Liquid::Drops::Page do + + before(:each) do + @home = Factory.build(:page) + @home.stubs(:children).returns([ + Page.new(:title => 'Child #1'), + Page.new(:title => 'Child #2'), + Page.new(:title => 'Child #3') + ]) + @home.children.last.stubs(:children).returns([ + Page.new(:title => 'Child #3.1'), + Page.new(:title => 'Child #3.2') + ]) + end + + context '#rendering' do + + context '#children' do + + it 'renders title of all children pages' do + content = render_template '{% for child in home.children %}{{ child.title }},{% endfor %}' + content.should == 'Child #1,Child #2,Child #3,' + end + + end + + context '#sub children' do + + it 'renders title of all sub children pages' do + content = render_template '{% for child in home.children.last.children %}{{ child.title }},{% endfor %}' + content.should == 'Child #3.1,Child #3.2,' + end + + end + + end + + def render_template(template = '', assigns = {}) + assigns = { + 'home' => @home + }.merge(assigns) + + Liquid::Template.parse(template).render assigns + end + +end \ No newline at end of file