Skip to content

Commit f0ceb99

Browse files
committed
Update RJS for Rails 4. Fix test suite.
1 parent bac2352 commit f0ceb99

File tree

8 files changed

+67
-67
lines changed

8 files changed

+67
-67
lines changed

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require 'rake/testtask'
33
task :default => :test
44

55
Rake::TestTask.new do |t|
6-
t.libs << 'test'
6+
t.libs << 'test/lib'
77
t.test_files = Dir.glob('test/*_test.rb') + Dir.glob('test/{controller,template}/**/*_test.rb')
88
t.warning = true
99
t.verbose = true

lib/action_view/helpers/prototype_helper.rb

+33-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
require 'active_support/core_ext/string/output_safety'
55

66
module ActionView
7+
# A string that returns itself as its JSON-encoded form.
8+
class JsonLiteral < String
9+
def as_json(options = nil) self end #:nodoc:
10+
def encode_json(encoder) self end #:nodoc:
11+
end
12+
713
# = Action View Prototype Helpers
814
module Helpers
915
# Prototype[http://www.prototypejs.org/] is a JavaScript library that provides
@@ -147,6 +153,7 @@ def remote_function(options)
147153
class JavaScriptGenerator #:nodoc:
148154
def initialize(context, &block) #:nodoc:
149155
@context, @lines = context, []
156+
def @lines.encoding() last.to_s.encoding end
150157
include_helpers_from_context
151158
@context.with_output_buffer(@lines) do
152159
@context.instance_exec(self, &block)
@@ -155,7 +162,7 @@ def initialize(context, &block) #:nodoc:
155162

156163
private
157164
def include_helpers_from_context
158-
extend @context.helpers if @context.respond_to?(:helpers) && @context.helpers
165+
extend @context.controller._helpers if @context.controller.respond_to?(:_helpers) && @context.controller._helpers
159166
extend GeneratorMethods
160167
end
161168

@@ -253,14 +260,21 @@ def [](id)
253260
when String, Symbol, NilClass
254261
JavaScriptElementProxy.new(self, id)
255262
else
256-
JavaScriptElementProxy.new(self, ActionController::RecordIdentifier.dom_id(id))
263+
JavaScriptElementProxy.new(self, RecordIdentifier.dom_id(id))
257264
end
258265
end
259266

267+
RecordIdentifier =
268+
if defined? ActionView::RecordIdentifier
269+
ActionView::RecordIdentifier
270+
else
271+
ActionController::RecordIdentifier
272+
end
273+
260274
# Returns an object whose <tt>to_json</tt> evaluates to +code+. Use this to pass a literal JavaScript
261275
# expression as an argument to another JavaScriptGenerator method.
262276
def literal(code)
263-
::ActiveSupport::JSON::Variable.new(code.to_s)
277+
JsonLiteral.new(code.to_s)
264278
end
265279

266280
# Returns a collection reference by finding it through a CSS +pattern+ in the DOM. This collection can then be
@@ -517,6 +531,15 @@ def delay(seconds = 1)
517531
record "}, #{(seconds * 1000).to_i})"
518532
end
519533

534+
def javascript_object_for(object)
535+
::ActiveSupport::JSON.encode(object)
536+
end
537+
538+
def arguments_for_call(arguments, block = nil)
539+
arguments << block_to_function(block) if block
540+
arguments.map { |argument| javascript_object_for(argument) }.join ', '
541+
end
542+
520543
private
521544
def loop_on_multiple_args(method, ids)
522545
record(ids.size>1 ?
@@ -547,7 +570,7 @@ def render(*options)
547570

548571
def with_formats(*args)
549572
return yield unless @context
550-
573+
551574
lookup = @context.lookup_context
552575
begin
553576
old_formats, lookup.formats = lookup.formats, args
@@ -557,15 +580,6 @@ def with_formats(*args)
557580
end
558581
end
559582

560-
def javascript_object_for(object)
561-
::ActiveSupport::JSON.encode(object)
562-
end
563-
564-
def arguments_for_call(arguments, block = nil)
565-
arguments << block_to_function(block) if block
566-
arguments.map { |argument| javascript_object_for(argument) }.join ', '
567-
end
568-
569583
def block_to_function(block)
570584
generator = self.class.new(@context, &block)
571585
literal("function() { #{generator.to_s} }")
@@ -655,7 +669,6 @@ def build_callbacks(options)
655669

656670
# Converts chained method calls on DOM proxy elements into JavaScript chains
657671
class JavaScriptProxy < ActiveSupport::ProxyObject #:nodoc:
658-
659672
def initialize(generator, root = nil)
660673
@generator = generator
661674
@generator << root if root
@@ -730,7 +743,7 @@ def reload(options_for_replace = {})
730743

731744
class JavaScriptVariableProxy < JavaScriptProxy #:nodoc:
732745
def initialize(generator, variable)
733-
@variable = ::ActiveSupport::JSON::Variable.new(variable)
746+
@variable = JsonLiteral.new(variable)
734747
@empty = true # only record lines if we have to. gets rid of unnecessary linebreaks
735748
super(generator)
736749
end
@@ -754,8 +767,8 @@ def append_to_function_chain!(call)
754767
end
755768

756769
class JavaScriptCollectionProxy < JavaScriptProxy #:nodoc:
757-
ENUMERABLE_METHODS_WITH_RETURN = [:all, :any, :collect, :map, :detect, :find, :find_all, :select, :max, :min, :partition, :reject, :sort_by, :in_groups_of, :each_slice] unless defined? ENUMERABLE_METHODS_WITH_RETURN
758-
ENUMERABLE_METHODS = ENUMERABLE_METHODS_WITH_RETURN + [:each] unless defined? ENUMERABLE_METHODS
770+
ENUMERABLE_METHODS_WITH_RETURN = [:all, :any, :collect, :map, :detect, :find, :find_all, :select, :max, :min, :partition, :reject, :sort_by, :in_groups_of, :each_slice]
771+
ENUMERABLE_METHODS = ENUMERABLE_METHODS_WITH_RETURN + [:each]
759772
attr_reader :generator
760773
delegate :arguments_for_call, :to => :generator
761774

@@ -773,7 +786,7 @@ def each_slice(variable, number, &block)
773786
end
774787

775788
def grep(variable, pattern, &block)
776-
enumerate :grep, :variable => variable, :return => true, :method_args => [::ActiveSupport::JSON::Variable.new(pattern.inspect)], :yield_args => %w(value index), &block
789+
enumerate :grep, :variable => variable, :return => true, :method_args => [JsonLiteral.new(pattern.inspect)], :yield_args => %w(value index), &block
777790
end
778791

779792
def in_groups_of(variable, number, fill_with = nil)
@@ -797,7 +810,7 @@ def zip(variable, *arguments, &block)
797810
append_enumerable_function!("zip(#{arguments_for_call arguments}")
798811
if block
799812
function_chain[-1] += ", function(array) {"
800-
yield ::ActiveSupport::JSON::Variable.new('array')
813+
yield JsonLiteral.new('array')
801814
add_return_statement!
802815
@generator << '});'
803816
else
@@ -810,7 +823,7 @@ def method_missing(method, *arguments, &block)
810823
if ENUMERABLE_METHODS.include?(method)
811824
returnable = ENUMERABLE_METHODS_WITH_RETURN.include?(method)
812825
variable = arguments.first if returnable
813-
enumerate(method, {:variable => (arguments.first if returnable), :return => returnable, :yield_args => %w(value index)}, &block)
826+
enumerate(method, {:variable => variable, :return => returnable, :yield_args => %w(value index)}, &block)
814827
else
815828
super
816829
end

lib/prototype-rails/javascript_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
ActionView::Helpers::JavaScriptHelper.module_eval do
44
include ActionView::Helpers::PrototypeHelper
5+
undef_method :button_to_function
6+
undef_method :link_to_function
57

68
# Returns a button with the given +name+ text that'll trigger a JavaScript +function+ using the
79
# onclick handler.

lib/prototype-rails/selector_assertions.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def assert_select_rjs(*args, &block)
124124
end
125125

126126
if matches
127-
assert_block("") { true } # to count the assertion
127+
assert true # to count the assertion
128128
if block_given? && !([:remove, :show, :hide, :toggle].include? rjs_type)
129129
begin
130130
@selected ||= nil

test/controller/caching_test.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
require 'fileutils'
21
require 'abstract_unit'
32

4-
CACHE_DIR = 'test_cache'
53
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
6-
FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
7-
ActionController::Base.page_cache_directory = FILE_STORE_PATH
4+
FILE_STORE_PATH = File.expand_path('../../../temp/test_cache', __FILE__)
85

96
class CachingController < ActionController::Base
107
abstract!
@@ -41,6 +38,6 @@ def test_fragment_caching_in_rjs_partials
4138
xhr :get, :js_fragment_cached_with_partial
4239
assert_response :success
4340
assert_match(/Old fragment caching in a partial/, @response.body)
44-
assert_match("Old fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial'))
41+
assert_match "Old fragment caching in a partial", @store.instance_variable_get('@data').values.first.value
4542
end
46-
end
43+
end

test/lib/abstract_unit.rb

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
2-
$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
1+
require 'rubygems'
2+
require 'bundler/setup'
3+
require 'minitest/autorun'
34

4-
$:.unshift(File.dirname(__FILE__) + '/lib')
5-
6-
if defined? Gem
7-
Gem.source_index
8-
gem 'bundler'
9-
else
10-
require 'rubygems'
11-
end
12-
require 'bundler'
13-
Bundler.setup
14-
15-
require 'test/unit'
165
require 'active_support'
6+
require 'active_support/test_case'
177
require 'action_controller'
188
require 'action_view'
199
require 'action_view/testing/resolvers'
2010

2111
require 'prototype-rails/on_load_action_controller'
2212
require 'prototype-rails/on_load_action_view'
2313

24-
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
14+
FIXTURE_LOAD_PATH = File.expand_path('../../fixtures', __FILE__)
2515
FIXTURES = Pathname.new(FIXTURE_LOAD_PATH)
2616

2717

@@ -59,11 +49,11 @@ class TestCase
5949
# have been loaded.
6050
setup_once do
6151
SharedTestRoutes.draw do
62-
match ':controller(/:action)'
52+
get ':controller(/:action)'
6353
end
6454

6555
ActionDispatch::IntegrationTest.app.routes.draw do
66-
match ':controller(/:action)'
56+
get ':controller(/:action)'
6757
end
6858
end
6959
end
@@ -88,7 +78,7 @@ class BasicController
8878
def config
8979
@config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config).tap do |config|
9080
# VIEW TODO: View tests should not require a controller
91-
public_dir = File.expand_path("../fixtures/public", __FILE__)
81+
public_dir = "#{FIXTURE_LOAD_PATH}/public"
9282
config.assets_dir = public_dir
9383
config.javascripts_dir = "#{public_dir}/javascripts"
9484
config.stylesheets_dir = "#{public_dir}/stylesheets"
@@ -109,7 +99,7 @@ def self.build_app(routes = nil)
10999
middleware.use "ActionDispatch::ParamsParser"
110100
middleware.use "ActionDispatch::Cookies"
111101
middleware.use "ActionDispatch::Flash"
112-
middleware.use "ActionDispatch::Head"
102+
middleware.use "Rack::Head"
113103
yield(middleware) if block_given?
114104
end
115105
end

test/render_other_test.rb

+7-9
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,15 @@ def setup
162162
end
163163

164164
def test_enum_rjs_test
165-
SecureRandom.stubs(:base64).returns("asdf")
166-
get :enum_rjs_test
167-
body = %{
168-
$$(".product").each(function(value, index) {
165+
pre = %[$$(".product").each(function(value, index) {
169166
new Effect.Highlight(element,{});
170167
new Effect.Highlight(value,{});
171-
Sortable.create(value, {onUpdate:function(){new Ajax.Request('/render_other_test/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value) + '&authenticity_token=' + encodeURIComponent('asdf')})}});
172-
new Draggable(value, {});
173-
});
174-
}.gsub(/^ /, '').strip
175-
assert_equal body, @response.body
168+
Sortable.create(value, {onUpdate:function(){new Ajax.Request('/render_other_test/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value) + '&authenticity_token=' + encodeURIComponent('].gsub(/^\s+/, '')
169+
post = %[')})}});\nnew Draggable(value, {});\n});]
170+
171+
get :enum_rjs_test
172+
assert_match pre, @response.body
173+
assert_match post, @response.body
176174
end
177175

178176
def test_explicitly_rendering_an_html_template_with_implicit_html_template_renders_should_be_possible_from_an_rjs_template

test/template/scriptaculous_helper_test.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,33 @@ def test_toggle_effects
5050

5151

5252
def test_sortable_element
53-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>),
53+
assert_dom_equal %(<script>\n//<![CDATA[\nSortable.create(\"mylist\", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>),
5454
sortable_element("mylist", :url => { :action => "order" })
55-
assert_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}, tag:'div'})\n//]]>\n</script>),
55+
assert_equal %(<script>\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}, tag:'div'})\n//]]>\n</script>),
5656
sortable_element("mylist", :tag => "div", :constraint => "horizontal", :url => { :action => "order" })
57-
assert_dom_equal %|<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:['list1','list2'], onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>|,
57+
assert_dom_equal %|<script>\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:['list1','list2'], onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>|,
5858
sortable_element("mylist", :containment => ['list1','list2'], :constraint => "horizontal", :url => { :action => "order" })
59-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:'list1', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>),
59+
assert_dom_equal %(<script>\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:'list1', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>),
6060
sortable_element("mylist", :containment => 'list1', :constraint => "horizontal", :url => { :action => "order" })
6161
end
6262

6363
def test_draggable_element
64-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Draggable(\"product_13\", {})\n//]]>\n</script>),
64+
assert_dom_equal %(<script>\n//<![CDATA[\nnew Draggable(\"product_13\", {})\n//]]>\n</script>),
6565
draggable_element("product_13")
66-
assert_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Draggable(\"product_13\", {revert:true})\n//]]>\n</script>),
66+
assert_equal %(<script>\n//<![CDATA[\nnew Draggable(\"product_13\", {revert:true})\n//]]>\n</script>),
6767
draggable_element("product_13", :revert => true)
6868
end
6969

7070
def test_drop_receiving_element
71-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nDroppables.add(\"droptarget1\", {onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
71+
assert_dom_equal %(<script>\n//<![CDATA[\nDroppables.add(\"droptarget1\", {onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
7272
drop_receiving_element("droptarget1")
73-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nDroppables.add(\"droptarget1\", {accept:'products', onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
73+
assert_dom_equal %(<script>\n//<![CDATA[\nDroppables.add(\"droptarget1\", {accept:'products', onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
7474
drop_receiving_element("droptarget1", :accept => 'products')
75-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nDroppables.add(\"droptarget1\", {accept:'products', onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
75+
assert_dom_equal %(<script>\n//<![CDATA[\nDroppables.add(\"droptarget1\", {accept:'products', onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
7676
drop_receiving_element("droptarget1", :accept => 'products', :update => 'infobox')
77-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nDroppables.add(\"droptarget1\", {accept:['tshirts','mugs'], onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
77+
assert_dom_equal %(<script>\n//<![CDATA[\nDroppables.add(\"droptarget1\", {accept:['tshirts','mugs'], onDrop:function(element){new Ajax.Updater('infobox', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>),
7878
drop_receiving_element("droptarget1", :accept => ['tshirts','mugs'], :update => 'infobox')
79-
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nDroppables.add("droptarget1", {hoverclass:'dropready', onDrop:function(element){if (confirm('Are you sure?')) { new Ajax.Request('http://www.example.com/update_drop', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)}); }}})\n//]]>\n</script>),
79+
assert_dom_equal %(<script>\n//<![CDATA[\nDroppables.add("droptarget1", {hoverclass:'dropready', onDrop:function(element){if (confirm('Are you sure?')) { new Ajax.Request('http://www.example.com/update_drop', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)}); }}})\n//]]>\n</script>),
8080
drop_receiving_element('droptarget1', :hoverclass=>'dropready', :url=>{:action=>'update_drop'}, :confirm => 'Are you sure?')
8181

8282
end

0 commit comments

Comments
 (0)