access children of a page in liquid templates (useful for building nav widgets)

This commit is contained in:
dinedine 2010-06-21 17:46:17 +02:00
parent b7e1cd0926
commit 074326095d
6 changed files with 75 additions and 4 deletions

View File

@ -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 ##
@ -66,6 +66,10 @@ class Page
"http://#{self.site.domains.first}/#{self.fullpath}.html"
end
def to_liquid(options = {})
Locomotive::Liquid::Drops::Page.new(self)
end
protected
def do_not_remove_index_and_404_pages

View File

@ -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

View File

@ -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

View File

@ -3,8 +3,6 @@ module Locomotive
module Drops
class Content < Base
@@forbidden_attributes = %w{_id _version _index}
def before_method(meth)
return '' if @source.nil?

View File

@ -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

View File

@ -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