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

Commit 913f599

Browse files
committed
Add the Requirejs::ViewProxy class as a fallback for when RequirejsHelper is mixed into something that doesn't provide the full functionality of Rails views
1 parent 18e6405 commit 913f599

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

app/helpers/requirejs_helper.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'requirejs/error'
1+
require "requirejs/error"
2+
require "requirejs/rails/view_proxy"
23

34
module RequirejsHelper
45
# EXPERIMENTAL: Additional priority settings appended to
@@ -7,7 +8,7 @@ module RequirejsHelper
78
mattr_accessor :_priority
89
@@_priority = []
910

10-
def requirejs_include_tag(name=nil, &block)
11+
def requirejs_include_tag(name = nil, &block)
1112
requirejs = Rails.application.config.requirejs
1213

1314
if requirejs.loader == :almond
@@ -75,11 +76,19 @@ def requirejs_include_tag(name=nil, &block)
7576
end
7677
end
7778

78-
def javascript_path(name, options = {})
79+
def javascript_path(source, options = {})
7980
if defined?(super)
8081
super
8182
else
82-
"/assets/#{name}"
83+
view_proxy.javascript_path(source, options)
84+
end
85+
end
86+
87+
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
88+
if defined?(super) && respond_to?(:output_buffer) && respond_to?(:output_buffer=)
89+
super
90+
else
91+
view_proxy.content_tag(name, content_or_options_with_block, options, escape, &block)
8392
end
8493
end
8594

@@ -106,4 +115,8 @@ def base_url(js_asset)
106115
asset_host = uri.host && js_asset_path.sub(uri.request_uri, '')
107116
[asset_host, Rails.application.config.relative_url_root, Rails.application.config.assets.prefix].join
108117
end
118+
119+
def view_proxy
120+
@view_proxy ||= Requirejs::Rails::ViewProxy.new
121+
end
109122
end

lib/requirejs/rails/view_proxy.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Requirejs
2+
module Rails
3+
class ViewProxy
4+
include ActionView::Context
5+
include ActionView::Helpers::AssetUrlHelper
6+
include ActionView::Helpers::TagHelper
7+
end
8+
end
9+
end

test/requirejs-rails_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RequirejsRailsTest < ActiveSupport::TestCase
1717
end
1818

1919
test "CHANGELOG up to date" do
20-
changelog_match = (/^# v#{Requirejs::Rails::Version}/ =~ Pathname.new(__FILE__+'/../../CHANGELOG.md').cleanpath.read)
20+
changelog_match = (/^### v#{Requirejs::Rails::Version}/ =~ Pathname.new(__FILE__+'/../../CHANGELOG.md').cleanpath.read)
2121
assert changelog_match, "CHANGELOG has no section for v#{Requirejs::Rails::Version}"
2222
end
2323
end

0 commit comments

Comments
 (0)