rails_var_to_js_helper 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: db4abbf628bbf913c2727d8a63052d762cb47d5f2c9043136613d5ef2333b2ae
4
- data.tar.gz: 06004f1e9df93b281db734e8fa9af98e9c64fb75e6f91f46caf223e2871a13c9
3
+ metadata.gz: '093284add893187a2e4b1de2d910d234c2e2faec32c99c38076383c40cb83415'
4
+ data.tar.gz: 00f41859d69aa857a18b57c8a60aa376e2ec4662f2c497d6a3bd889b37f21b85
5
5
  SHA512:
6
- metadata.gz: 6f1039dbe8d25c62ab9ac7696c9d9c59aae31aca218707c2d21114b7daeaa558a5a07bc0b233563138979ab3e6139d4e285053727b0b531e65247e25950bc84c
7
- data.tar.gz: 3328ca09d78cb60002dd2fc320c03d07cd759174a9c04bb2c6f07b94b3f86069c8773b9a9cfcf39a138abb19e921c540296b73cdb3eb920e75bb414c4218edd9
6
+ metadata.gz: e79d79425deea845ca1e30699accf034b8f9e73b854062fa901e963f7cf0dc4d137ddfe4ca806921f54370cd09d61cb992211235065873869c454b39e62dfe75
7
+ data.tar.gz: 7f5279138d1630f7ebd8a51c1c614b31e7f1415790477fc5028fea7c9de7381a195167e251980158d85fbc7f9d71230f69e6475c3b42c90a1f8b7c58d6e4bd3e
data/README.md CHANGED
@@ -3,7 +3,7 @@ Pass the Rails variable to JavaScript
3
3
 
4
4
  ## Usage
5
5
  ```
6
- <%== pass_to_js_tag({a: 1, b: "str"}) %>
6
+ <%= pass_to_js_tag({a: 1, b: "str"}) %>
7
7
 
8
8
  #=>
9
9
  # <script>
@@ -2,46 +2,42 @@
2
2
  require "rails_var_to_js_helper/railtie"
3
3
 
4
4
  module RailsVarToJsHelper
5
+ include ActionView::Helpers::TagHelper
5
6
  include ActionView::Helpers::JavaScriptHelper
6
- extend ActionView::Helpers::JavaScriptHelper
7
7
 
8
- class << self
9
- def pass_to_js_tag(vars = {})
10
- result = +"<script>\n"
11
-
12
- vars.each do |(k, v)|
13
- result << "window.#{k} = #{pass_to_js(v)};\n"
14
- end
15
-
16
- result << "</script>"
17
- result
8
+ def pass_to_js_tag(vars = {})
9
+ result = +""
10
+ vars.each do |(k, v)|
11
+ result << "window.#{k} = #{pass_to_js(v)};\n"
18
12
  end
19
13
 
20
- def pass_to_js(v)
21
- case v
22
- when String, Symbol
23
- "\"#{ escape_javascript(v.to_s) }\""
24
- when Numeric
25
- v
26
- when Array
27
- js_val = v.inject([]) do |result, array_v|
28
- result << pass_to_js(array_v)
29
- end
30
- "[#{js_val.join(',')}]"
31
- when Hash
32
- js_val = v.inject([]) do |result, (hash_k, hash_v)|
33
- result << "#{pass_to_js(hash_k)}: #{pass_to_js(hash_v)}"
34
- end
35
- "{#{js_val.join(',')}}"
36
- when TrueClass, FalseClass
37
- v.to_s
38
- when Time, Date, DateTime
39
- "new Date(#{pass_to_js(v.to_s)})"
40
- when nil
41
- js_val = "null"
42
- else
43
- raise "unsupport type: #{v.class}"
14
+ content_tag(:script, result, {}, false)
15
+ end
16
+
17
+ def pass_to_js(v)
18
+ case v
19
+ when String, Symbol
20
+ "\"#{ escape_javascript(v.to_s) }\""
21
+ when Numeric
22
+ v
23
+ when Array
24
+ js_val = v.inject([]) do |result, array_v|
25
+ result << pass_to_js(array_v)
26
+ end
27
+ "[#{js_val.join(',')}]"
28
+ when Hash
29
+ js_val = v.inject([]) do |result, (hash_k, hash_v)|
30
+ result << "#{pass_to_js(hash_k)}: #{pass_to_js(hash_v)}"
44
31
  end
32
+ "{#{js_val.join(',')}}"
33
+ when TrueClass, FalseClass
34
+ v.to_s
35
+ when Time, Date, DateTime
36
+ "new Date(#{pass_to_js(v.to_s)})"
37
+ when nil
38
+ js_val = "null"
39
+ else
40
+ raise "unsupport type: #{v.class}"
45
41
  end
46
42
  end
47
43
  end
@@ -1,3 +1,3 @@
1
1
  module RailsVarToJsHelper
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_var_to_js_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - potato2003
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-14 00:00:00.000000000 Z
11
+ date: 2019-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails