de_rjs 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/README +3 -0
- data/Rakefile +10 -0
- data/bin/de_rjs +6 -0
- data/lib/de_rjs.rb +12 -0
- data/lib/de_rjs/jquery_generator.rb +789 -0
- data/lib/de_rjs/rewriter.rb +17 -0
- data/lib/de_rjs/rewriter/erbify.rb +104 -0
- data/lib/de_rjs/runner.rb +26 -0
- data/test/abstract_unit.rb +13 -0
- data/test/de_rjs_test.rb +432 -0
- metadata +107 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'parser/current'
|
2
|
+
require_relative 'rewriter/erbify'
|
3
|
+
|
4
|
+
module DeRjs
|
5
|
+
module Rewriter
|
6
|
+
def rewrite_rjs(source)
|
7
|
+
buffer = Parser::Source::Buffer.new("buffer_name")
|
8
|
+
buffer.source = source
|
9
|
+
parser = Parser::CurrentRuby.new
|
10
|
+
ast = parser.parse(buffer)
|
11
|
+
rewriter = Erbify.new
|
12
|
+
|
13
|
+
rewriter.rewrite(buffer, ast)
|
14
|
+
end
|
15
|
+
module_function :rewrite_rjs
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module DeRjs
|
2
|
+
module Rewriter
|
3
|
+
class Erbify < Parser::Rewriter
|
4
|
+
class MustTranslateManually < StandardError; end
|
5
|
+
|
6
|
+
def on_send(node)
|
7
|
+
receiver_node, method_name, *arg_nodes = *node
|
8
|
+
|
9
|
+
if receiver_node.to_a.last == :page
|
10
|
+
case method_name
|
11
|
+
when :replace_html, :replace
|
12
|
+
rewrite_replace_html(receiver_node, method_name, *arg_nodes)
|
13
|
+
|
14
|
+
when :insert_html
|
15
|
+
rewrite_insert_html(receiver_node, method_name, *arg_nodes)
|
16
|
+
|
17
|
+
when :redirect_to
|
18
|
+
rewrite_redirect_to(receiver_node, method_name, *arg_nodes)
|
19
|
+
|
20
|
+
when :[]
|
21
|
+
rewrite_square_bracket(receiver_node, method_name, *arg_nodes)
|
22
|
+
|
23
|
+
else
|
24
|
+
# All others such as :
|
25
|
+
# page.alert
|
26
|
+
# page.hide
|
27
|
+
# page.redirect_to
|
28
|
+
# page.reload
|
29
|
+
# page.replace
|
30
|
+
# page.select
|
31
|
+
# page.show
|
32
|
+
# page.visual_effect
|
33
|
+
rewrite_all_args(receiver_node, method_name, *arg_nodes)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# page[:html_id].some_method
|
38
|
+
if receiver_node.to_a.first.to_a.last == :page && receiver_node.to_a[1] == :[]
|
39
|
+
rewrite_square_bracket(*receiver_node.to_a)
|
40
|
+
rewrite_square_replace(receiver_node, method_name, *arg_nodes) if [:replace, :replace_html].include?(method_name)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def rewrite_all_args(receiver_node, method_name, *arg_nodes)
|
45
|
+
arg_nodes.each {|arg_node| rewrite_to_erb_unless_static(arg_node) }
|
46
|
+
end
|
47
|
+
|
48
|
+
# id, *options_for_render
|
49
|
+
def rewrite_replace_html(receiver_node, method_name, *arg_nodes)
|
50
|
+
rewrite_to_erb_unless_static(arg_nodes.shift)
|
51
|
+
rewrite_options_for_render(arg_nodes)
|
52
|
+
end
|
53
|
+
|
54
|
+
# position, id, *options_for_render
|
55
|
+
def rewrite_insert_html(receiver_node, method_name, *arg_nodes)
|
56
|
+
rewrite_to_erb_unless_static(arg_nodes.shift)
|
57
|
+
rewrite_to_erb_unless_static(arg_nodes.shift)
|
58
|
+
rewrite_options_for_render(arg_nodes)
|
59
|
+
end
|
60
|
+
|
61
|
+
# location (string, or url_for compatible options)
|
62
|
+
def rewrite_redirect_to(receiver_node, method_name, *arg_nodes)
|
63
|
+
rewrite_url_for(arg_nodes.first)
|
64
|
+
end
|
65
|
+
|
66
|
+
# e.g. page["sgfg"] or page["wat_#{@id}"]
|
67
|
+
#
|
68
|
+
# But this will raise when it encounters e.g. page[@var] or page[Bunny.new]
|
69
|
+
# While this is NOT accurate, throwing an error seems better than trying to evaluating
|
70
|
+
# a turing complete statement, and causing subtle failures downstream
|
71
|
+
def rewrite_square_bracket(receiver_node, method_name, *arg_nodes)
|
72
|
+
unless [:str, :sym, :dstr].include?(arg_nodes.first.type)
|
73
|
+
raise MustTranslateManually, "Unable to compute type of expression statically:#{arg_nodes.first.inspect}\n -" \
|
74
|
+
"`def id[]` supports String, Symbol, NilClass or something that `dom_id` can understand"
|
75
|
+
end
|
76
|
+
rewrite_to_erb_unless_static(arg_nodes.shift)
|
77
|
+
end
|
78
|
+
|
79
|
+
# *options_for_render
|
80
|
+
def rewrite_square_replace(receiver_node, method_name, *arg_nodes)
|
81
|
+
rewrite_options_for_render(arg_nodes)
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
def rewrite_to_erb_unless_static(id_arg)
|
86
|
+
return if [:str, :sym].include?(id_arg.type)
|
87
|
+
insert_before id_arg.loc.expression, "%q{<%= "
|
88
|
+
insert_after id_arg.loc.expression, " %>}"
|
89
|
+
end
|
90
|
+
|
91
|
+
def rewrite_options_for_render(arg_nodes)
|
92
|
+
return if arg_nodes.size == 1 && arg_nodes.first.type == :str
|
93
|
+
insert_before arg_nodes.first.loc.expression, "%q{<%= escape_javascript(render("
|
94
|
+
insert_after arg_nodes.last.loc.expression, ")) %>}"
|
95
|
+
end
|
96
|
+
|
97
|
+
def rewrite_url_for(url_arg)
|
98
|
+
return if url_arg.type == :str
|
99
|
+
insert_before url_arg.loc.expression, "%q{<%= url_for("
|
100
|
+
insert_after url_arg.loc.expression, ") %>}"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'de_rjs'
|
2
|
+
|
3
|
+
module DeRjs
|
4
|
+
class Runner
|
5
|
+
attr_reader :filenames
|
6
|
+
|
7
|
+
def initialize(filenames)
|
8
|
+
@filenames = filenames
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute
|
12
|
+
filenames.each do |filename|
|
13
|
+
source = File.read filename
|
14
|
+
js_erb = rewrite_to_js_erb(source)
|
15
|
+
File.open(filename, "w") {|f| f << js_erb}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def rewrite_to_js_erb(rjs)
|
21
|
+
rewritten_source = DeRjs::Rewriter.rewrite_rjs(rjs)
|
22
|
+
generator = DeRjs::JqueryGenerator.new(nil) { eval(rewritten_source)}
|
23
|
+
generator.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
|
2
|
+
$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
|
3
|
+
|
4
|
+
$:.unshift(File.dirname(__FILE__) + '/lib')
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'bundler/setup'
|
8
|
+
require "minitest/autorun"
|
9
|
+
require 'active_support'
|
10
|
+
require 'action_controller'
|
11
|
+
require 'action_view'
|
12
|
+
|
13
|
+
require 'de_rjs'
|
data/test/de_rjs_test.rb
ADDED
@@ -0,0 +1,432 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_model'
|
3
|
+
|
4
|
+
class Bunny < Struct.new(:Bunny, :id)
|
5
|
+
extend ActiveModel::Naming
|
6
|
+
include ActiveModel::Conversion
|
7
|
+
def to_key() id ? [id] : nil end
|
8
|
+
end
|
9
|
+
|
10
|
+
class DeRjsBaseTest < Minitest::Test
|
11
|
+
protected
|
12
|
+
def not_supported
|
13
|
+
skip "not supported"
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_generator
|
17
|
+
block = Proc.new { |*args| yield(*args) if block_given? }
|
18
|
+
DeRjs::JqueryGenerator.new self, &block
|
19
|
+
end
|
20
|
+
|
21
|
+
def generate_js(rjs)
|
22
|
+
rewritten_source = DeRjs::Rewriter.rewrite_rjs(rjs)
|
23
|
+
generator = DeRjs::JqueryGenerator.new(nil) { eval(rewritten_source)}
|
24
|
+
generator.to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
class DeRjsTest < DeRjsBaseTest
|
30
|
+
def setup
|
31
|
+
super
|
32
|
+
@generator = create_generator
|
33
|
+
ActiveSupport.escape_html_entities_in_json = true
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
ActiveSupport.escape_html_entities_in_json = false
|
38
|
+
end
|
39
|
+
|
40
|
+
def _evaluate_assigns_and_ivars() end
|
41
|
+
|
42
|
+
def test_insert_html_with_string
|
43
|
+
assert_equal '$("#element").prepend("\\u003cp\\u003eThis is a test\\u003c/p\\u003e");',
|
44
|
+
generate_js(%Q{ page.insert_html(:top, 'element', '<p>This is a test</p>') })
|
45
|
+
assert_equal '$("#element").append("\\u003cp\u003eThis is a test\\u003c/p\u003e");',
|
46
|
+
generate_js(%Q{ page.insert_html(:bottom, 'element', '<p>This is a test</p>') })
|
47
|
+
assert_equal '$("#element").before("\\u003cp\u003eThis is a test\\u003c/p\u003e");',
|
48
|
+
generate_js(%Q{ page.insert_html(:before, 'element', '<p>This is a test</p>') })
|
49
|
+
assert_equal '$("#element").after("\\u003cp\u003eThis is a test\\u003c/p\u003e");',
|
50
|
+
generate_js(%Q{ page.insert_html(:after, 'element', '<p>This is a test</p>') })
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_replace_html_with_string
|
54
|
+
assert_equal '$("#element").html("\\u003cp\\u003eThis is a test\\u003c/p\\u003e");',
|
55
|
+
generate_js(%Q{ page.replace_html('element', '<p>This is a test</p>') })
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_replace_element_with_string
|
59
|
+
assert_equal '$("#element").replaceWith("\\u003cdiv id=\"element\"\\u003e\\u003cp\\u003eThis is a test\\u003c/p\\u003e\\u003c/div\\u003e");',
|
60
|
+
generate_js(%Q{ page.replace('element', '<div id="element"><p>This is a test</p></div>') })
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_insert_html_with_hash
|
64
|
+
assert_equal '$("#element").prepend("<%= escape_javascript(render(:partial => "post", :locals => {:ab => "cd"})) %>");',
|
65
|
+
generate_js(%Q{ page.insert_html(:top, 'element', :partial => "post", :locals => {:ab => "cd"}) })
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_replace_html_with_hash
|
69
|
+
assert_equal '$("#element").html("<%= escape_javascript(render(:partial => "post", :locals => {:ab => "cd"})) %>");',
|
70
|
+
generate_js(%Q{ page.replace_html('element', :partial => "post", :locals => {:ab => "cd"}) })
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_replace_element_with_hash
|
74
|
+
assert_equal '$("#element").replaceWith("<%= escape_javascript(render(:partial => "post", :locals => {:ab => "cd"})) %>");',
|
75
|
+
generate_js(%Q{ page.replace('element', :partial => "post", :locals => {:ab => "cd"}) })
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
def test_remove
|
80
|
+
assert_equal '$("#foo").remove();',
|
81
|
+
generate_js(%Q{ page.remove('foo') })
|
82
|
+
assert_equal '$("#foo,#bar,#baz").remove();',
|
83
|
+
generate_js(%Q{ page.remove('foo', 'bar', 'baz') })
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_show
|
87
|
+
assert_equal '$("#foo").show();',
|
88
|
+
generate_js(%Q{ page.show('foo') })
|
89
|
+
assert_equal '$("#foo,#bar,#baz").show();',
|
90
|
+
generate_js(%Q{ page.show('foo', 'bar', 'baz') })
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_hide
|
94
|
+
assert_equal '$("#foo").hide();',
|
95
|
+
generate_js(%Q{ page.hide('foo') })
|
96
|
+
assert_equal '$("#foo,#bar,#baz").hide();',
|
97
|
+
generate_js(%Q{ page.hide('foo', 'bar', 'baz') })
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_toggle
|
101
|
+
assert_equal '$("#foo").toggle();',
|
102
|
+
generate_js(%Q{ page.toggle('foo') })
|
103
|
+
assert_equal '$("#foo,#bar,#baz").toggle();',
|
104
|
+
generate_js(%Q{ page.toggle('foo', 'bar', 'baz') })
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_alert
|
108
|
+
assert_equal 'alert("hello");', generate_js(%Q{ page.alert('hello') })
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_redirect_to
|
112
|
+
assert_equal 'window.location.href = "http://www.example.com/welcome?a=b&c=d";',
|
113
|
+
generate_js(%Q{ page.redirect_to("http://www.example.com/welcome?a=b&c=d") })
|
114
|
+
assert_equal 'window.location.href = "<%= url_for(:action => \'welcome\') %>";',
|
115
|
+
generate_js(%Q{ page.redirect_to(:action => 'welcome') })
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_reload
|
119
|
+
assert_equal 'window.location.reload();',
|
120
|
+
generate_js(%Q{ page.reload })
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_delay
|
124
|
+
not_supported
|
125
|
+
|
126
|
+
@generator.delay(20) do
|
127
|
+
@generator.hide('foo')
|
128
|
+
end
|
129
|
+
|
130
|
+
assert_equal "setTimeout(function() {\n;\n$(\"#foo\").hide();\n}, 20000);", @generator.to_s
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_to_s
|
134
|
+
not_supported
|
135
|
+
|
136
|
+
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
|
137
|
+
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
|
138
|
+
@generator.remove('foo', 'bar')
|
139
|
+
@generator.replace_html('baz', '<p>This is a test</p>')
|
140
|
+
|
141
|
+
assert_equal <<-EOS.chomp, @generator.to_s
|
142
|
+
$("#element").prepend("\\u003cp\\u003eThis is a test\\u003c/p\\u003e");
|
143
|
+
$("#element").append("\\u003cp\\u003eThis is a test\\u003c/p\\u003e");
|
144
|
+
$("#foo,#bar").remove();
|
145
|
+
$("#baz").html("\\u003cp\\u003eThis is a test\\u003c/p\\u003e");
|
146
|
+
EOS
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_element_access
|
150
|
+
assert_equal %($("#hello");), generate_js(%Q{ page['hello'] })
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_element_access_on_variable
|
154
|
+
assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
|
155
|
+
assert_equal %($("#<%= 'hello' + @var %>");), generate_js(%Q{ page['hello' + @var] })
|
156
|
+
end
|
157
|
+
assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
|
158
|
+
assert_equal %($("#<%= 'hello' + @var %>").hide();), generate_js(%Q{ page['hello' + @var].hide })
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_element_access_on_records
|
163
|
+
assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
|
164
|
+
assert_equal %($("#<%= Bunny.new(:id => 5) %>");), generate_js(%Q{ page[Bunny.new(:id => 5)] })
|
165
|
+
end
|
166
|
+
assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
|
167
|
+
assert_equal %($("#<%= Bunny.new %>");), generate_js(%Q{ page[Bunny.new] })
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
def test_element_proxy_one_deep
|
173
|
+
assert_equal %($("#hello").hide();), generate_js(%Q{ page['hello'].hide })
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_element_proxy_variable_access
|
177
|
+
assert_equal %($("#hello").style;), generate_js(%Q{ page['hello']['style'] })
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_element_proxy_variable_access_with_assignment
|
181
|
+
assert_equal %($("#hello").style.color = "red";), generate_js(%Q{ page['hello']['style']['color'] = 'red' })
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_element_proxy_assignment
|
185
|
+
assert_equal %($("#hello").width = 400;), generate_js(%Q{ page['hello'].width = 400 })
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_element_proxy_two_deep
|
189
|
+
@generator['hello'].hide("first").clean_whitespace
|
190
|
+
assert_equal %($("#hello").hide("first").cleanWhitespace();), @generator.to_s
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_select_access
|
194
|
+
assert_equal %($("div.hello");), @generator.select('div.hello')
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_select_proxy_one_deep
|
198
|
+
assert_equal %($("p.welcome b").first().hide();), generate_js(%Q{ page.select('p.welcome b').first.hide })
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_visual_effect
|
202
|
+
assert_equal %($(\"#blah\").effect(\"puff\",{});),
|
203
|
+
generate_js(%Q{ page.visual_effect(:puff,'blah') })
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_visual_effect_toggle
|
207
|
+
assert_equal %($(\"#blah\").toggle(\"fade\",{});),
|
208
|
+
generate_js(%Q{ page.visual_effect(:toggle_appear,'blah') })
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_visual_effect_with_variable
|
212
|
+
assert_equal %($(\"#<%= "blah" + blah.id %>\").toggle(\"fade\",{});),
|
213
|
+
generate_js(%Q{ page.visual_effect(:toggle_appear,"blah" + blah.id) })
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_sortable
|
217
|
+
assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
|
218
|
+
@generator.sortable('blah', :url => { :action => "order" })
|
219
|
+
assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:false, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
|
220
|
+
@generator.sortable('blah', :url => { :action => "order" }, :type => :synchronous)
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_draggable
|
224
|
+
assert_equal %(new Draggable("blah", {});),
|
225
|
+
@generator.draggable('blah')
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_drop_receiving
|
229
|
+
assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
|
230
|
+
@generator.drop_receiving('blah', :url => { :action => "order" })
|
231
|
+
assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:false, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
|
232
|
+
@generator.drop_receiving('blah', :url => { :action => "order" }, :type => :synchronous)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_collection_first_and_last
|
236
|
+
js = generate_js(%Q{
|
237
|
+
page.select('p.welcome b').first.hide()
|
238
|
+
page.select('p.welcome b').last.show()
|
239
|
+
})
|
240
|
+
assert_equal <<-EOS.strip, js
|
241
|
+
$("p.welcome b").first().hide();
|
242
|
+
$("p.welcome b").last().show();
|
243
|
+
EOS
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_collection_proxy_with_each
|
247
|
+
not_supported
|
248
|
+
|
249
|
+
@generator.select('p.welcome b').each do |value|
|
250
|
+
value.remove_class_name 'selected'
|
251
|
+
end
|
252
|
+
@generator.select('p.welcome b').each do |value, index|
|
253
|
+
@generator.visual_effect :highlight, value
|
254
|
+
end
|
255
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
256
|
+
$("p.welcome b").each(function(value, index) {
|
257
|
+
value.removeClassName("selected");
|
258
|
+
});
|
259
|
+
$("p.welcome b").each(function(value, index) {
|
260
|
+
$("#value").effect("highlight",{});
|
261
|
+
});
|
262
|
+
EOS
|
263
|
+
end
|
264
|
+
|
265
|
+
def test_collection_proxy_on_collect
|
266
|
+
not_supported
|
267
|
+
|
268
|
+
@generator.select('p').collect('a') { |para| para.show }
|
269
|
+
@generator.select('p').collect { |para| para.hide }
|
270
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
271
|
+
var a = $("p").collect(function(value, index) {
|
272
|
+
return value.show();
|
273
|
+
});
|
274
|
+
$("p").collect(function(value, index) {
|
275
|
+
return value.hide();
|
276
|
+
});
|
277
|
+
EOS
|
278
|
+
@generator = create_generator
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_collection_proxy_with_grep
|
282
|
+
not_supported
|
283
|
+
|
284
|
+
@generator.select('p').grep 'a', /^a/ do |value|
|
285
|
+
@generator << '(value.className == "welcome")'
|
286
|
+
end
|
287
|
+
@generator.select('p').grep 'b', /b$/ do |value, index|
|
288
|
+
@generator.call 'alert', value
|
289
|
+
@generator << '(value.className == "welcome")'
|
290
|
+
end
|
291
|
+
|
292
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
293
|
+
var a = $("p").grep(/^a/, function(value, index) {
|
294
|
+
return (value.className == "welcome");
|
295
|
+
});
|
296
|
+
var b = $("p").grep(/b$/, function(value, index) {
|
297
|
+
alert(value);
|
298
|
+
return (value.className == "welcome");
|
299
|
+
});
|
300
|
+
EOS
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_collection_proxy_with_inject
|
304
|
+
not_supported
|
305
|
+
|
306
|
+
@generator.select('p').inject 'a', [] do |memo, value|
|
307
|
+
@generator << '(value.className == "welcome")'
|
308
|
+
end
|
309
|
+
@generator.select('p').inject 'b', nil do |memo, value, index|
|
310
|
+
@generator.call 'alert', memo
|
311
|
+
@generator << '(value.className == "welcome")'
|
312
|
+
end
|
313
|
+
|
314
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
315
|
+
var a = $("p").inject([], function(memo, value, index) {
|
316
|
+
return (value.className == "welcome");
|
317
|
+
});
|
318
|
+
var b = $("p").inject(null, function(memo, value, index) {
|
319
|
+
alert(memo);
|
320
|
+
return (value.className == "welcome");
|
321
|
+
});
|
322
|
+
EOS
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_collection_proxy_with_pluck
|
326
|
+
js = generate_js(%Q{ page.select('p').pluck('a', 'className') })
|
327
|
+
assert_equal %(var a = $("p").pluck("className");), js
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_collection_proxy_with_zip
|
331
|
+
not_supported
|
332
|
+
|
333
|
+
ActionView::Helpers::JavaScriptCollectionProxy.new(@generator, '[1, 2, 3]').zip('a', [4, 5, 6], [7, 8, 9])
|
334
|
+
ActionView::Helpers::JavaScriptCollectionProxy.new(@generator, '[1, 2, 3]').zip('b', [4, 5, 6], [7, 8, 9]) do |array|
|
335
|
+
@generator.call 'array.reverse'
|
336
|
+
end
|
337
|
+
|
338
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
339
|
+
var a = [1, 2, 3].zip([4,5,6], [7,8,9]);
|
340
|
+
var b = [1, 2, 3].zip([4,5,6], [7,8,9], function(array) {
|
341
|
+
return array.reverse();
|
342
|
+
});
|
343
|
+
EOS
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_collection_proxy_with_find_all
|
347
|
+
not_supported
|
348
|
+
|
349
|
+
@generator.select('p').find_all 'a' do |value, index|
|
350
|
+
@generator << '(value.className == "welcome")'
|
351
|
+
end
|
352
|
+
|
353
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
354
|
+
var a = $("p").findAll(function(value, index) {
|
355
|
+
return (value.className == "welcome");
|
356
|
+
});
|
357
|
+
EOS
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_collection_proxy_with_in_groups_of
|
361
|
+
not_supported
|
362
|
+
|
363
|
+
@generator.select('p').in_groups_of('a', 3)
|
364
|
+
@generator.select('p').in_groups_of('a', 3, 'x')
|
365
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
366
|
+
var a = $("p").inGroupsOf(3);
|
367
|
+
var a = $("p").inGroupsOf(3, "x");
|
368
|
+
EOS
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_collection_proxy_with_each_slice
|
372
|
+
not_supported
|
373
|
+
|
374
|
+
@generator.select('p').each_slice('a', 3)
|
375
|
+
@generator.select('p').each_slice('a', 3) do |group, index|
|
376
|
+
group.reverse
|
377
|
+
end
|
378
|
+
|
379
|
+
assert_equal <<-EOS.strip, @generator.to_s
|
380
|
+
var a = $("p").eachSlice(3);
|
381
|
+
var a = $("p").eachSlice(3, function(value, index) {
|
382
|
+
return value.reverse();
|
383
|
+
});
|
384
|
+
EOS
|
385
|
+
end
|
386
|
+
|
387
|
+
#def test_debug_rjs
|
388
|
+
#ActionView::Base.debug_rjs = true
|
389
|
+
#@generator['welcome'].replace_html 'Welcome'
|
390
|
+
#assert_equal "try {\n$(\"#welcome\").html(\"Welcome\");\n} catch (e) { alert('RJS error:\\n\\n' + e.toString()); alert('$(\\\"#welcome\\\").html(\\\"Welcome\\\");'); throw e }", @generator.to_s
|
391
|
+
#ensure
|
392
|
+
#ActionView::Base.debug_rjs = false
|
393
|
+
#end
|
394
|
+
|
395
|
+
def test_literal
|
396
|
+
not_supported
|
397
|
+
|
398
|
+
literal = @generator.literal("function() {}")
|
399
|
+
assert_equal "function() {}", ActiveSupport::JSON.encode(literal)
|
400
|
+
assert_equal "", @generator.to_s
|
401
|
+
end
|
402
|
+
|
403
|
+
def test_class_proxy
|
404
|
+
@generator.form.focus('my_field')
|
405
|
+
assert_equal "Form.focus(\"my_field\");", @generator.to_s
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_call_with_block
|
409
|
+
not_supported
|
410
|
+
|
411
|
+
@generator.call(:before)
|
412
|
+
@generator.call(:my_method) do |p|
|
413
|
+
p[:one].show
|
414
|
+
p[:two].hide
|
415
|
+
end
|
416
|
+
@generator.call(:in_between)
|
417
|
+
@generator.call(:my_method_with_arguments, true, "hello") do |p|
|
418
|
+
p[:three].visual_effect(:highlight)
|
419
|
+
end
|
420
|
+
assert_equal "before();\nmy_method(function() { $(\"#one\").show();\n$(\"#two\").hide(); });\nin_between();\nmy_method_with_arguments(true, \"hello\", function() { $(\"#three\").visualEffect(\"highlight\"); });", @generator.to_s
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_class_proxy_call_with_block
|
424
|
+
not_supported
|
425
|
+
|
426
|
+
@generator.my_object.my_method do |p|
|
427
|
+
p[:one].show
|
428
|
+
p[:two].hide
|
429
|
+
end
|
430
|
+
assert_equal "MyObject.myMethod(function() { $(\"#one\").show();\n$(\"#two\").hide(); });", @generator.to_s
|
431
|
+
end
|
432
|
+
end
|