automatically load helper modules from helpers/. closes #238
This commit is contained in:
parent
3e0ed70c69
commit
aeb9d10b25
|
@ -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
|
||||
====
|
||||
|
|
|
@ -4,3 +4,9 @@ Feature: Helpers in external files
|
|||
Given the Server is running at "external-helpers"
|
||||
Then going to "/index.html" should not raise an exception
|
||||
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"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
module FourHelper
|
||||
def four; "Four"; end
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
module OneHelper
|
||||
def one; "One"; end
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
module ThreeHelper
|
||||
def three; "Three"; end
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
module TwoHelper
|
||||
def two; "Two"; end
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
<%= one %>:<%= two %>:<%= three %>:<%= four %>
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue