automatically load helper modules from helpers/. closes #238

This commit is contained in:
Thomas Reynolds 2012-01-15 13:43:26 -08:00
parent 3e0ed70c69
commit aeb9d10b25
11 changed files with 64 additions and 3 deletions

View file

@ -24,6 +24,7 @@
* Don't re-minify files with ".min" in their name * Don't re-minify files with ".min" in their name
* Serve purely static folders directly (without source/ and config.rb) * Serve purely static folders directly (without source/ and config.rb)
* Set ignored files and disable directory_indexes from YAML frontmatter * Set ignored files and disable directory_indexes from YAML frontmatter
* Automatically load helper modules in helpers/ directory
2.0.14 2.0.14
==== ====

View file

@ -4,3 +4,9 @@ Feature: Helpers in external files
Given the Server is running at "external-helpers" Given the Server is running at "external-helpers"
Then going to "/index.html" should not raise an exception Then going to "/index.html" should not raise an exception
And I should see "Hello World" And I should see "Hello World"
Scenario: Automatic Helpers
Given the Server is running at "external-helpers"
Then going to "/automatic.html" should not raise an exception
And I should see "One:Two:Three:Four"

View file

@ -0,0 +1,3 @@
module FourHelper
def four; "Four"; end
end

View file

@ -0,0 +1,3 @@
module OneHelper
def one; "One"; end
end

View file

@ -0,0 +1,3 @@
module ThreeHelper
def three; "Three"; end
end

View file

@ -0,0 +1,3 @@
module TwoHelper
def two; "Two"; end
end

View file

@ -0,0 +1 @@
<%= one %>:<%= two %>:<%= three %>:<%= four %>

View file

@ -72,6 +72,9 @@ module Middleman
# Parse YAML from templates # Parse YAML from templates
autoload :FrontMatter, "middleman-core/core_extensions/front_matter" autoload :FrontMatter, "middleman-core/core_extensions/front_matter"
# External helpers looks in the helpers/ folder for helper modules
autoload :ExternalHelpers, "middleman-core/core_extensions/external_helpers"
# DefaultHelpers are the built-in dynamic template helpers. # DefaultHelpers are the built-in dynamic template helpers.
autoload :DefaultHelpers, "middleman-core/core_extensions/default_helpers" autoload :DefaultHelpers, "middleman-core/core_extensions/default_helpers"

View file

@ -225,6 +225,9 @@ class Middleman::Base
# Sitemap # Sitemap
register Middleman::CoreExtensions::Sitemap register Middleman::CoreExtensions::Sitemap
# Setup external helpers
register Middleman::CoreExtensions::ExternalHelpers
# Setup default helpers # Setup default helpers
register Middleman::CoreExtensions::DefaultHelpers register Middleman::CoreExtensions::DefaultHelpers

View file

@ -0,0 +1,35 @@
# Load helpers in helpers/
module Middleman::CoreExtensions::ExternalHelpers
# Setup extension
class << self
# once registered
def registered(app)
# Setup a default helpers paths
app.set :helpers_dir, "helpers"
app.set :helpers_filename_glob, "**/*_helper.rb"
app.set :helpers_filename_to_module_name_proc, Proc.new { |filename|
basename = File.basename(filename, File.extname(filename))
basename.camelcase
}
# After config
app.after_configuration do
helpers_path = File.expand_path(helpers_dir, root)
next unless File.exists?(helpers_path)
Dir[File.join(helpers_path, helpers_filename_glob)].each do |filename|
module_name = helpers_filename_to_module_name_proc.call(filename)
next unless module_name
require filename
next unless Object.const_defined?(module_name.to_sym)
helpers Object.const_get(module_name.to_sym)
end
end
end
alias :included :registered
end
end

View file

@ -130,12 +130,12 @@ module Middleman
end end
private private
# Whether the passed files are config.rb or lib/*.rb # Whether the passed files are config.rb, lib/*.rb or helpers
# @param [Array<String>] paths Array of paths to check # @param [Array<String>] paths Array of paths to check
# @return [Boolean] Whether the server needs to reload # @return [Boolean] Whether the server needs to reload
def needs_to_reload?(paths) def needs_to_reload?(paths)
paths.any? do |path| paths.any? do |path|
path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$}) path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$}) || path.match(%r{^helpers/^[^\.](.*)_helper\.rb$})
end end
end end