de_rjs 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45fff0b7ea1479cbc72d621956f580b4a2eebae4
4
- data.tar.gz: 385153d5782044994e2b5a13d0a408ba79b56205
3
+ metadata.gz: c2e9a07c8725690641007139a17dd1b6d817e658
4
+ data.tar.gz: a61b27b0601518e2891d0ba8e01829518b4dc0a2
5
5
  SHA512:
6
- metadata.gz: 55edfb6f9ca26275582323852e7c009ae5d947455e38647e589b34e0256ed9c08de1773caff5a5004206399411d68956c007cc7e1376516812210647db48ec34
7
- data.tar.gz: 3de4af5f9488338fbc93371a9f4929cc7da7ae78e13b5df8a1d1bf07f80a68d4f78b7b8560f565c940d1d2951c68488efd435d2dd3fd120f53758dd9bf2ee0c5
6
+ metadata.gz: 7d60e5bdba480b0395c4d861cbebf11c5ce6c23573d43e0ab900b356ba1d7fec37bec1bd9a4d6b52d87ac990e05ab644e3061ff893bd875ce7cb62f227c05710
7
+ data.tar.gz: d465e14b55b9a35a65def6cfa6086d258e8d3bdc4ac2a09c7fb646a13cb01ef3e8ced2fca40333a6bffe0da386d929a0a33030eb5293e5cf50aef58e5328041b
@@ -0,0 +1,32 @@
1
+ Use this to de-RJS your application.
2
+
3
+ Converts your .rjs code into js.erb compliant code
4
+
5
+ # CAVEATS
6
+
7
+ ## dom_id_or_string
8
+
9
+ `page[@record]`, where `@record` is a ActiveRecord object
10
+ would translate to `$("<%= dom_id(@record)")` perfectly fine.
11
+
12
+ However, if `@var` computes to a string, such as `@var = "fixed_id"`,
13
+ then `dom_id(@var)` will result in an error. Hence for safety, I have decided to
14
+ transcode `page[@var]` to :
15
+
16
+ ```
17
+ $("<%= dom_id_or_string(@var) $>")
18
+ ```
19
+
20
+ You can either choose to visually inspet the diff and manuall replace each occurence
21
+ back to dom_id, or you can define the following method:
22
+
23
+ ```
24
+ def dom_id_or_string(thing)
25
+ case thing
26
+ when String, Symbol, NilClass
27
+ thing
28
+ else
29
+ dom_id(thing)
30
+ end
31
+ end
32
+ ```
@@ -4,6 +4,10 @@ module DeRjs
4
4
  class MustTranslateManually < StandardError; end
5
5
 
6
6
  def on_send(node)
7
+ rewrite_page_calls(node)
8
+ end
9
+
10
+ def rewrite_page_calls(node)
7
11
  receiver_node, method_name, *arg_nodes = *node
8
12
 
9
13
  if receiver_node.to_a.last == :page
@@ -17,6 +21,9 @@ module DeRjs
17
21
  when :redirect_to
18
22
  rewrite_redirect_to(receiver_node, method_name, *arg_nodes)
19
23
 
24
+ when :visual_effect
25
+ rewrite_visual_effect(receiver_node, method_name, *arg_nodes)
26
+
20
27
  when :[]
21
28
  rewrite_square_bracket(receiver_node, method_name, *arg_nodes)
22
29
 
@@ -29,7 +36,6 @@ module DeRjs
29
36
  # page.replace
30
37
  # page.select
31
38
  # page.show
32
- # page.visual_effect
33
39
  rewrite_all_args(receiver_node, method_name, *arg_nodes)
34
40
  end
35
41
  end
@@ -41,6 +47,12 @@ module DeRjs
41
47
  end
42
48
  end
43
49
 
50
+ protected
51
+ # name, id=nil, options
52
+ def rewrite_visual_effect(receiver_node, method_name, *arg_nodes)
53
+ rewrite_to_erb_unless_static(arg_nodes[1]) if arg_nodes[1]
54
+ end
55
+
44
56
  def rewrite_all_args(receiver_node, method_name, *arg_nodes)
45
57
  arg_nodes.each {|arg_node| rewrite_to_erb_unless_static(arg_node) }
46
58
  end
@@ -73,7 +85,6 @@ module DeRjs
73
85
  rewrite_options_for_render(arg_nodes)
74
86
  end
75
87
 
76
- protected
77
88
  def rewrite_to_erb_unless_static(id_arg)
78
89
  return if [:str, :sym].include?(id_arg.type)
79
90
  insert_before id_arg.loc.expression, "%q{<%= "
@@ -86,7 +97,7 @@ module DeRjs
86
97
  insert_before id_arg.loc.expression, "%q{<%= "
87
98
  insert_after id_arg.loc.expression, " %>}"
88
99
  else
