automatically load helper modules from helpers/. closes #238

i18n_v4
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
* Serve purely static folders directly (without source/ and config.rb)
* Set ignored files and disable directory_indexes from YAML frontmatter
* Automatically load helper modules in helpers/ directory
2.0.14
====

View File

@ -3,4 +3,10 @@ Feature: Helpers in external files
Scenario: Hello Helper
Given the Server is running at "external-helpers"
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
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.
autoload :DefaultHelpers, "middleman-core/core_extensions/default_helpers"

View File

@ -225,6 +225,9 @@ class Middleman::Base
# Sitemap
register Middleman::CoreExtensions::Sitemap
# Setup external helpers
register Middleman::CoreExtensions::ExternalHelpers
# Setup default helpers
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
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
# @return [Boolean] Whether the server needs to reload
def needs_to_reload?(paths)
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