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

@ -192,4 +192,45 @@ Feature: Collections
Then I should see 'Newer Article Content' Then I should see 'Newer Article Content'
When I go to "2.html" When I go to "2.html"
Then I should see 'Again' 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. # gets a chance to modify any new resources that get added.
self.resource_list_manipulator_priority = 110 self.resource_list_manipulator_priority = 110
attr_accessor :sitemap_collector, :data_collector, :leaves attr_accessor :leaves
# Expose `resources`, `data`, and `collection` to config. # Expose `resources`, `data`, and `collection` to config.
expose_to_config resources: :sitemap_collector, expose_to_config resources: :sitemap_collector,
data: :data_collector, data: :data_collector,
collection: :register_collector collection: :register_collector,
live: :live_collector
# Exposes `collection` to templates # Exposes `collection` to templates
expose_to_template collection: :collector_value expose_to_template collection: :collector_value
@ -39,8 +40,7 @@ module Middleman
@collectors_by_name = {} @collectors_by_name = {}
@values_by_name = {} @values_by_name = {}
@sitemap_collector = LazyCollectorRoot.new(self) @collector_roots = []
@data_collector = LazyCollectorRoot.new(self)
end end
def before_configuration def before_configuration
@ -52,6 +52,28 @@ module Middleman
@collectors_by_name[label] = endpoint @collectors_by_name[label] = endpoint
end 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 Contract Symbol => Any
def collector_value(label) def collector_value(label)
@values_by_name[label] @values_by_name[label]
@ -59,8 +81,10 @@ module Middleman
Contract ResourceList => ResourceList Contract ResourceList => ResourceList
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
@sitemap_collector.realize!(resources) @collector_roots.each do |pair|
@data_collector.realize!(app.data) dataset = pair[:block].call(app, resources)
pair[:root].realize!(dataset)
end
ctx = StepContext.new ctx = StepContext.new
leaves = @leaves.dup leaves = @leaves.dup

View file

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

View file

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