gon 1.0.0 → 1.1.0
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/Gemfile +1 -1
- data/README.md +15 -15
- data/gon.gemspec +2 -0
- data/lib/gon.rb +33 -7
- data/lib/gon/helpers.rb +18 -3
- data/lib/gon/version.rb +1 -1
- data/spec/gon/gon_spec.rb +15 -9
- metadata +48 -42
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Gon gem — get your Rails variables in your js
|
2
2
|
|
3
|
-
If you need to send some data to your js files and you don't want to do this with long way
|
3
|
+
If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
@@ -20,25 +20,25 @@ In action of your controller you put something like this:
|
|
20
20
|
@your_int = 123
|
21
21
|
@your_array = [1,2]
|
22
22
|
@your_hash = {'a' => 1, 'b' => 2}
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
gon.your_int = @your_int
|
24
|
+
gon.your_other_int = 345 + gon.your_int
|
25
|
+
gon.your_array = @your_array
|
26
|
+
gon.your_array << gon.your_int
|
27
|
+
gon.your_hash = @your_hash
|
28
28
|
|
29
|
-
|
30
|
-
|
29
|
+
gon.all_variables # > {:your_int => 123, :your_other_int => 468, :your_array => [1, 2, 123], :your_hash => {'a' => 1, 'b' => 2}}
|
30
|
+
gon.your_array # > [1, 2, 123]
|
31
31
|
|
32
|
-
|
32
|
+
gon.clear # gon.all_variables now is {}
|
33
33
|
```
|
34
34
|
|
35
35
|
In javascript file for view of this action write call to your variable:
|
36
36
|
|
37
37
|
``` js
|
38
|
-
alert(
|
39
|
-
alert(
|
40
|
-
alert(
|
41
|
-
alert(
|
38
|
+
alert(gon.your_int)
|
39
|
+
alert(gon.your_other_int)
|
40
|
+
alert(gon.your_array)
|
41
|
+
alert(gon.your_hash)
|
42
42
|
```
|
43
43
|
|
44
44
|
## Installation
|
@@ -46,13 +46,13 @@ alert(Gon.your_hash)
|
|
46
46
|
Puts this line into `Gemfile` then run `$ bundle`:
|
47
47
|
|
48
48
|
``` ruby
|
49
|
-
gem 'gon', '1.
|
49
|
+
gem 'gon', '1.1.0'
|
50
50
|
```
|
51
51
|
|
52
52
|
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
|
53
53
|
|
54
54
|
``` ruby
|
55
|
-
config.gem 'gon', :version => '1.
|
55
|
+
config.gem 'gon', :version => '1.1.0'
|
56
56
|
```
|
57
57
|
|
58
58
|
Or manually install gon gem: `$ gem install gon`
|
data/gon.gemspec
CHANGED
@@ -18,4 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
+
s.add_dependency "actionpack", '>= 2.3.0'
|
22
|
+
s.add_development_dependency "rspec"
|
21
23
|
end
|
data/lib/gon.rb
CHANGED
@@ -1,14 +1,40 @@
|
|
1
1
|
require 'action_view'
|
2
2
|
require 'gon/helpers'
|
3
|
-
require 'ostruct'
|
4
3
|
|
5
|
-
Gon
|
4
|
+
module Gon
|
5
|
+
def self.all_variables
|
6
|
+
@request_env[:gon]
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.clear
|
10
|
+
@request_env[:gon] = {}
|
11
|
+
end
|
6
12
|
|
7
|
-
|
8
|
-
|
9
|
-
instance_variable_get :@table
|
13
|
+
def self.request_env=(request_env)
|
14
|
+
@request_env = request_env
|
10
15
|
end
|
11
|
-
|
12
|
-
|
16
|
+
|
17
|
+
def self.request_env
|
18
|
+
if defined?(@request_env)
|
19
|
+
return @request_env
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.method_missing(m, *args, &block)
|
24
|
+
@request_env[:gon] ||= {}
|
25
|
+
|
26
|
+
if ( m.to_s =~ /=/ )
|
27
|
+
set_variable(m.to_s.delete('='), args[0])
|
28
|
+
else
|
29
|
+
get_variable(m.to_s)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.get_variable(name)
|
34
|
+
@request_env[:gon][name]
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.set_variable(name, value)
|
38
|
+
@request_env[:gon][name] = value
|
13
39
|
end
|
14
40
|
end
|
data/lib/gon/helpers.rb
CHANGED
@@ -6,18 +6,33 @@ module Gon
|
|
6
6
|
|
7
7
|
module InstanceMethods
|
8
8
|
def include_gon
|
9
|
-
data = Gon.all_variables
|
9
|
+
data = Gon.all_variables
|
10
10
|
|
11
|
-
script = "<script>window.
|
11
|
+
script = "<script>window.gon = {};"
|
12
12
|
data.each do |key, val|
|
13
|
-
script += "
|
13
|
+
script += "gon." + key.to_s + '=' + val.to_json + ";"
|
14
14
|
end
|
15
15
|
script += "</script>"
|
16
16
|
script.html_safe
|
17
17
|
end
|
18
18
|
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module GonHelpers
|
22
|
+
def self.included base
|
23
|
+
base.send(:include, InstanceMethods)
|
24
|
+
end
|
19
25
|
|
26
|
+
module InstanceMethods
|
27
|
+
def gon
|
28
|
+
if !Gon.request_env || Gon.request_env.object_id != request.env.object_id
|
29
|
+
Gon.request_env = request.env
|
30
|
+
end
|
31
|
+
Gon
|
32
|
+
end
|
33
|
+
end
|
20
34
|
end
|
21
35
|
end
|
22
36
|
|
23
37
|
ActionView::Base.send :include, Gon::Helpers
|
38
|
+
ActionController::Base.send :include, Gon::GonHelpers
|
data/lib/gon/version.rb
CHANGED
data/spec/gon/gon_spec.rb
CHANGED
@@ -2,12 +2,16 @@
|
|
2
2
|
require 'gon'
|
3
3
|
|
4
4
|
describe Gon, '#all_variables' do
|
5
|
+
before(:each) do
|
6
|
+
Gon.stub(:request).and_return(request)
|
7
|
+
end
|
8
|
+
|
5
9
|
it 'returns all variables in hash' do
|
6
10
|
Gon.a = 1
|
7
11
|
Gon.b = 2
|
8
12
|
Gon.c = Gon.a + Gon.b
|
9
13
|
Gon.c.should == 3
|
10
|
-
Gon.all_variables.should == {
|
14
|
+
Gon.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3}
|
11
15
|
end
|
12
16
|
|
13
17
|
it 'supports all data types' do
|
@@ -18,18 +22,20 @@ describe Gon, '#all_variables' do
|
|
18
22
|
Gon.array = [ 1, 'string' ]
|
19
23
|
Gon.hash = { :a => 1, :b => '2'}
|
20
24
|
Gon.hash_w_array = { :a => [ 2, 3 ] }
|
21
|
-
Gon.klass =
|
22
|
-
|
25
|
+
Gon.klass = Hash
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'output as js correct' do
|
29
|
+
Gon.clear
|
30
|
+
Gon.int = 1
|
23
31
|
ActionView::Base.instance_methods.include?('include_gon').should == true
|
24
32
|
base = ActionView::Base.new
|
25
33
|
base.include_gon.should == "<script>window.Gon = {};" +
|
26
|
-
"Gon.klass={\"table\":{}};" +
|
27
|
-
"Gon.string=\"string\";" +
|
28
|
-
"Gon.array=[1,\"string\"];" +
|
29
|
-
"Gon.float=1.1;" +
|
30
34
|
"Gon.int=1;" +
|
31
|
-
"Gon.hash={\"a\":1,\"b\":\"2\"};" +
|
32
|
-
"Gon.hash_w_array={\"a\":[2,3]};" +
|
33
35
|
"</script>"
|
34
36
|
end
|
37
|
+
|
38
|
+
def request
|
39
|
+
@request ||= double 'request', :env => {}
|
40
|
+
end
|
35
41
|
end
|
metadata
CHANGED
@@ -1,33 +1,47 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: gon
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 0
|
10
|
-
version: 1.0.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- gazay
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
|
22
|
-
|
12
|
+
date: 2011-06-25 00:00:00.000000000 %:z
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
requirement: &2156682020 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.3.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2156682020
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &2156681540 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2156681540
|
37
|
+
description: If you need to send some data to your js files and you don't want to
|
38
|
+
do this with long way trough views and parsing - use this force!
|
39
|
+
email:
|
23
40
|
- alex.gaziev@gmail.com
|
24
41
|
executables: []
|
25
|
-
|
26
42
|
extensions: []
|
27
|
-
|
28
43
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
44
|
+
files:
|
31
45
|
- .gitignore
|
32
46
|
- Gemfile
|
33
47
|
- README.md
|
@@ -37,38 +51,30 @@ files:
|
|
37
51
|
- lib/gon/helpers.rb
|
38
52
|
- lib/gon/version.rb
|
39
53
|
- spec/gon/gon_spec.rb
|
40
|
-
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: ''
|
41
56
|
licenses: []
|
42
|
-
|
43
57
|
post_install_message:
|
44
58
|
rdoc_options: []
|
45
|
-
|
46
|
-
require_paths:
|
59
|
+
require_paths:
|
47
60
|
- lib
|
48
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
62
|
none: false
|
50
|
-
requirements:
|
51
|
-
- -
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
57
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
68
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
segments:
|
64
|
-
- 0
|
65
|
-
version: "0"
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
66
73
|
requirements: []
|
67
|
-
|
68
74
|
rubyforge_project: gon
|
69
|
-
rubygems_version: 1.
|
75
|
+
rubygems_version: 1.6.2
|
70
76
|
signing_key:
|
71
77
|
specification_version: 3
|
72
78
|
summary: Get your Rails variables in your JS
|
73
|
-
test_files:
|
79
|
+
test_files:
|
74
80
|
- spec/gon/gon_spec.rb
|