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

Commit

Permalink
Add the Requirejs::ViewProxy class as a fallback for when `Requirej…
Browse files Browse the repository at this point in the history
…sHelper` is mixed into something that doesn't provide the full functionality of Rails views
  • Loading branch information
carsomyr committed Sep 21, 2014
1 parent 18e6405 commit 913f599
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
21 changes: 17 additions & 4 deletions app/helpers/requirejs_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'requirejs/error'
require "requirejs/error"
require "requirejs/rails/view_proxy"

module RequirejsHelper
# EXPERIMENTAL: Additional priority settings appended to
Expand All @@ -7,7 +8,7 @@ module RequirejsHelper
mattr_accessor :_priority
@@_priority = []

def requirejs_include_tag(name=nil, &block)
def requirejs_include_tag(name = nil, &block)
requirejs = Rails.application.config.requirejs

if requirejs.loader == :almond
Expand Down Expand Up @@ -75,11 +76,19 @@ def requirejs_include_tag(name=nil, &block)
end
end

def javascript_path(name, options = {})
def javascript_path(source, options = {})
if defined?(super)
super
else
"/assets/#{name}"
view_proxy.javascript_path(source, options)
end
end

def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
if defined?(super) && respond_to?(:output_buffer) && respond_to?(:output_buffer=)
super
else
view_proxy.content_tag(name, content_or_options_with_block, options, escape, &block)
end
end

Expand All @@ -106,4 +115,8 @@ def base_url(js_asset)
asset_host = uri.host && js_asset_path.sub(uri.request_uri, '')
[asset_host, Rails.application.config.relative_url_root, Rails.application.config.assets.prefix].join
end

def view_proxy
@view_proxy ||= Requirejs::Rails::ViewProxy.new
end
end
9 changes: 9 additions & 0 deletions lib/requirejs/rails/view_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Requirejs
module Rails
class ViewProxy
include ActionView::Context
include ActionView::Helpers::AssetUrlHelper
include ActionView::Helpers::TagHelper
end
end
end
2 changes: 1 addition & 1 deletion test/requirejs-rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RequirejsRailsTest < ActiveSupport::TestCase
end

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

0 comments on commit 913f599

Please sign in to comment.