Extracted scoping into its own module. Fixes #44

Rodrigo Alvarez committed May 14, 2013
commit ef34d3bed7b278e975e9ffbd1873c8e024bac4ec
Showing 9 changed files with 76 additions and 23 deletions
locomotive/wagon/liquid/drops/content_types.rb b/lib/locomotive/wagon/liquid/drops/content_types.rb +5 -22
@@ @@ -1,3 +1,5 @@
+ require "locomotive/wagon/scopeable"
+
module Locomotive
module Wagon
module Liquid
@@ @@ -12,6 +14,7 @@ module Locomotive
end
class ProxyCollection < ::Liquid::Drop
+ include Scopeable
def initialize(content_type)
@content_type = content_type
@@ @@ -91,28 +94,8 @@ module Locomotive
def collection
return unless @collection.blank?
-
- if @context['with_scope'].blank?
- @collection = @content_type.entries
- else
- @collection = []
-
- conditions = @context['with_scope'].clone.delete_if { |k, _| %w(order_by per_page page).include?(k) }
-
- @content_type.entries.each do |content|
- accepted = (conditions.map do |key, value|
- case value
- when TrueClass, FalseClass, String then content.send(key) == value
- else
- true
- end
- end).all? # all conditions works ?
-
- @collection << content if accepted
- end
- end
-
- @collection
+
+ @collection = apply_scope(@content_type.entries)
end
end
end
locomotive/wagon/liquid/drops/site.rb b/lib/locomotive/wagon/liquid/drops/site.rb +4 -1
@@ @@ -1,8 +1,11 @@
+ require "locomotive/wagon/scopeable"
+
module Locomotive
module Wagon
module Liquid
module Drops
class Site < Base
+ include Scopeable
delegate :name, :seo_title, :meta_description, :meta_keywords, :to => '_source'
@@ @@ -11,7 +14,7 @@ module Locomotive
end
def pages
- @pages ||= liquify(*self.mounting_point.pages.values)
+ @pages ||= liquify(*apply_scope(self.mounting_point.pages.values))
end
end
locomotive/wagon/scopeable.rb b/lib/locomotive/wagon/scopeable.rb +28 -0
@@ @@ -0,0 +1,28 @@
+ module Locomotive
+ module Wagon
+ module Scopeable
+ def apply_scope(entries)
+ if @context['with_scope'].blank?
+ entries
+ else
+ collection = []
+
+ conditions = @context['with_scope'].clone.delete_if { |k, _| %w(order_by per_page page).include?(k) }
+
+ entries.each do |content|
+ accepted = (conditions.map do |key, value|
+ case value
+ when TrueClass, FalseClass, String then content.send(key) == value
+ else
+ true
+ end
+ end).all? # all conditions works ?
+
+ collection << content if accepted
+ end
+ collection
+ end
+ end
+ end
+ end
+ end
\ No newline at end of file
locomotivecms_wagon.gemspec +1 -0
@@ @@ -40,4 +40,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'vcr'
gem.add_development_dependency 'webmock', '~> 1.8.0'
gem.add_development_dependency 'rack-test'
+ gem.add_development_dependency 'launchy'
end
spec/fixtures/default/app/views/pages/grunge_bands.liquid.haml +8 -0
@@ @@ -0,0 +1,8 @@
+ ---
+ title: Grunge leaders
+ ---
+ {% with_scope kind: "grunge" %}
+ {% for band in contents.bands %}
+ {{ band.leader }}
+ {% endfor %}
+ {% endwith_scope %}
\ No newline at end of file
spec/fixtures/default/app/views/pages/unlisted_pages.liquid.haml +9 -0
@@ @@ -0,0 +1,9 @@
+ ---
+ title: Unlisted pages
+ ---
+ %ul
+ {% with_scope listed: false %}
+ {% for page in site.pages %}
+ %li {{ page.title }}
+ {% endfor %}
+ {% endwith_scope %}
\ No newline at end of file
spec/integration/server/basic_spec.rb +13 -0
@@ @@ -76,6 +76,19 @@ describe Locomotive::Wagon::Server do
end
+
+ describe 'contents with_scope' do
+ subject { get '/grunge_bands'; last_response.body }
+
+ it { should match(/Layne/)}
+ it { should_not match(/Peter/) }
+ end
+
+ describe "pages with_scope" do
+ subject { get '/unlisted_pages'; last_response.body }
+ it { subject.should match(/Page to test the nav tag/)}
+ it { should_not match(/About Us/)}
+ end
describe 'theme assets' do
spec/spec_helper.rb +1 -0
@@ @@ -1,5 +1,6 @@
require "locomotive/wagon"
require "rspec"
+ require "launchy"
Dir["#{File.expand_path('../support', __FILE__)}/*.rb"].each do |file|
require file
spec/support/helpers.rb +7 -0
@@ @@ -22,6 +22,13 @@ module Spec
reader.run!(path: path)
Locomotive::Wagon::Server.new(reader, disable_listen: true)
end
+
+ def open_in_browser
+ path = File.join(File.dirname(__FILE__), '..', 'tmp', "wagon-#{Time.new.strftime("%Y%m%d%H%M%S")}#{rand(10**10)}.html")
+ FileUtils.mkdir_p(File.dirname(path))
+ File.open(path,'w') { |f| f.write last_response.body }
+ Launchy.open(path)
+ end
end
end
\ No newline at end of file