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 +4 -4
- data/README.md +1 -1
- data/lib/rails_var_to_js_helper.rb +31 -35
- data/lib/rails_var_to_js_helper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '093284add893187a2e4b1de2d910d234c2e2faec32c99c38076383c40cb83415'
|
4
|
+
data.tar.gz: 00f41859d69aa857a18b57c8a60aa376e2ec4662f2c497d6a3bd889b37f21b85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e79d79425deea845ca1e30699accf034b8f9e73b854062fa901e963f7cf0dc4d137ddfe4ca806921f54370cd09d61cb992211235065873869c454b39e62dfe75
|
7
|
+
data.tar.gz: 7f5279138d1630f7ebd8a51c1c614b31e7f1415790477fc5028fea7c9de7381a195167e251980158d85fbc7f9d71230f69e6475c3b42c90a1f8b7c58d6e4bd3e
|
data/README.md
CHANGED
@@ -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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2019-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|