From b2cb90c20f92cdd36c72619a92736cbd00344ae2 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 2 Jun 2015 16:16:07 -0700 Subject: [PATCH] Allow live collections based on generic data. Helps with #1527 --- middleman-core/features/collections.feature | 43 ++++++++++++++++++- .../core_extensions/collections.rb | 36 +++++++++++++--- .../step_definitions/middleman_steps.rb | 10 ++++- .../step_definitions/server_steps.rb | 7 ++- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/middleman-core/features/collections.feature b/middleman-core/features/collections.feature index 5b4f5625..30f32691 100644 --- a/middleman-core/features/collections.feature +++ b/middleman-core/features/collections.feature @@ -192,4 +192,45 @@ Feature: Collections Then I should see 'Newer Article Content' When I go to "2.html" Then I should see 'Again' - \ No newline at end of file + + 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' \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/core_extensions/collections.rb b/middleman-core/lib/middleman-core/core_extensions/collections.rb index f1cea9a5..3491d894 100644 --- a/middleman-core/lib/middleman-core/core_extensions/collections.rb +++ b/middleman-core/lib/middleman-core/core_extensions/collections.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb b/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb index cca191f3..f4bed280 100644 --- a/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/middleman_steps.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/step_definitions/server_steps.rb b/middleman-core/lib/middleman-core/step_definitions/server_steps.rb index 5f3fe6ff..2fbbd3a5 100644 --- a/middleman-core/lib/middleman-core/step_definitions/server_steps.rb +++ b/middleman-core/lib/middleman-core/step_definitions/server_steps.rb @@ -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|