gon 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of gon might be problematic. Click here for more details.
- data/README.md +22 -2
- data/lib/gon.rb +5 -1
- data/lib/gon/helpers.rb +17 -7
- data/lib/gon/version.rb +1 -1
- data/spec/gon/gon_spec.rb +14 -9
- metadata +7 -9
data/README.md
CHANGED
@@ -14,6 +14,17 @@ If you need to send some data to your js files and you don't want to do this wit
|
|
14
14
|
...
|
15
15
|
```
|
16
16
|
|
17
|
+
For camelize your variables in js you can use:
|
18
|
+
|
19
|
+
``` erb
|
20
|
+
<head>
|
21
|
+
<title>some title</title>
|
22
|
+
<%= include_gon(:camel_case => true) %>
|
23
|
+
<!-- or just include_gon(true) -->
|
24
|
+
<!-- include your action js code with camelized variables -->
|
25
|
+
...
|
26
|
+
```
|
27
|
+
|
17
28
|
In action of your controller you put something like this:
|
18
29
|
|
19
30
|
``` ruby
|
@@ -41,18 +52,27 @@ alert(gon.your_array)
|
|
41
52
|
alert(gon.your_hash)
|
42
53
|
```
|
43
54
|
|
55
|
+
With camelize:
|
56
|
+
|
57
|
+
``` js
|
58
|
+
alert(gon.yourInt)
|
59
|
+
alert(gon.yourOtherInt)
|
60
|
+
alert(gon.yourArray)
|
61
|
+
alert(gon.yourHash)
|
62
|
+
```
|
63
|
+
|
44
64
|
## Installation
|
45
65
|
|
46
66
|
Puts this line into `Gemfile` then run `$ bundle`:
|
47
67
|
|
48
68
|
``` ruby
|
49
|
-
gem 'gon', '1.1.
|
69
|
+
gem 'gon', '1.1.1'
|
50
70
|
```
|
51
71
|
|
52
72
|
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
|
53
73
|
|
54
74
|
``` ruby
|
55
|
-
config.gem 'gon', :version => '1.1.
|
75
|
+
config.gem 'gon', :version => '1.1.1'
|
56
76
|
```
|
57
77
|
|
58
78
|
Or manually install gon gem: `$ gem install gon`
|
data/lib/gon.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'action_view'
|
2
|
+
require 'action_controller'
|
2
3
|
require 'gon/helpers'
|
3
4
|
|
4
5
|
module Gon
|
@@ -23,7 +24,10 @@ module Gon
|
|
23
24
|
def self.method_missing(m, *args, &block)
|
24
25
|
@request_env[:gon] ||= {}
|
25
26
|
|
26
|
-
if ( m.to_s =~
|
27
|
+
if ( m.to_s =~ /=$/ )
|
28
|
+
if self.public_methods.include? m[0..-2].to_sym
|
29
|
+
raise "You can't use Gon public methods for storing data"
|
30
|
+
end
|
27
31
|
set_variable(m.to_s.delete('='), args[0])
|
28
32
|
else
|
29
33
|
get_variable(m.to_s)
|
data/lib/gon/helpers.rb
CHANGED
@@ -5,15 +5,25 @@ module Gon
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module InstanceMethods
|
8
|
-
def include_gon
|
9
|
-
|
8
|
+
def include_gon(camel_case = false)
|
9
|
+
if Gon.request_env
|
10
|
+
data = Gon.all_variables
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
script = "<script>window.gon = {};"
|
13
|
+
unless camel_case
|
14
|
+
data.each do |key, val|
|
15
|
+
script += "gon." + key.to_s + '=' + val.to_json + ";"
|
16
|
+
end
|
17
|
+
else
|
18
|
+
data.each do |key, val|
|
19
|
+
script += "gon." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
script += "</script>"
|
23
|
+
script.html_safe
|
24
|
+
else
|
25
|
+
""
|
14
26
|
end
|
15
|
-
script += "</script>"
|
16
|
-
script.html_safe
|
17
27
|
end
|
18
28
|
end
|
19
29
|
end
|
data/lib/gon/version.rb
CHANGED
data/spec/gon/gon_spec.rb
CHANGED
@@ -3,9 +3,9 @@ require 'gon'
|
|
3
3
|
|
4
4
|
describe Gon, '#all_variables' do
|
5
5
|
before(:each) do
|
6
|
-
Gon.
|
6
|
+
Gon.request_env = {}
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
it 'returns all variables in hash' do
|
10
10
|
Gon.a = 1
|
11
11
|
Gon.b = 2
|
@@ -13,28 +13,33 @@ describe Gon, '#all_variables' do
|
|
13
13
|
Gon.c.should == 3
|
14
14
|
Gon.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3}
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
it 'supports all data types' do
|
18
18
|
Gon.clear
|
19
19
|
Gon.int = 1
|
20
20
|
Gon.float = 1.1
|
21
21
|
Gon.string = 'string'
|
22
22
|
Gon.array = [ 1, 'string' ]
|
23
|
-
Gon.
|
23
|
+
Gon.hash_var = { :a => 1, :b => '2'}
|
24
24
|
Gon.hash_w_array = { :a => [ 2, 3 ] }
|
25
25
|
Gon.klass = Hash
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it 'output as js correct' do
|
29
29
|
Gon.clear
|
30
30
|
Gon.int = 1
|
31
|
-
ActionView::Base.instance_methods.include?('include_gon').should == true
|
31
|
+
ActionView::Base.instance_methods.map(&:to_s).include?('include_gon').should == true
|
32
32
|
base = ActionView::Base.new
|
33
|
-
base.include_gon.should == "<script>window.
|
34
|
-
"
|
33
|
+
base.include_gon.should == "<script>window.gon = {};" +
|
34
|
+
"gon.int=1;" +
|
35
35
|
"</script>"
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
|
+
it 'returns exception if try to set public method as variable' do
|
39
|
+
Gon.clear
|
40
|
+
lambda { Gon.all_variables = 123 }.should raise_error
|
41
|
+
end
|
42
|
+
|
38
43
|
def request
|
39
44
|
@request ||= double 'request', :env => {}
|
40
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
13
|
-
default_executable:
|
12
|
+
date: 2011-10-26 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: actionpack
|
17
|
-
requirement: &
|
16
|
+
requirement: &2161715780 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ! '>='
|
@@ -22,10 +21,10 @@ dependencies:
|
|
22
21
|
version: 2.3.0
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *2161715780
|
26
25
|
- !ruby/object:Gem::Dependency
|
27
26
|
name: rspec
|
28
|
-
requirement: &
|
27
|
+
requirement: &2161715360 !ruby/object:Gem::Requirement
|
29
28
|
none: false
|
30
29
|
requirements:
|
31
30
|
- - ! '>='
|
@@ -33,7 +32,7 @@ dependencies:
|
|
33
32
|
version: '0'
|
34
33
|
type: :development
|
35
34
|
prerelease: false
|
36
|
-
version_requirements: *
|
35
|
+
version_requirements: *2161715360
|
37
36
|
description: If you need to send some data to your js files and you don't want to
|
38
37
|
do this with long way trough views and parsing - use this force!
|
39
38
|
email:
|
@@ -51,7 +50,6 @@ files:
|
|
51
50
|
- lib/gon/helpers.rb
|
52
51
|
- lib/gon/version.rb
|
53
52
|
- spec/gon/gon_spec.rb
|
54
|
-
has_rdoc: true
|
55
53
|
homepage: ''
|
56
54
|
licenses: []
|
57
55
|
post_install_message:
|
@@ -72,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
70
|
version: '0'
|
73
71
|
requirements: []
|
74
72
|
rubyforge_project: gon
|
75
|
-
rubygems_version: 1.
|
73
|
+
rubygems_version: 1.8.5
|
76
74
|
signing_key:
|
77
75
|
specification_version: 3
|
78
76
|
summary: Get your Rails variables in your JS
|