Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 937682e

Browse files
committed
Guard existence of #controller in requirejs_include_tag
Minor refactoring to aid some experiments in using test frameworks with the gem.
1 parent 4742bd2 commit 937682e

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

app/helpers/requirejs_helper.rb

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,42 @@ def requirejs_include_tag(name=nil, &block)
3333

3434
html = ""
3535

36-
if controller.requirejs_included
37-
raise Requirejs::MultipleIncludeError, "Only one requirejs_include_tag allowed per page."
38-
end
39-
40-
unless requirejs.run_config.empty?
41-
run_config = requirejs.run_config
42-
if Rails.application.config.assets.digest
43-
modules = requirejs.build_config['modules'].map { |m| requirejs.module_name_for m }
36+
_once_guard do
37+
unless requirejs.run_config.empty?
38+
run_config = requirejs.run_config
39+
if Rails.application.config.assets.digest
40+
modules = requirejs.build_config['modules'].map { |m| requirejs.module_name_for m }
4441

45-
# Generate digestified paths from the modules spec
46-
paths = {}
47-
modules.each { |m| paths[m] = javascript_path(m).sub /\.js$/,'' }
42+
# Generate digestified paths from the modules spec
43+
paths = {}
44+
modules.each { |m| paths[m] = javascript_path(m).sub /\.js$/,'' }
4845

49-
# Override uesr paths, whose mappings are only relevant in dev mode
50-
# and in the build_config.
51-
run_config['paths'] = paths
46+
# Override uesr paths, whose mappings are only relevant in dev mode
47+
# and in the build_config.
48+
run_config['paths'] = paths
49+
end
50+
html.concat <<-HTML
51+
<script>var require = #{run_config.to_json};</script>
52+
HTML
5253
end
54+
5355
html.concat <<-HTML
54-
<script>var require = #{run_config.to_json};</script>
56+
<script #{_requirejs_data(name, &block)} src="#{javascript_path 'require.js'}"></script>
5557
HTML
58+
59+
html.html_safe
60+
end
61+
end
62+
63+
def _once_guard
64+
if defined?(controller) && controller.requirejs_included
65+
raise Requirejs::MultipleIncludeError, "Only one requirejs_include_tag allowed per page."
5666
end
5767

58-
html.concat <<-HTML
59-
<script #{_requirejs_data(name, &block)} src="#{javascript_path 'require.js'}"></script>
60-
HTML
68+
retval = yield
6169

62-
controller.requirejs_included = true
63-
html.html_safe
70+
controller.requirejs_included = true if defined?(controller)
71+
retval
6472
end
6573

6674
def _almond_include_tag(name, &block)

0 commit comments

Comments
 (0)