fix issues about the with_scope liquid tag (and the proxy_collection)

did committed Feb 17, 2015
commit 0320b8457f0565667cb725434f3d7ed8b3c917e5
Showing 12 changed files with 117 additions and 42 deletions
locomotive/steam/liquid/drops/content_types.rb b/lib/locomotive/steam/liquid/drops/content_types.rb +1 -1
@@ @@ -58,7 +58,7 @@ module Locomotive
end
def collection
- repository.all(@content_type, @context['with_scope'])
+ @collection ||= repository.all(@content_type, @context['with_scope'])
end
end
locomotive/steam/liquid/drops/proxy_collection.rb b/lib/locomotive/steam/liquid/drops/proxy_collection.rb +5 -1
@@ @@ -17,7 +17,11 @@ module Locomotive
@count ||= collection.count
end
- alias :all :collection
+ def all
+ collection
+ self
+ end
+
alias :size :count
alias :length :count
locomotive/steam/liquid/filters/misc.rb b/lib/locomotive/steam/liquid/filters/misc.rb +18 -0
@@ @@ -22,6 +22,24 @@ module Locomotive
rand(input.to_i)
end
+ # map/collect on a given property (support to_f, to_i)
+ def map(input, property)
+ ::Liquid::StandardFilters::InputIterator.new(input).map do |e|
+ e = e.call if e.is_a?(Proc)
+
+ if property == "to_liquid".freeze
+ e
+ elsif property == 'to_f'.freeze
+ e.to_f
+ elsif property == 'to_i'.freeze
+ e.to_i
+ elsif e.respond_to?(:[])
+ e[property]
+ end
+ end
+
+ end
+
end
::Liquid::Template.register_filter(Misc)
locomotive/steam/repositories/filesystem/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/content_entry.rb +1 -1
@@ @@ -16,7 +16,7 @@ module Locomotive
# priority:
# 1/ order_by passed in the conditions parameter
# 2/ the default order (_position) defined in the content type
- order_by = conditions.delete(:order_by) || type.order_by
+ order_by = conditions.delete(:order_by)|| conditions.delete('order_by') || type.order_by
query(type) do
where(conditions).order_by(order_by)
locomotive/steam/repositories/filesystem/memory_adapter/query.rb b/lib/locomotive/steam/repositories/filesystem/memory_adapter/query.rb +4 -1
@@ @@ -39,7 +39,10 @@ module Locomotive
end
def order_by(order_string)
- @sorting = order_string.downcase.split.map(&:to_sym) unless order_string.blank?
+ unless order_string.blank?
+ pattern = order_string.include?('.') ? '.' : ' '
+ @sorting = order_string.downcase.split(pattern).map(&:to_sym)
+ end
self
end
locomotive/steam/repositories/filesystem/models/content_entry.rb b/lib/locomotive/steam/repositories/filesystem/models/content_entry.rb +10 -4
@@ @@ -27,6 +27,16 @@ module Locomotive
alias :_id :_slug
alias :_permalink :_slug
+ def method_missing(name, *args, &block)
+ if is_dynamic_attribute?(name)
+ cast_value(name)
+ elsif attributes.include?(name)
+ self[name]
+ else
+ super
+ end
+ end
+
def content_type
@content_type || attributes[:content_type]
end
@@ @@ -39,10 +49,6 @@ module Locomotive
self[content_type.label_field_name]
end
- def [](name)
- is_dynamic_attribute?(name) ? cast_value(name) : super
- end
-
def localized_attributes
self.class.localized_attributes + content_type.localized_fields_names
end
spec/fixtures/default/app/content_types/bands.yml +3 -1
@@ @@ -16,4 +16,6 @@ fields:
type: has_many
class_name: songs
inverse_of: band
- ui_enabled: true
\ No newline at end of file
+ ui_enabled: true
+ - featured:
+ type: boolean
spec/fixtures/default/app/views/pages/music.liquid.haml +1 -1
@@ @@ -39,4 +39,4 @@ position: 2
{% endfor %}
%p.collection_equality {{ contents.songs.all.size }}={{ contents.songs.size }}
- {% endblock %}
\ No newline at end of file
+ {% endblock %}
spec/fixtures/default/data/bands.yml +3 -0
@@ @@ -1,10 +1,13 @@
- Pearl Jam:
leader: Eddie
kind: grunge
+ featured: false
- Alice in Chains:
leader: Layne
kind: grunge
+ featured: false
- The who:
leader: Peter
kind: rock
+ featured: true
spec/integration/server/with_scope_spec.rb +32 -32
@@ @@ -1,42 +1,42 @@
- # require File.dirname(__FILE__) + '/../integration_helper'
+ require File.dirname(__FILE__) + '/../integration_helper'
- # describe 'Complex with_scope conditions' do
+ describe 'Complex with_scope conditions' do
- # include Rack::Test::Methods
+ include Rack::Test::Methods
- # def app
- # run_server
- # end
+ def app
+ run_server
+ end
- # it 'returns the right number of events' do
- # get '/filtered'
- # expect(last_response.body).to include 'events=1'
- # end
+ it 'returns the right number of events' do
+ get '/filtered'
+ expect(last_response.body).to include 'events=1'
+ end
- # it 'returns the right number of bands' do
- # get '/filtered'
- # expect(last_response.body).to include 'bands=2'
- # end
+ it 'returns the right number of bands' do
+ get '/filtered'
+ expect(last_response.body).to include 'bands=2'
+ end
- # it 'returns the first band in the right order' do
- # get '/filtered'
- # expect(last_response.body).to include "first event=Browne's Market"
- # end
+ it 'returns the first band in the right order' do
+ get '/filtered'
+ expect(last_response.body).to include "first event=Browne's Market"
+ end
- # # it 'returns the right number of events' do
- # # get '/filtered'
- # # last_response.body.should =~ /events=1./
- # # end
+ it 'returns the right number of events' do
+ get '/filtered'
+ expect(last_response.body).to include 'events=1'
+ end
- # # it 'evaluates collection when called all inside of scope' do
- # # get '/music'
- # # expect(last_response.body).to include "<p class='scoped_song'>Song #3"
- # # expect(last_response.body).to match /<p class=scoped_song_link.>\s+<a href=.\/songs\/song-number-3.>Song #3/m
- # # end
+ it 'evaluates collection when called all inside of scope' do
+ get '/music'
+ expect(last_response.body).to include "<p class='scoped_song'>Song #3"
+ expect(last_response.body).to match /<p class='scoped_song_link'>\s+<a href='\/songs\/song-number-3'>Song #3<\/a>/m
+ end
- # # it 'size of evaluated unscoped collection equal to unevaluated one' do
- # # get '/music'
- # # expect(last_response.body).to include "class='collection_equality'>8=8"
- # # end
+ it 'size of evaluated unscoped collection equal to unevaluated one' do
+ get '/music'
+ expect(last_response.body).to include "class='collection_equality'>8=8"
+ end
- # end
+ end
spec/unit/liquid/filters/misc_spec.rb +32 -0
@@ @@ -73,4 +73,36 @@ describe Locomotive::Steam::Liquid::Filters::Misc do
expect(random_number.class).to eq Fixnum
end
+ describe '#map' do
+
+ context 'to_liquid' do
+
+ subject { map(['4', '5'], 'to_liquid') }
+ it { is_expected.to eq(['4', '5']) }
+
+ end
+
+ context 'to_i' do
+
+ subject { map(['4', '5'], 'to_i') }
+ it { is_expected.to eq([4, 5]) }
+
+ end
+
+ context 'to_f' do
+
+ subject { map(['4.3', '5.2'], 'to_f') }
+ it { is_expected.to eq([4.3, 5.2]) }
+
+ end
+
+ context 'property' do
+
+ subject { map([{ 'title' => 'a' }, { 'title' => 'b' }], 'title') }
+ it { is_expected.to eq(['a', 'b']) }
+
+ end
+
+ end
+
end
spec/unit/repositories/filesystem/models/content_entry_spec.rb +7 -0
@@ @@ -57,6 +57,13 @@ describe Locomotive::Steam::Repositories::Filesystem::Models::ContentEntry do
end
+ context 'no provided value, should return nil' do
+
+ let(:attributes) { {} }
+ it { is_expected.to eq nil }
+
+ end
+
context 'a string' do
let(:value) { 'Hello world' }
it { is_expected.to eq 'Hello world' }