Skip to content

Commit a9be730

Browse files
Tom Hanoldtdennisvandehoef
Tom Hanoldt
authored andcommitted
load helper dynamic
1 parent 16e9e6b commit a9be730

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module ActivesupportOverride
2+
def stylesheet_link_tag(path, media='screen')
3+
'<link href="'+path_to_css(path)+'" media="'+media+'" rel="stylesheet" type="text/css" />'
4+
end
5+
6+
def javascript_include_tag(path)
7+
'<script src="'+path_to_js(path)+'"></script>'
8+
end
9+
10+
def meta_tag(type, content)
11+
'<meta name="'+type+'" content="'+content+'" />'
12+
end
13+
14+
def meta_tag_http(type, content)
15+
'<meta http-equiv="'+type+'" content="'+content+'" />'
16+
end
17+
18+
def link_tag(source, relation, type)
19+
'<link rel="'+relation+'" href="'+source+'" type="'+type+'" />'
20+
end
21+
22+
def path_to_css(path)
23+
return path if external_path?(path)
24+
"css/#{path}"
25+
end
26+
27+
def path_to_js(path)
28+
return path if external_path? path
29+
"js/#{path}"
30+
end
31+
32+
def path_to_image(path)
33+
return path if external_path? path
34+
"images/#{path}"
35+
end
36+
37+
def external_path? path
38+
path.start_with?('//') || path.start_with?('http')
39+
end
40+
end
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module SharedHelper
2+
def sharedhelper_method
3+
'sharedhelper_method'
4+
end
5+
end

dev_root/test1/helper/test_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module TestHelper
2+
def testhelper_method
3+
'testhelper_method'
4+
end
5+
end

dev_root/test1/index.haml

+8
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
Here is the index page content.
66
%p
77
And more index content.
8+
9+
%p
10+
this comes from shared/shared_helper.rb:
11+
= sharedhelper_method
12+
13+
%p
14+
this comes from test1/test_helper.rb:
15+
= testhelper_method

lib/generator/haml_generator.rb

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
require 'fileutils'
22
require 'haml'
3-
require_relative '../activesupport_emulation_helper'
3+
require 'action_view'
4+
5+
#require shared helper
6+
$shared_helper = Dir.glob('./dev_root/shared/helper/*.rb')
7+
$shared_helper.each do |file| require file end
48

59
module Generator
610
class HamlGenerator
@@ -42,25 +46,33 @@ def write file, content
4246
end
4347
end
4448

45-
# TODO include shared/helper dynamic
46-
# Dir.glob(File.join('.', 'dev_root', 'shared', 'helper', '*.rb'), &method(:require))
47-
48-
4949
# Calls to "render" can take a context object that will be accessible from the templates.
5050
class Context
5151
# Any properties of this object are available in the Haml templates.
5252
attr_reader :example_boolean
5353

54-
include ActivesupportEmulationHelper
54+
include ActionView::Helpers
5555

56-
#Dir.glob(File.join('dev_root', 'shared', 'helper', '*.rb'), &method(:include))
56+
$shared_helper.each do |path|
57+
file_without_ext = path.split('/')[-1].split('.').first
58+
module_name = file_without_ext.classify
59+
STDERR.puts 'loading helper -> '+module_name
60+
include module_name.constantize
61+
end
5762

5863
def initialize(example_boolean, scope, options, input_folder, output_folder)
5964
@example_boolean = example_boolean
6065
@scope = scope
6166
@options = options
6267
@input_folder = input_folder
6368
@output_folder = output_folder
69+
Dir.glob("./#{input_folder}/helper/*.rb").each do |path|
70+
require path
71+
file_without_ext = path.split('/')[-1].split('.').first
72+
module_name = file_without_ext.classify
73+
STDERR.puts 'loading project helper -> '+module_name
74+
self.class.send(:include, module_name.constantize)
75+
end
6476
end
6577

6678
# This function is no different from the "copyright_year" function above. It just uses some

web_root/test1/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ <h1 class="test1">
3535
<p>
3636
And more index content.
3737
</p>
38+
<p>
39+
this comes from shared/shared_helper.rb:
40+
sharedhelper_method
41+
</p>
42+
<p>
43+
this comes from test1/test_helper.rb:
44+
testhelper_method
45+
</p>
3846
FOOTER
3947
</body>
4048
</html>

0 commit comments

Comments
 (0)