use absolute urls for the pagination links + allow developers to access the http params within a liquid template + the with_scope handles category fields

This commit is contained in:
did 2011-04-29 01:16:40 +02:00
parent 5654924e0b
commit ea050afd5e
5 changed files with 28 additions and 10 deletions

View File

@ -82,7 +82,14 @@ class ContentType
# convert alias (key) to name
field = self.content_custom_fields.detect { |f| f._alias == key }
conditions_with_names[field._name.to_sym] = value
case field.kind.to_sym
when :category
if (category_item = field.category_items.where(:name => value).first).present?
conditions_with_names[field._name.to_sym] = category_item._id
end
else
conditions_with_names[field._name.to_sym] = value
end
end
self.contents.where(conditions_with_names)

View File

@ -82,13 +82,13 @@ module Locomotive
previous_link = (if paginate['previous'].blank?
"<span class=\"disabled prev_page\">#{previous_label}</span>"
else
"<a href=\"/#{paginate['previous']['url']}\" class=\"prev_page\">#{previous_label}</a>"
"<a href=\"#{absolute_url(paginate['previous']['url'])}\" class=\"prev_page\">#{previous_label}</a>"
end)
links = ""
paginate['parts'].each do |part|
links << (if part['is_link']
"<a href=\"/#{part['url']}\">#{part['title']}</a>"
"<a href=\"#{absolute_url(part['url'])}\">#{part['title']}</a>"
elsif part['hellip_break']
"<span class=\"gap\">#{part['title']}</span>"
else
@ -99,7 +99,7 @@ module Locomotive
next_link = (if paginate['next'].blank?
"<span class=\"disabled next_page\">#{next_label}</span>"
else
"<a href=\"/#{paginate['next']['url']}\" class=\"next_page\">#{next_label}</a>"
"<a href=\"#{absolute_url(paginate['next']['url'])}\" class=\"next_page\">#{next_label}</a>"
end)
%{<div class="pagination #{options[:css]}">
@ -140,6 +140,10 @@ module Locomotive
ThemeAssetUploader.url_for(@context.registers[:site], path)
end
def absolute_url(url)
url.starts_with('/') ? url : "/#{url}"
end
end
::Liquid::Template.register_filter(Html)

View File

@ -13,21 +13,21 @@ module Locomotive
def render(context)
context.stack do
context['with_scope'] = decode(@attributes)
context['with_scope'] = decode(@attributes, context)
render_all(@nodelist, context)
end
end
private
def decode(attributes)
def decode(attributes, context)
attributes.each_pair do |key, value|
attributes[key] = (case value
when /true|false/ then value == 'true'
when /[0-9]+/ then value.to_i
when /'(\S+)'/ then $1
else
value
context[value]
end)
end
end

View File

@ -61,7 +61,8 @@ module Locomotive
'page' => @page,
'asset_collections' => Locomotive::Liquid::Drops::AssetCollections.new,
'contents' => Locomotive::Liquid::Drops::Contents.new,
'current_page' => self.params[:page]
'current_page' => self.params[:page],
'params' => self.params
}.merge(flash.stringify_keys) # data from api
if @page.templatized? # add instance from content type

View File

@ -2,15 +2,21 @@ require 'spec_helper'
describe Locomotive::Liquid::Tags::WithScope do
it 'should decode options (boolean, interger, ...)' do
it 'should decode basic options (boolean, interger, ...)' do
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'active:true price:42 title:\'foo\' hidden:false', ["{% endwith_scope %}"], {})
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes))
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), {})
attributes['active'].should == true
attributes['price'].should == 42
attributes['title'].should == 'foo'
attributes['hidden'].should == false
end
it 'should decode context variable' do
scope = Locomotive::Liquid::Tags::WithScope.new('with_scope', 'category: params.type', ["{% endwith_scope %}"], {})
attributes = scope.send(:decode, scope.instance_variable_get(:@attributes), { 'params.type' => 'posts' })
attributes['category'].should == 'posts'
end
it 'should store attributes in the context' do
template = ::Liquid::Template.parse("{% with_scope active:true title:'foo' %}{{ with_scope.active }}-{{ with_scope.title }}{% endwith_scope %}")
text = template.render