Skip to content

Commit 2d12a83

Browse files
committed
Added partial
1 parent d55978f commit 2d12a83

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

lib/gon.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'action_view'
22
require 'action_controller'
33
require 'gon/helpers'
4+
require 'gon/partial'
45

56
module Gon
67
def self.all_variables
@@ -25,7 +26,7 @@ def self.method_missing(m, *args, &block)
2526
@request_env[:gon] ||= {}
2627

2728
if ( m.to_s =~ /=$/ )
28-
if self.public_methods.include? m[0..-2].to_sym
29+
if self.public_methods.include? m.to_s[0..-2].to_sym
2930
raise "You can't use Gon public methods for storing data"
3031
end
3132
set_variable(m.to_s.delete('='), args[0])
@@ -41,4 +42,9 @@ def self.get_variable(name)
4142
def self.set_variable(name, value)
4243
@request_env[:gon][name] = value
4344
end
45+
46+
def self.[](object)
47+
request_env[:partials] ||= {}
48+
request_env[:partials][object] ||= Gon::Partial.new
49+
end
4450
end

lib/gon/helpers.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,38 @@ def include_gon(options = {})
1919
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
2020
end
2121
end
22+
script += partials_js(namespace)
2223
script += "</script>"
2324
script.html_safe
2425
else
2526
""
2627
end
2728
end
29+
30+
def gon_partials(object, options = {})
31+
if Gon.request_env
32+
data = Gon[object].all_variables
33+
attribute = "data-gon-partial={"
34+
unless options[:camel_case]
35+
data.each do |key, val|
36+
attribute += '"' + key.to_s + '":' + val.to_json + ","
37+
end
38+
else
39+
data.each do |key, val|
40+
attribute += '"' + key.to_s.camelize(:lower) + '":' + val.to_json + ","
41+
end
42+
end
43+
attribute = attribute[0..-2] + "}"
44+
attribute.html_safe
45+
end
46+
end
47+
48+
def partials_js(namespace)
49+
"window." + namespace + ".getPartial = function(domObject) {" +
50+
"data = domObject.getAttribute('data-gon-partial');" +
51+
"return JSON.parse(data);" +
52+
"};"
53+
end
2854
end
2955
end
3056

@@ -45,4 +71,4 @@ def gon
4571
end
4672

4773
ActionView::Base.send :include, Gon::Helpers
48-
ActionController::Base.send :include, Gon::GonHelpers
74+
ActionController::Base.send :include, Gon::GonHelpers

lib/gon/partial.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Gon::Partial
2+
def initialize()
3+
@data = {}
4+
end
5+
6+
def clear
7+
@data = {}
8+
end
9+
10+
def method_missing(m, *args, &block)
11+
@data ||= {}
12+
13+
if ( m.to_s =~ /=$/ )
14+
if public_methods.include? m.to_s[0..-2].to_sym
15+
raise "You can't use Gon public methods for storing data"
16+
end
17+
set_variable(m.to_s.delete('='), args[0])
18+
else
19+
get_variable(m.to_s)
20+
end
21+
end
22+
23+
def set_variable(name, value)
24+
@data[name] = value
25+
end
26+
27+
def get_variable(name)
28+
@data[name]
29+
end
30+
31+
def all_variables
32+
@data
33+
end
34+
end

0 commit comments

Comments
 (0)