Allow live collections based on generic data. Helps with #1527

This commit is contained in:
Thomas Reynolds 2015-06-02 16:16:07 -07:00
parent 81a77828bd
commit b2cb90c20f
4 changed files with 83 additions and 13 deletions

View file

@ -193,3 +193,44 @@ Feature: Collections
When I go to "2.html"
Then I should see 'Again'
Scenario: Arbitrary live datasets
Given a fixture app "collections-app"
And a file named "config.rb" with:
"""
live {
Dir["descriptions/*.txt"]
}.each do |description_name|
base = File.basename(description_name, '.txt')
proxy "#{base}.html", "/description_template.html", locals: {
contents: File.read(description_name)
}, ignore: true
end
"""
And a file named "source/description_template.html.erb" with:
"""
<%= contents %>
"""
And a file named "descriptions/test1.txt" with:
"""
Test1
"""
Given the Server is running at "collections-app"
When I go to "test1.html"
Then I should see 'Test1'
When I go to "test2.html"
Then I should see 'Not Found'
When the file "descriptions/test2.txt" has the contents
"""
Test2
"""
When I go to "test1.html"
Then I should see 'Test1'
When I go to "test2.html"
Then I should see 'Test2'
When the file "descriptions/test1.txt" is removed
When I go to "test1.html"
Then I should see 'Not Found'
When I go to "test2.html"
Then I should see 'Test2'

View file

@ -16,12 +16,13 @@ module Middleman
# gets a chance to modify any new resources that get added.
self.resource_list_manipulator_priority = 110
attr_accessor :sitemap_collector, :data_collector, :leaves
attr_accessor :leaves
# Expose `resources`, `data`, and `collection` to config.
expose_to_config resources: :sitemap_collector,
data: :data_collector,
collection: :register_collector
collection: :register_collector,
live: :live_collector
# Exposes `collection` to templates
expose_to_template collection: :collector_value
@ -39,8 +40,7 @@ module Middleman
@collectors_by_name = {}
@values_by_name = {}
@sitemap_collector = LazyCollectorRoot.new(self)
@data_collector = LazyCollectorRoot.new(self)
@collector_roots = []
end
def before_configuration
@ -52,6 +52,28 @@ module Middleman
@collectors_by_name[label] = endpoint
end
Contract LazyCollectorRoot
def sitemap_collector
live_collector { |_, resources| resources }
end
Contract LazyCollectorRoot
def data_collector
live_collector { |app, _| app.data }
end
Contract Proc => LazyCollectorRoot
def live_collector(&block)
root = LazyCollectorRoot.new(self)
@collector_roots << {
root: root,
block: block
}
root
end
Contract Symbol => Any
def collector_value(label)
@values_by_name[label]
@ -59,8 +81,10 @@ module Middleman
Contract ResourceList => ResourceList
def manipulate_resource_list(resources)
@sitemap_collector.realize!(resources)
@data_collector.realize!(app.data)
@collector_roots.each do |pair|
dataset = pair[:block].call(app, resources)
pair[:root].realize!(dataset)
end
ctx = StepContext.new
leaves = @leaves.dup

View file

@ -1,9 +1,15 @@
Then /^the file "([^\"]*)" has the contents$/ do |path, contents|
write_file(path, contents)
in_current_dir do
@server_inst.files.find_new_files!
end
end
Then /^the file "([^\"]*)" is removed$/ do |path|
step %Q{I remove the file "#{path}"}
in_current_dir do
@server_inst.files.find_new_files!
end
end

View file

@ -33,7 +33,6 @@ end
Given /^the Server is running$/ do
root_dir = File.expand_path(current_dir)
if File.exists?(File.join(root_dir, 'source'))
ENV['MM_SOURCE'] = 'source'
else
@ -53,10 +52,10 @@ Given /^the Server is running$/ do
instance_exec(&p)
end
end
end
rack = ::Middleman::Rack.new(@server_inst)
@browser = ::Rack::MockRequest.new(rack.to_app)
end
end
Given /^the Server is running at "([^\"]*)"$/ do |app_path|