gon 2.0.6 → 2.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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Gon gem — get your Rails variables in your js
2
2
 
3
+ ![Gon. You should try this. If you look closer - you will see an elephant.](https://github.com/gazay/gon/raw/master/doc/logo_small.png)
4
+
3
5
 
4
6
  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!
5
7
 
@@ -9,6 +11,8 @@ For Sinatra available [gon-sinatra](https://github.com/gazay/gon-sinatra).
9
11
 
10
12
  ## An example of typical use
11
13
 
14
+ ### Very good and detailed example and reasons to use is considered in [railscast](http://railscasts.com/episodes/324-passing-data-to-javascript) by Ryan Bates
15
+
12
16
  When you need to send some start data from your controller to your js
13
17
  you might be doing something like this:
14
18
 
@@ -124,6 +128,14 @@ alert(customNamespace.yourHash)
124
128
  Now you can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).
125
129
  The way of writing Rabl templates is very clearly described in their repo.
126
130
 
131
+ Add Rabl to your Gemfile
132
+
133
+ `Gemfile`
134
+
135
+ ``` ruby
136
+ gem 'rabl'
137
+ ```
138
+
127
139
  Profit of using Rabl with gon:
128
140
 
129
141
  1. You can clean your controllers now!
@@ -292,7 +304,7 @@ Special thanks to @brainopia, @kossnocorp and @ai.
292
304
 
293
305
  The MIT License
294
306
 
295
- Copyright (c) 2011 gazay
307
+ Copyright (c) 2011-2012 gazay
296
308
 
297
309
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
298
310
 
data/doc/logo.png ADDED
Binary file
Binary file
data/gon.gemspec CHANGED
@@ -19,8 +19,8 @@ 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
22
  s.add_dependency "json"
23
+ s.add_development_dependency "rabl"
24
24
  s.add_development_dependency "rspec"
25
25
  s.add_development_dependency "jbuilder"
26
26
  end
data/lib/gon.rb CHANGED
@@ -4,17 +4,24 @@ end
4
4
  require 'action_view'
5
5
  require 'action_controller'
6
6
  require 'gon/helpers'
7
- require 'gon/rabl'
7
+ if defined?(Rabl)
8
+ require 'gon/rabl'
9
+ end
8
10
  if RUBY_VERSION =~ /9/ && defined?(Jbuilder)
9
11
  require 'gon/jbuilder'
10
12
  end
11
13
 
12
14
  module Gon
13
15
  class << self
16
+
14
17
  def all_variables
15
18
  @request_env[:gon]
16
19
  end
17
20
 
21
+ def all_variables=(values)
22
+ raise "You can't use Gon public methods for storing data"
23
+ end
24
+
18
25
  def clear
19
26
  @request_env[:gon] = {}
20
27
  end
@@ -35,7 +42,7 @@ module Gon
35
42
  end
36
43
 
37
44
  def request=(request_id)
38
- @request_id = request_id
45
+ @request_id = request_id
39
46
  end
40
47
 
41
48
  def method_missing(m, *args, &block)
@@ -57,9 +64,14 @@ module Gon
57
64
  @request_env[:gon][name] = value
58
65
  end
59
66
 
67
+ # TODO: Remove required argument view_path, and by default use current action
60
68
  def rabl(view_path, options = {})
61
- rabl_data = Gon::Rabl.parse_rabl(view_path, options[:controller] ||
62
- @request_env['action_controller.instance'] ||
69
+ if !defined?(Gon::Rabl)
70
+ raise NoMethodError.new('You should define Rabl in your Gemfile')
71
+ end
72
+
73
+ rabl_data = Gon::Rabl.parse_rabl(view_path, options[:controller] ||
74
+ @request_env['action_controller.instance'] ||
63
75
  @request_env['action_controller.rescue.response'].
64
76
  instance_variable_get('@template').
65
77
  instance_variable_get('@controller'))
@@ -76,13 +88,13 @@ module Gon
76
88
 
77
89
  def jbuilder(view_path, options = {})
78
90
  if RUBY_VERSION !~ /9/
79
- raise NoMethodError.new('You can use Jbuilder support only in 1.9+')
91
+ raise NoMethodError.new('You can use Jbuilder support only in 1.9+')
80
92
  elsif !defined?(Gon::Jbuilder)
81
- raise NoMethodError.new('You should define Jbuilder in your Gemfile')
93
+ raise NoMethodError.new('You should define Jbuilder in your Gemfile')
82
94
  end
83
95
 
84
- jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
85
- @request_env['action_controller.instance'] ||
96
+ jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
97
+ @request_env['action_controller.instance'] ||
86
98
  @request_env['action_controller.rescue.response'].
87
99
  instance_variable_get('@template').
88
100
  instance_variable_get('@controller'))
@@ -97,4 +109,5 @@ module Gon
97
109
  end
98
110
  end
99
111
  end
112
+
100
113
  end
data/lib/gon/helpers.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Gon
2
2
  module Helpers
3
+
3
4
  def self.included base
4
5
  base.send(:include, InstanceMethods)
5
6
  end
@@ -9,17 +10,17 @@ module Gon
9
10
  if Gon.request_env && Gon.all_variables.present?
10
11
  data = Gon.all_variables
11
12
  namespace = options[:namespace] || 'gon'
12
- script = "<script>window." + namespace + " = {};"
13
- unless options[:camel_case]
13
+ script = '<script>window.' + namespace + ' = {};'
14
+ if options[:camel_case]
14
15
  data.each do |key, val|
15
- script += namespace + "." + key.to_s + '=' + val.to_json + ";"
16
+ script << namespace + '.' + key.to_s.camelize(:lower) + '=' + val.to_json + ';'
16
17
  end
17
18
  else
18
19
  data.each do |key, val|
19
- script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
20
+ script << namespace + '.' + key.to_s + '=' + val.to_json + ';'
20
21
  end
21
22
  end
22
- script += "</script>"
23
+ script << '</script>'
23
24
  script.html_safe
24
25
  else
25
26
  ""
@@ -43,6 +44,7 @@ module Gon
43
44
  end
44
45
  end
45
46
  end
47
+
46
48
  end
47
49
 
48
50
  ActionView::Base.send :include, Gon::Helpers
data/lib/gon/jbuilder.rb CHANGED
@@ -41,6 +41,7 @@ module Gon
41
41
  end
42
42
  end.flatten
43
43
  end
44
+
44
45
  end
45
46
  end
46
47
  end
data/lib/gon/rabl.rb CHANGED
@@ -3,12 +3,14 @@ require 'rabl'
3
3
  module Gon
4
4
  module Rabl
5
5
  class << self
6
+
6
7
  def parse_rabl(rabl_path, controller)
7
8
  source = File.read(rabl_path)
8
9
  rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
9
10
  output = rabl_engine.render(controller, {})
10
11
  JSON.parse(output)
11
12
  end
13
+
12
14
  end
13
15
  end
14
16
  end
data/lib/gon/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gon
2
- VERSION = '2.0.6'
2
+ VERSION = '2.1.0'
3
3
  end
data/spec/gon/gon_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # gon_spec_rb
2
2
  require 'gon'
3
3
 
4
- describe Gon, '#all_variables' do
4
+ describe Gon, '#all_variables' do
5
5
 
6
6
  before(:each) do
7
7
  Gon.request_env = {}
@@ -41,13 +41,26 @@ describe Gon, '#all_variables' do
41
41
  lambda { Gon.all_variables = 123 }.should raise_error
42
42
  end
43
43
 
44
- it 'render json from rabl template' do
45
- Gon.clear
46
- controller = ActionController::Base.new
47
- objects = [1,2]
48
- controller.instance_variable_set('@objects', objects)
49
- Gon.rabl 'spec/test_data/sample.rabl', :controller => controller
50
- Gon.objects.length.should == 2
44
+ context 'render json from rabl template' do
45
+ before :each do
46
+ Gon.clear
47
+ controller.instance_variable_set('@objects', objects)
48
+ end
49
+
50
+ let(:controller) { ActionController::Base.new}
51
+ let(:objects) { [1,2]}
52
+
53
+ it 'raises an error if rabl is not included' do
54
+ expect { Gon.rabl 'spec/test_data/sample.rabl', :controller => controller}.to raise_error
55
+ end
56
+
57
+ it 'works if rabl is included' do
58
+ require 'rabl'
59
+ require 'gon/rabl'
60
+ Gon.rabl 'spec/test_data/sample.rabl', :controller => controller
61
+ Gon.objects.length.should == 2
62
+ end
63
+
51
64
  end
52
65
 
53
66
  if RUBY_VERSION =~ /1.9/
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gon
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ hash: 11
5
+ prerelease:
6
+ segments:
7
+ - 2
8
+ - 1
9
+ - 0
10
+ version: 2.1.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - gazay
@@ -9,59 +15,80 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2012-01-23 00:00:00 +08:00
13
- default_executable:
18
+ date: 2012-02-15 00:00:00 Z
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: actionpack
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 2
31
+ - 3
32
+ - 0
23
33
  version: 2.3.0
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: rabl
27
34
  type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
30
41
  requirements:
31
42
  - - ">="
32
43
  - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
33
47
  version: "0"
34
- version:
35
- - !ruby/object:Gem::Dependency
36
- name: json
37
48
  type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rabl
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
40
55
  requirements:
41
56
  - - ">="
42
57
  - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
43
61
  version: "0"
44
- version:
62
+ type: :development
63
+ version_requirements: *id003
45
64
  - !ruby/object:Gem::Dependency
46
65
  name: rspec
47
- type: :development
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
50
69
  requirements:
51
70
  - - ">="
52
71
  - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
53
75
  version: "0"
54
- version:
76
+ type: :development
77
+ version_requirements: *id004
55
78
  - !ruby/object:Gem::Dependency
56
79
  name: jbuilder
57
- type: :development
58
- version_requirement:
59
- version_requirements: !ruby/object:Gem::Requirement
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
60
83
  requirements:
61
84
  - - ">="
62
85
  - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
63
89
  version: "0"
64
- version:
90
+ type: :development
91
+ version_requirements: *id005
65
92
  description: If you need to send some data to your js files and you don't want to do this with long way trough views and parsing - use this force!
66
93
  email:
67
94
  - alex.gaziev@gmail.com
@@ -76,6 +103,8 @@ files:
76
103
  - Gemfile
77
104
  - README.md
78
105
  - Rakefile
106
+ - doc/logo.png
107
+ - doc/logo_small.png
79
108
  - gon.gemspec
80
109
  - lib/gon.rb
81
110
  - lib/gon/helpers.rb
@@ -87,7 +116,6 @@ files:
87
116
  - spec/test_data/sample.json.jbuilder
88
117
  - spec/test_data/sample.rabl
89
118
  - spec/test_data/sample_with_partial.json.jbuilder
90
- has_rdoc: true
91
119
  homepage: https://github.com/gazay/gon
92
120
  licenses: []
93
121
 
@@ -97,21 +125,27 @@ rdoc_options: []
97
125
  require_paths:
98
126
  - lib
99
127
  required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
100
129
  requirements:
101
130
  - - ">="
102
131
  - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
103
135
  version: "0"
104
- version:
105
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
106
138
  requirements:
107
139
  - - ">="
108
140
  - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
109
144
  version: "0"
110
- version:
111
145
  requirements: []
112
146
 
113
147
  rubyforge_project: gon
114
- rubygems_version: 1.3.5
148
+ rubygems_version: 1.8.15
115
149
  signing_key:
116
150
  specification_version: 3
117
151
  summary: Get your Rails variables in your JS