mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Allow live collections based on generic data. Helps with #1527
This commit is contained in:
parent
81a77828bd
commit
b2cb90c20f
4 changed files with 83 additions and 13 deletions
|
@ -192,4 +192,45 @@ Feature: Collections
|
|||
Then I should see 'Newer Article Content'
|
||||
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'
|
|
@ -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
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
Then /^the file "([^\"]*)" has the contents$/ do |path, contents|
|
||||
write_file(path, contents)
|
||||
@server_inst.files.find_new_files!
|
||||
|
||||
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}"}
|
||||
@server_inst.files.find_new_files!
|
||||
|
||||
in_current_dir do
|
||||
@server_inst.files.find_new_files!
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
rack = ::Middleman::Rack.new(@server_inst)
|
||||
@browser = ::Rack::MockRequest.new(rack.to_app)
|
||||
end
|
||||
end
|
||||
|
||||
Given /^the Server is running at "([^\"]*)"$/ do |app_path|
|
||||
|
|
Loading…
Reference in a new issue