Merge pull request #319 from paulsponagl/nav_with_liquid_2.0

added a snippet option to the liquid nav tag
This commit is contained in:
Didier Lafforgue 2012-03-07 17:44:00 -08:00
commit effce313bf
6 changed files with 45 additions and 9 deletions

View File

@ -78,8 +78,8 @@ module Locomotive
#
# @return [ Array ] The children pages ordered by their position
#
def children_with_minimal_attributes
self.children.minimal_attributes
def children_with_minimal_attributes( attrs = [] )
self.children.minimal_attributes( attrs )
end
# Assigns the new position of each child of this node.

View File

@ -51,7 +51,7 @@ module Locomotive
scope :published, :where => { :published => true }
scope :fullpath, lambda { |fullpath| { :where => { :fullpath => fullpath } } }
scope :handle, lambda { |handle| { :where => { :handle => handle } } }
scope :minimal_attributes, :only => %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at)
scope :minimal_attributes, lambda { |attrs=[]| {:only => attrs + %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at) } }
## methods ##

View File

@ -41,6 +41,10 @@ module Locomotive
self._source.published?
end
def before_method(meth)
self._source.editable_elements.where(:slug => meth).try(:first).try(:content)
end
end
end
end

View File

@ -23,6 +23,16 @@ module Locomotive
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
@options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
@options[:add_attributes] = []
if @options[:snippet]
template = @options[:snippet].include?('{') ? @options[:snippet] : context[:site].snippets.where(:slug => @options[:snippet] ).try(:first).try(:template)
unless template.blank?
@options[:liquid_render] = ::Liquid::Template.parse( template )
@options[:add_attributes] = ['editable_elements']
end
end
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <page|site> <options>")
end
@ -60,12 +70,12 @@ module Locomotive
@site, @page = context.registers[:site], context.registers[:page]
children = (case @source
when 'site' then @site.pages.root.minimal_attributes.first # start from home page
when 'site' then @site.pages.root.minimal_attributes( @options[:add_attributes] ).first # start from home page
when 'parent' then @page.parent || @page
when 'page' then @page
else
@site.pages.fullpath(@source).minimal_attributes.first
end).children_with_minimal_attributes.to_a
@site.pages.fullpath(@source).minimal_attributes( @options[:add_attributes] ).first
end).children_with_minimal_attributes( @options[:add_attributes] ).to_a
children.delete_if { |p| !include_page?(p) }
end
@ -75,7 +85,10 @@ module Locomotive
selected = @page.fullpath =~ /^#{page.fullpath}/ ? " #{@options[:active_class]}" : ''
icon = @options[:icon] ? '<span></span>' : ''
label = %{#{icon if @options[:icon] != 'after' }#{page.title}#{icon if @options[:icon] == 'after' }}
title = @options[:liquid_render] ? @options[:liquid_render].render( 'page' => page ) : page.title
label = %{#{icon if @options[:icon] != 'after' }#{title}#{icon if @options[:icon] == 'after' }}
output = %{<li id="#{page.slug.dasherize}-link" class="link#{selected} #{css}">}
output << %{<a href="/#{@site.localized_page_fullpath(page)}">#{label}</a>}
@ -89,7 +102,7 @@ module Locomotive
def render_entry_children(page, depth)
output = %{}
children = page.children_with_minimal_attributes.reject { |c| !include_page?(c) }
children = page.children_with_minimal_attributes( @options[:add_attributes] ).reject { |c| !include_page?(c) }
if children.present?
output = %{<ul id="#{@options[:id]}-#{page.slug.dasherize}">}
children.each do |c, page|

View File

@ -81,6 +81,21 @@ describe Locomotive::Liquid::Drops::Page do
end
context '#rendering page with editable_elements' do
before(:each) do
@site = FactoryGirl.create(:site)
@home = @site.pages.root.first
@home.update_attributes :raw_template => "{% block body %}{% editable_short_text 'body' %}Lorem ipsum{% endeditable_short_text %}{% endblock %}"
@home.editable_elements.first.content = 'Lorem ipsum'
end
it 'renders the text of the editable field' do
render_template('{{ home.body }}').should == 'Lorem ipsum'
end
end
describe 'published?' do
subject { render_template('{{ home.published? }}') }
it { should == @home.published?.to_s }

View File

@ -96,6 +96,10 @@ describe Locomotive::Liquid::Tags::Nav do
render_nav('site', {}, 'icon: after').should match /<li id="child-1-link" class="link first"><a href="\/child_1">Child #1<span><\/span><\/a><\/li>/
end
it 'renders a snippet for the title' do
render_nav('site', {}, 'snippet: "-{{page.title}}-"').should match /<li id="child-1-link" class="link first"><a href="\/child_1">-Child #1-<\/a><\/li>/
end
it 'assigns a different dom id' do
render_nav('site', {}, 'id: "main-nav"').should match /<ul id="main-nav">/
end