collections can be ordered by with_scope + fix link_to for templatized pages + new way of setting Sprockets + new version of the mounter (1.2.6)

did committed Sep 02, 2013
commit 8edca4dd8e8b0d73de5ca0317d90d1d164f718d4
Showing 9 changed files with 84 additions and 15 deletions
locomotive/wagon/liquid/drops/content_types.rb b/lib/locomotive/wagon/liquid/drops/content_types.rb +5 -0
@@ @@ -99,6 +99,11 @@ module Locomotive
def collection
return unless @collection.blank?
+ # define the default order_by if not set
+ if @context['with_scope'] && !@context['with_scope']['order_by'].blank? && !%w(manually position).include?(@content_type.order_by)
+ @context['with_scope']['order_by'] = @content_type.order_by + '.' + @content_type.order_direction
+ end
+
@collection = apply_scope(@content_type.entries)
end
end
locomotive/wagon/liquid/filters/text.rb b/lib/locomotive/wagon/liquid/filters/text.rb +4 -0
@@ @@ -33,6 +33,10 @@ module Locomotive
result
end
+ def encode(input)
+ Rack::Utils.escape(input)
+ end
+
def textile(input)
::RedCloth.new(input).to_html
end
locomotive/wagon/liquid/scopeable.rb b/lib/locomotive/wagon/liquid/scopeable.rb +48 -4
@@ @@ -16,7 +16,7 @@ module Locomotive
Locomotive::Wagon::Logger.info "[with_scope] conditions: #{conditions.map(&:to_s).join(', ')}"
# get only the entries matching ALL the conditions
- entries.find_all do |content|
+ _entries = entries.find_all do |content|
accepted = true
conditions.each do |_condition|
@@ @@ -28,6 +28,22 @@ module Locomotive
accepted
end
+
+ self._apply_scope_order(_entries, @context['with_scope']['order_by'])
+ end
+ end
+
+ def _apply_scope_order(entries, order_by)
+ return entries if order_by.blank?
+
+ name, direction = order_by.split('.').map(&:to_sym)
+
+ Locomotive::Wagon::Logger.info "[with_scope] order_by #{name} #{direction || 'asc'}"
+
+ if direction == :asc || direction.nil?
+ entries.sort { |a, b| a.send(name) <=> b.send(name) }
+ else
+ entries.sort { |a, b| b.send(name) <=> a.send(name) }
end
end
@@ @@ -40,6 +56,8 @@ module Locomotive
def initialize(name, value)
self.name, self.right_operand = name, value
+ self.process_right_operand
+
# default value
self.operator = :==
@@ @@ -47,7 +65,7 @@ module Locomotive
end
def matches?(entry)
- value = entry.send(self.name)
+ value = self.get_value(entry)
self.decode_operator_based_on_value(value)
@@ @@ -55,7 +73,7 @@ module Locomotive
when :== then value == self.right_operand
when :ne then value != self.right_operand
when :matches then self.right_operand =~ value
- when :gte then value > self.right_operand
+ when :gt then value > self.right_operand
when :gte then value >= self.right_operand
when :lt then value < self.right_operand
when :lte then value <= self.right_operand
@@ @@ -74,11 +92,37 @@ module Locomotive
end
def to_s
- "#{name} #{operator} #{self.right_operand.inspect}"
+ "#{name} #{operator} #{self.right_operand.to_s}"
end
protected
+ def get_value(entry)
+ value = entry.send(self.name)
+
+ if value.respond_to?(:_slug)
+ # belongs_to
+ value._slug
+ elsif value.respond_to?(:map)
+ # many_to_many or tags ?
+ value.map { |v| v.respond_to?(:_slug) ? v._slug : v }
+ else
+ value
+ end
+ end
+
+ def process_right_operand
+ if self.right_operand.respond_to?(:_slug)
+ # belongs_to
+ self.right_operand = self.right_operand._slug
+ elsif self.right_operand.respond_to?(:map) && self.right_operand.first.respond_to?(:_slug)
+ # many_to_many
+ self.right_operand = self.right_operand.map do |entry|
+ entry.try(&:_slug)
+ end
+ end
+ end
+
def decode_operator_based_on_name
if name =~ /^([a-z0-9_-]+)\.(#{OPERATORS.join('|')})$/
self.name = $1.to_sym
locomotive/wagon/liquid/tags/link_to.rb b/lib/locomotive/wagon/liquid/tags/link_to.rb +1 -0
@@ @@ -64,6 +64,7 @@ module Locomotive
if templatized
page = mounting_point.pages.values.find do |_page|
_page.templatized? &&
+ !_page.templatized_from_parent &&
_page.content_type.slug == handle.content_type.slug &&
(@options['with'].nil? || _page.handle == @options['with'])
end
locomotive/wagon/server.rb b/lib/locomotive/wagon/server.rb +0 -2
@@ @@ -1,7 +1,5 @@
require 'better_errors'
require 'coffee_script'
- require 'sprockets'
- require 'sprockets-sass'
require 'locomotive/wagon/listen'
require 'locomotive/wagon/server/middleware'
locomotive/wagon/server/dynamic_assets.rb b/lib/locomotive/wagon/server/dynamic_assets.rb +3 -8
@@ @@ -5,17 +5,12 @@ module Locomotive::Wagon
attr_reader :app, :sprockets, :regexp
- def initialize(app, root)
+ def initialize(app, site_path)
super(app)
- @regexp = /^\/(javascripts|stylesheets)\/(.*)$/
+ @regexp = /^\/(javascripts|stylesheets)\/(.*)$/
- # make sure Compass is correctly configured
- Locomotive::Mounter::Extensions::Compass.configure(root)
-
- @sprockets = Sprockets::Environment.new
- @sprockets.append_path File.join(root, 'public/stylesheets')
- @sprockets.append_path File.join(root, 'public/javascripts')
+ @sprockets = Locomotive::Mounter::Extensions::Sprockets.environment(site_path)
end
def call(env)
locomotivecms_wagon.gemspec +1 -1
@@ @@ -35,7 +35,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'httmultiparty', '0.3.10'
gem.add_dependency 'will_paginate', '~> 3.0.3'
- gem.add_dependency 'locomotivecms_mounter', '~> 1.2.5'
+ gem.add_dependency 'locomotivecms_mounter', '~> 1.2.6'
gem.add_dependency 'faker', '~> 0.9.5'
spec/fixtures/default/app/views/pages/songs/template/band.liquid.haml +16 -0
@@ @@ -0,0 +1,16 @@
+ ---
+ title: Band
+ ---
+ {% extends parent %}
+
+ {% block content %}
+
+ %h3 {{ song.title }}
+
+ %h4 Leader: {{ song.band.leader }}
+
+ #is_templatized{templatized: "{{ page.templatized? }}"}
+ #content_type_size{content_type_size: "{{ page.content_type.size }}"}
+ #content_type_count{content_type_count: "{{ page.content_type.count }}"}
+
+ {% endblock %}
\ No newline at end of file
spec/integration/server/basic_spec.rb +6 -0
@@ @@ -32,6 +32,12 @@ describe Locomotive::Wagon::Server do
last_response.body.should =~ /Song #1/
end
+ it 'renders a page under a templatized one' do
+ get '/songs/song-1/band'
+ last_response.body.should =~ /Song #1/
+ last_response.body.should =~ /Leader: Eddie/
+ end
+
it 'translates strings' do
get '/en'
last_response.body.should =~ /Powered by/