From 33cb9b3ba93c94949e090b5fc6e43088e7364210 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 26 Apr 2015 13:22:58 -0700 Subject: [PATCH] Expand collection laziness to data --- middleman-core/features/collections.feature | 50 +++++++++++++++++++ .../2011-01-01-new-article.html.markdown | 2 + .../lib/middleman-core/config_context.rb | 2 +- .../core_extensions/collections.rb | 11 ++-- .../middleman-core/core_extensions/data.rb | 2 +- .../lib/middleman-core/renderers/slim.rb | 1 + 6 files changed, 62 insertions(+), 6 deletions(-) diff --git a/middleman-core/features/collections.feature b/middleman-core/features/collections.feature index 5020c147..5b4f5625 100644 --- a/middleman-core/features/collections.feature +++ b/middleman-core/features/collections.feature @@ -143,3 +143,53 @@ Feature: Collections When I go to "index.html" And I should see 'Article: Blog3 Another Article' And I should see 'Article: Blog2 Yet Another Article' + + + Scenario: Collected data update with file changes + Given a fixture app "collections-app" + And a file named "config.rb" with: + """ + data.articles.each_with_index do |a, i| + proxy "/#{i}.html", a + end + """ + And a file named "data/articles.yaml" with: + """ + --- + - "/blog1/2011-01-01-new-article.html" + - "/blog2/2011-01-02-another-article.html" + """ + Given the Server is running at "collections-app" + When I go to "0.html" + Then I should see 'Newer Article Content' + When I go to "1.html" + Then I should see 'Another Article Content' + When I go to "2.html" + Then I should see 'Not Found' + + When the file "data/articles.yaml" has the contents + """ + --- + - "/blog1/2011-01-01-new-article.html" + """ + When I go to "0.html" + Then I should see 'Newer Article Content' + When I go to "1.html" + Then I should see 'Not Found' + When I go to "2.html" + Then I should see 'Not Found' + + When the file "data/articles.yaml" has the contents + """ + --- + - "/blog2/2011-01-02-another-article.html" + - "/blog1/2011-01-01-new-article.html" + - "/blog2/2011-01-01-new-article.html" + """ + When I go to "0.html" + Then I should see 'Another Article Content' + When I go to "1.html" + Then I should see 'Newer Article Content' + When I go to "2.html" + Then I should see 'Again' + \ No newline at end of file diff --git a/middleman-core/fixtures/collections-app/source/blog2/2011-01-01-new-article.html.markdown b/middleman-core/fixtures/collections-app/source/blog2/2011-01-01-new-article.html.markdown index 125b71fa..e50d19af 100755 --- a/middleman-core/fixtures/collections-app/source/blog2/2011-01-01-new-article.html.markdown +++ b/middleman-core/fixtures/collections-app/source/blog2/2011-01-01-new-article.html.markdown @@ -5,3 +5,5 @@ tags: foo, bar --- Newer Article Content + +Again \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/config_context.rb b/middleman-core/lib/middleman-core/config_context.rb index b517f2bd..db351c0f 100644 --- a/middleman-core/lib/middleman-core/config_context.rb +++ b/middleman-core/lib/middleman-core/config_context.rb @@ -7,7 +7,7 @@ module Middleman attr_reader :app # Whitelist methods that can reach out. - def_delegators :@app, :config, :logger, :use, :map, :mime_type, :data, :files, :root, :build?, :server?, :environment? + def_delegators :@app, :config, :logger, :use, :map, :mime_type, :files, :root, :build?, :server?, :environment? def_delegator :"@app.extensions", :activate def initialize(app, template_context_class) diff --git a/middleman-core/lib/middleman-core/core_extensions/collections.rb b/middleman-core/lib/middleman-core/core_extensions/collections.rb index c8ceb1fd..0a0971b4 100644 --- a/middleman-core/lib/middleman-core/core_extensions/collections.rb +++ b/middleman-core/lib/middleman-core/core_extensions/collections.rb @@ -16,7 +16,7 @@ module Middleman # gets a chance to modify any new resources that get added. self.resource_list_manipulator_priority = 110 - attr_accessor :root_collector, :leaves + attr_accessor :sitemap_collector, :data_collector, :leaves def initialize(app, options_hash={}, &block) super @@ -25,14 +25,16 @@ module Middleman @collectors_by_name = {} @values_by_name = {} - @root_collector = LazyCollectorRoot.new(self) + @sitemap_collector = LazyCollectorRoot.new(self) + @data_collector = LazyCollectorRoot.new(self) end Contract Any def before_configuration @leaves.clear - app.add_to_config_context :resources, &method(:root_collector) + app.add_to_config_context :resources, &method(:sitemap_collector) + app.add_to_config_context :data, &method(:data_collector) app.add_to_config_context :collection, &method(:register_collector) end @@ -48,7 +50,8 @@ module Middleman Contract ResourceList => ResourceList def manipulate_resource_list(resources) - @root_collector.realize!(resources) + @sitemap_collector.realize!(resources) + @data_collector.realize!(app.data) ctx = StepContext.new leaves = @leaves.dup diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index 46279c43..bc578bff 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -14,11 +14,11 @@ module Middleman def initialize(app, config={}, &block) super + @data_store = DataStore.new(app, DATA_FILE_MATCHER) app.config.define_setting :data_dir, 'data', 'The directory data files are stored in' app.add_to_instance(:data, &method(:data_store)) - app.add_to_config_context(:data, &method(:data_store)) start_watching(app.config[:data_dir]) end diff --git a/middleman-core/lib/middleman-core/renderers/slim.rb b/middleman-core/lib/middleman-core/renderers/slim.rb index 40b44c34..21277fe4 100644 --- a/middleman-core/lib/middleman-core/renderers/slim.rb +++ b/middleman-core/lib/middleman-core/renderers/slim.rb @@ -24,6 +24,7 @@ module Middleman super # Setup Slim options to work with partials + ::Slim::Engine.disable_option_validator! ::Slim::Engine.set_options( buffer: '@_out_buf', use_html_safe: true,