de_rjs 0.2.2 → 0.3.0

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: f44960ac0f469ff1711cd967aac5329ddf0e5396
4
- data.tar.gz: fea015c10bdea4828b694beba00315d0581d3e55
3
+ metadata.gz: da7377a69fb9bcc6c69b25e0972f8fdfa51af1d9
4
+ data.tar.gz: 6ea98789b3bb597dbdf98c457975f2002f77cdac
5
5
  SHA512:
6
- metadata.gz: d96893c52e23fb62646b5067275f1834f0ef20202dd8f99ee0edffdcfba13fe16d85df388966f58a16dbf31f4c6420543e1d5b1462c2ee75dbca9ff1d8e5c5de
7
- data.tar.gz: 91d27c2831e7cd3ca8efcfbb69c266b519b9d55a1fe2d55ea0e5d72ddf5f29d3b61d768c1b5ff5af27bae7b71f18611cb71030816391539ecda5bd9373ec2118
6
+ metadata.gz: 15564eabf6d2a638cfadab4da2671d9f6a5d915c02d0aee68f6dcf0959c0d782829b7cf1516631228bcf41dc8c42cfcb032d77d810bf114ec035832bd3e51fe0
7
+ data.tar.gz: 64264098fb4c161a45aaea42faa042ec8681f8085b039eaf4862d31c7f96f6afacacb960cf10e6dc460d349b6009930e0d11f99e2da5ffd87c4883d35498d4e2
@@ -134,7 +134,7 @@ module DeRjs
134
134
  when String, Symbol, NilClass
135
135
  JavaScriptElementProxy.new(self, id)
136
136
  else
137
- JavaScriptElementProxy.new(self, ActionController::RecordIdentifier.dom_id(id))
137
+ JavaScriptElementProxy.new(self, ActionView::RecordIdentifier.dom_id(id))
138
138
  end
139
139
  end
140
140
 
@@ -64,16 +64,8 @@ module DeRjs
64
64
  end
65
65
 
66
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
67
  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)
68
+ rewrite_dom_id(arg_nodes.shift)
77
69
  end
78
70
 
79
71
  # *options_for_render
@@ -88,6 +80,22 @@ module DeRjs
88
80
  insert_after id_arg.loc.expression, " %>}"
89
81
  end
90
82
 
83
+ def rewrite_dom_id(id_arg)
84
+ return if [:str, :sym].include?(id_arg.type)
85
+ if id_arg.type == :dstr || is_dom_id_call?(id_arg)
86
+ insert_before id_arg.loc.expression, "%q{<%= "
87
+ insert_after id_arg.loc.expression, " %>}"
88
+ else
89
+ insert_before id_arg.loc.expression, "%q{<%= dom_id("
90
+ insert_after id_arg.loc.expression, ") %>}"
91
+ end
92
+ end
93
+
94
+ def is_dom_id_call?(arg_node)
95
+ array = arg_node.to_a
96
+ array[0..1] == [nil, :dom_id] #&& array.size == 3
97
+ end
98
+
91
99
  def rewrite_options_for_render(arg_nodes)
92
100
  return if arg_nodes.size == 1 && arg_nodes.first.type == :str
93
101
  insert_before arg_nodes.first.loc.expression, "%q{<%= escape_javascript(render("
@@ -151,23 +151,26 @@ jQuery("#baz").html("\\u003cp\\u003eThis is a test\\u003c/p\\u003e");
151
151
  end
152
152
 
153
153
  def test_element_access_on_variable
154
- assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
155
- assert_equal %(jQuery("#<%= 'hello' + @var %>");), generate_js(%Q{ page['hello' + @var] })
156
- end
157
- assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
158
- assert_equal %(jQuery("#<%= 'hello' + @var %>").hide();), generate_js(%Q{ page['hello' + @var].hide })
159
- end
154
+ assert_equal %(jQuery("#<%= dom_id(@var) %>");), generate_js(%Q{ page[@var] })
155
+ assert_equal %(jQuery("#<%= dom_id(@var) %>").hide();), generate_js(%Q{ page[@var].hide })
156
+ end
157
+
158
+ def test_element_access_on_interpolated_string
159
+ assert_equal %q(jQuery("#<%= "hello#{@var}" %>");), generate_js(%q{ page["hello#{@var}"] })
160
+ assert_equal %q(jQuery("#<%= "hello#{@var}" %>").hide();), generate_js(%q{page["hello#{@var}"].hide })
160
161
  end
161
162
 
162
163
  def test_element_access_on_records
163
- assert_raises DeRjs::Rewriter::Erbify::MustTranslateManually do
164
- assert_equal %(jQuery("#<%= 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 %(jQuery("#<%= Bunny.new %>");), generate_js(%Q{ page[Bunny.new] })
168
- end
164
+ assert_equal %(jQuery("#<%= dom_id(Bunny.new(:id => 5)) %>");), generate_js(%Q{ page[Bunny.new(:id => 5)] })
165
+ assert_equal %(jQuery("#<%= dom_id(Bunny.new) %>");), generate_js(%Q{ page[Bunny.new] })
169
166
  end
170
167
 
168
+ def test_element_access_on_dom_id
169
+ assert_equal %(jQuery("#<%= dom_id(Bunny.new(:id => 5)) %>");), generate_js(%Q{ page[dom_id(Bunny.new(:id => 5))] })
170
+ assert_equal %(jQuery("#<%= dom_id(Bunny.new) %>");), generate_js(%Q{ page[dom_id(Bunny.new)] })
171
+
172
+ assert_equal %(jQuery("#<%= dom_id(dom_id(Bunny.new) + evil) %>");), generate_js(%Q{ page[dom_id(Bunny.new) + evil] })
173
+ end
171
174
 
172
175
  def test_element_proxy_one_deep
173
176
  assert_equal %(jQuery("#hello").hide();), generate_js(%Q{ page['hello'].hide })
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: de_rjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thong Kuah
@@ -83,7 +83,8 @@ files:
83
83
  - test/abstract_unit.rb
84
84
  - test/de_rjs_test.rb
85
85
  homepage: http://github.com/kuahyeow/de_rjs
86
- licenses: []
86
+ licenses:
87
+ - MIT
87
88
  metadata: {}
88
89
  post_install_message:
89
90
  rdoc_options: []