gon 1.1.3 → 2.0.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/README.md CHANGED
@@ -1,7 +1,33 @@
1
1
  # Gon gem — get your Rails variables in your js
2
2
 
3
+
3
4
  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
5
 
6
+ Now with [Rabl](https://github.com/nesquena/rabl) support!
7
+
8
+ ## A example of typical use
9
+
10
+ When you need to send some start data from your controller to your js
11
+ you probably doing something like this:
12
+
13
+ 1. Write this data in controller(presenter/model) in some variable
14
+ 2. In view for this action you put this data in some objects by data
15
+ attributes, or write js right in view
16
+ 3. Then in js it depends - if you previously write data in data
17
+ attributes - you should parse this attributes and write data to some
18
+ js variable. If you write js right in view (many frontenders shame you for
19
+ that) - you just use data from this js - OK.
20
+ 4. You can use your data in your js
21
+
22
+ And everytime when you need some data from action to js you do this.
23
+
24
+ With gon you configure it firstly - just put in layout one tag, and add
25
+ gem line to your Gemfile and do two actions:
26
+
27
+ 1. Write variables by gon.variable_name = variable_value
28
+ 2. In your js you get this by gon.variable_name
29
+ 3. profit?
30
+
5
31
  ## Usage
6
32
 
7
33
  `app/views/layouts/application.html.erb`
@@ -79,18 +105,94 @@ alert(customNamespace.yourArray)
79
105
  alert(customNamespace.yourHash)
80
106
  ```
81
107
 
108
+ ## Usage with Rabl
109
+
110
+ Now you can write your variables assign logic in templates with [Rabl](https://github.com/nesquena/rabl).
111
+ How write templates very clearly described in their repo.
112
+
113
+ Profit of using Rabl with gon:
114
+
115
+ 1. You can clean your controllers now!
116
+ 2. Clear and easy work with database objects and collections
117
+ 3. All power of Rabl
118
+ 4. You still can be lazy and don't use common way to transfer data in js
119
+ 5. And so on
120
+
121
+ For using gon with Rabl you need to create new Rabl template and map gon
122
+ to it.
123
+ For example you have model Post with attributes :title and :body.
124
+ You want to get all your posts in your js as an Array.
125
+ Thats what you need to do:
126
+
127
+ 1. Create Rabl template. I preffer creating special directory for
128
+ templates which are not view templates.
129
+
130
+ `app/goners/posts/index.rabl`
131
+
132
+ ``` rabl
133
+ collection @posts => 'posts'
134
+ attributes :id, :title, :body
135
+ ```
136
+
137
+ 2. After that you need only map this template to gon.
138
+
139
+ `app/controllers/post_controller.rb#index`
140
+
141
+ ``` ruby
142
+ def index
143
+ # some controller logic
144
+ @posts = Post.all # Rabl works with instance variables of controller
145
+
146
+ gon.rabl 'app/goners/posts/index.rabl'
147
+ end
148
+ ```
149
+
150
+ Thats it! In your js now you get gon.posts variable which is Array of
151
+ post objects with attributes :id, :title and :body.
152
+
153
+ P.s. If you didn't put include_gon tag in your html head area - it
154
+ wouldn't work. You can read about this in common usage above.
155
+
156
+ ### Some tips of usage Rabl with gon:
157
+
158
+ If you don't use alias in Rabl template:
159
+
160
+ ``` rabl
161
+ collection @posts
162
+ ....
163
+ ```
164
+
165
+ instead of using that:
166
+
167
+ ``` rabl
168
+ collection @posts => 'alias'
169
+ ....
170
+ ```
171
+
172
+ Rabl will return you array and gon by default put it to variable
173
+ gon.rabl
174
+
175
+ Two way how you can change it - using aliases or you can add alias to
176
+ gon mapping method:
177
+
178
+ ``` ruby
179
+ # your controller stuff here
180
+
181
+ gon.rabl 'path/to/rabl/file', :as => 'alias'
182
+ ```
183
+
82
184
  ## Installation
83
185
 
84
186
  Puts this line into `Gemfile` then run `$ bundle`:
85
187
 
86
188
  ``` ruby
87
- gem 'gon', '1.1.3'
189
+ gem 'gon', '2.0.0'
88
190
  ```
89
191
 
90
192
  Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
91
193
 
92
194
  ``` ruby
93
- config.gem 'gon', :version => '1.1.3'
195
+ config.gem 'gon', :version => '2.0.0'
94
196
  ```
95
197
 
96
198
  Or manually install gon gem: `$ gem install gon`
@@ -19,5 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
  s.add_dependency "actionpack", '>= 2.3.0'
22
+ s.add_dependency "rabl"
23
+ s.add_dependency "json"
22
24
  s.add_development_dependency "rspec"
23
25
  end
data/lib/gon.rb CHANGED
@@ -1,44 +1,59 @@
1
1
  require 'action_view'
2
2
  require 'action_controller'
3
3
  require 'gon/helpers'
4
+ require 'gon/rabl'
4
5
 
5
6
  module Gon
6
- def self.all_variables
7
- @request_env[:gon]
8
- end
9
-
10
- def self.clear
11
- @request_env[:gon] = {}
12
- end
7
+ class << self
8
+ def all_variables
9
+ @request_env[:gon]
10
+ end
13
11
 
14
- def self.request_env=(request_env)
15
- @request_env = request_env
16
- end
12
+ def clear
13
+ @request_env[:gon] = {}
14
+ end
17
15
 
18
- def self.request_env
19
- if defined?(@request_env)
20
- return @request_env
16
+ def request_env=(request_env)
17
+ @request_env = request_env
18
+ @request_env[:gon] ||= {}
21
19
  end
22
- end
23
20
 
24
- def self.method_missing(m, *args, &block)
25
- @request_env[:gon] ||= {}
21
+ def request_env
22
+ if defined?(@request_env)
23
+ return @request_env
24
+ end
25
+ end
26
26
 
27
- if ( m.to_s =~ /=$/ )
28
- if self.public_methods.include? m.to_s[0..-2].to_sym
29
- raise "You can't use Gon public methods for storing data"
27
+ def method_missing(m, *args, &block)
28
+ if ( m.to_s =~ /=$/ )
29
+ if public_methods.include? m.to_s[0..-2].to_sym
30
+ raise "You can't use Gon public methods for storing data"
31
+ end
32
+ set_variable(m.to_s.delete('='), args[0])
33
+ else
34
+ get_variable(m.to_s)
30
35
  end
31
- set_variable(m.to_s.delete('='), args[0])
32
- else
33
- get_variable(m.to_s)
34
36
  end
35
- end
36
37
 
37
- def self.get_variable(name)
38
- @request_env[:gon][name]
39
- end
38
+ def get_variable(name)
39
+ @request_env[:gon][name]
40
+ end
40
41
 
41
- def self.set_variable(name, value)
42
- @request_env[:gon][name] = value
42
+ def set_variable(name, value)
43
+ @request_env[:gon][name] = value
44
+ end
45
+
46
+ def rabl(view_path, options = {})
47
+ rabl_data = Gon::Rabl.parse_rabl(view_path, @request_env['action_controller.instance'])
48
+ if options[:as]
49
+ set_variable(options[:as].to_s, rabl_data)
50
+ elsif rabl_data.is_a? Hash
51
+ rabl_data.each do |key, value|
52
+ set_variable(key, value)
53
+ end
54
+ else
55
+ set_variable('rabl', rabl_data)
56
+ end
57
+ end
43
58
  end
44
59
  end
@@ -6,7 +6,7 @@ module Gon
6
6
 
7
7
  module InstanceMethods
8
8
  def include_gon(options = {})
9
- if Gon.request_env
9
+ if Gon.request_env && Gon.all_variables.present?
10
10
  data = Gon.all_variables
11
11
  namespace = options[:namespace] || 'gon'
12
12
  script = "<script>window." + namespace + " = {};"
@@ -45,4 +45,4 @@ module Gon
45
45
  end
46
46
 
47
47
  ActionView::Base.send :include, Gon::Helpers
48
- ActionController::Base.send :include, Gon::GonHelpers
48
+ ActionController::Base.send :include, Gon::GonHelpers
@@ -0,0 +1,12 @@
1
+ require 'rabl'
2
+
3
+ module Gon::Rabl
4
+ class << self
5
+ def parse_rabl(rabl_path, controller)
6
+ source = File.read(rabl_path)
7
+ rabl_engine = Rabl::Engine.new(source, :format => 'json')
8
+ output = rabl_engine.render(controller, {})
9
+ JSON.parse(output)
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Gon
2
- VERSION = '1.1.3'
2
+ VERSION = '2.0.0'
3
3
  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.3
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-11-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
16
- requirement: &2156527520 !ruby/object:Gem::Requirement
16
+ requirement: &2158488420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,32 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156527520
24
+ version_requirements: *2158488420
25
+ - !ruby/object:Gem::Dependency
26
+ name: rabl
27
+ requirement: &2158488000 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2158488000
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ requirement: &2158487540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2158487540
25
47
  - !ruby/object:Gem::Dependency
26
48
  name: rspec
27
- requirement: &2156527100 !ruby/object:Gem::Requirement
49
+ requirement: &2158487120 !ruby/object:Gem::Requirement
28
50
  none: false
29
51
  requirements:
30
52
  - - ! '>='
@@ -32,7 +54,7 @@ dependencies:
32
54
  version: '0'
33
55
  type: :development
34
56
  prerelease: false
35
- version_requirements: *2156527100
57
+ version_requirements: *2158487120
36
58
  description: If you need to send some data to your js files and you don't want to
37
59
  do this with long way trough views and parsing - use this force!
38
60
  email:
@@ -48,6 +70,7 @@ files:
48
70
  - gon.gemspec
49
71
  - lib/gon.rb
50
72
  - lib/gon/helpers.rb
73
+ - lib/gon/rabl.rb
51
74
  - lib/gon/version.rb
52
75
  - spec/gon/gon_spec.rb
53
76
  homepage: ''