89
- insert_before id_arg.loc.expression, "%q{<%= dom_id("
100
+ insert_before id_arg.loc.expression, "%q{<%= dom_id_or_string("
90
101
  insert_after id_arg.loc.expression, ") %>}"
91
102
  end
92
103
  end
@@ -84,6 +84,10 @@ END
84
84
  generate_js(%q{ page.replace('element', :partial => "post", :locals => {:ab => "cd"}) })
85
85
  end
86
86
 
87
+ def test_square_element_replace_with_hash
88
+ assert_equal 'jQuery("#element").replaceWith("<%= escape_javascript(render(:partial => "post", :locals => {:ab => "cd"})) %>");',
89
+ generate_js(%q{ page['element'].replace(:partial => "post", :locals => {:ab => "cd"}) })
90
+ end
87
91
 
88
92
  def test_remove
89
93
  assert_equal 'jQuery("#foo").remove();',
@@ -134,8 +138,8 @@ END
134
138
  end
135
139
 
136
140
  def test_element_access_on_variable
137
- assert_equal %(jQuery("#<%= dom_id(@var) %>");), generate_js(%q{ page[@var] })
138
- assert_equal %(jQuery("#<%= dom_id(@var) %>").hide();), generate_js(%q{ page[@var].hide })
141
+ assert_equal %(jQuery("#<%= dom_id_or_string(@var) %>");), generate_js(%q{ page[@var] })
142
+ assert_equal %(jQuery("#<%= dom_id_or_string(@var) %>").hide();), generate_js(%q{ page[@var].hide })
139
143
  end
140
144
 
141
145
  def test_element_access_on_interpolated_string
@@ -144,15 +148,15 @@ END
144
148
  end
145
149
 
146
150
  def test_element_access_on_records
147
- assert_equal %(jQuery("#<%= dom_id(Bunny.new(:id => 5)) %>");), generate_js(%q{ page[Bunny.new(:id => 5)] })
148
- assert_equal %(jQuery("#<%= dom_id(Bunny.new) %>");), generate_js(%q{ page[Bunny.new] })
151
+ assert_equal %(jQuery("#<%= dom_id_or_string(Bunny.new(:id => 5)) %>");), generate_js(%q{ page[Bunny.new(:id => 5)] })
152
+ assert_equal %(jQuery("#<%= dom_id_or_string(Bunny.new) %>");), generate_js(%q{ page[Bunny.new] })
149
153
  end
150
154
 
151
155
  def test_element_access_on_dom_id
152
156
  assert_equal %(jQuery("#<%= dom_id(Bunny.new(:id => 5)) %>");), generate_js(%q{ page[dom_id(Bunny.new(:id => 5))] })
153
157
  assert_equal %(jQuery("#<%= dom_id(Bunny.new) %>");), generate_js(%q{ page[dom_id(Bunny.new)] })
154
158
 
155
- assert_equal %(jQuery("#<%= dom_id(dom_id(Bunny.new) + evil) %>");), generate_js(%q{ page[dom_id(Bunny.new) + evil] })
159
+ assert_equal %(jQuery("#<%= dom_id_or_string(dom_id(Bunny.new) + evil) %>");), generate_js(%q{ page[dom_id(Bunny.new) + evil] })
156
160
  end
157
161
 
158
162
  def test_element_proxy_one_deep
@@ -202,6 +206,14 @@ END
202
206
  generate_js(%q{ page.visual_effect(:toggle_appear,"blah" + blah.id) })
203
207
  end
204
208
 
209
+ def test_visial_effect_with_options
210
+ assert_equal %(jQuery(\"#blah\").effect(\"highlight\",{endcolor:'#eeeeee', startcolor:'#ffffaa'});),
211
+ generate_js(%q{ page['blah'].visual_effect(:highlight, :startcolor => "#ffffaa", :endcolor => "#eeeeee") })
212
+
213
+ assert_equal %(jQuery(\"#blah\").effect(\"highlight\",{endcolor:'#eeeeee', startcolor:'#ffffaa'});),
214
+ generate_js(%q{ page.visual_effect(:highlight, 'blah', :startcolor => "#ffffaa", :endcolor => "#eeeeee") })
215
+ end
216
+
205
217
  def test_collection_first_and_last
206
218
  js = generate_js(%q{
207
219
  page.select('p.welcome b').first.hide()
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: de_rjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thong Kuah
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-30 00:00:00.000000000 Z
11
+ date: 2017-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -72,7 +72,7 @@ extensions: []
72
72
  extra_rdoc_files: []
73
73
  files:
74
74
  - Gemfile
75
- - README
75
+ - README.md
76
76
  - Rakefile
77
77
  - bin/de_rjs
78
78
  - lib/de_rjs.rb
data/README DELETED
@@ -1,3 +0,0 @@
1
- Use this to de-RJS your application.
2
-
3
- Converts your .rjs code into js.erb compliant code