Permalink
Browse files

Use Sinatra#helpers API properly

* Also use Sinatra::Base only
  - ensures Padrino compat
  • Loading branch information...
skade committed Jan 11, 2012
1 parent 3b039b7 commit c350f653941fbf0960c841be9887a14bafcd130c
Showing with 21 additions and 34 deletions.
  1. +21 −34 lib/gon/sinatra/helpers.rb
View
@@ -3,51 +3,38 @@
module Gon
module Sinatra
module Helpers
- def self.included base
- base.send(:include, InstanceMethods)
- end
-
- module InstanceMethods
- def include_gon(options = {})
- if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present?
- data = Gon::Sinatra.all_variables
- namespace = options[:namespace] || 'gon'
- script = "<script>window." + namespace + " = {};"
- unless options[:camel_case]
- data.each do |key, val|
- script += namespace + "." + key.to_s + '=' + val.to_json + ";"
- end
- else
- data.each do |key, val|
- script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
- end
+ def include_gon(options = {})
+ if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present?
+ data = Gon::Sinatra.all_variables
+ namespace = options[:namespace] || 'gon'
+ script = "<script>window." + namespace + " = {};"
+ unless options[:camel_case]
+ data.each do |key, val|
+ script += namespace + "." + key.to_s + '=' + val.to_json + ";"
end
- script += "</script>"
- script
else
- ""
+ data.each do |key, val|
+ script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
+ end
end
+ script += "</script>"
+ script
+ else
+ ""
end
end
end
module GonHelpers
- def self.included base
- base.send(:include, InstanceMethods)
- end
-
- module InstanceMethods
- def gon
- if !Gon::Sinatra.request_env || Gon::Sinatra.request != request.object_id
- Gon::Sinatra.request = request.object_id
- Gon::Sinatra.request_env = request.env
- end
- Gon::Sinatra
+ def gon
+ if !Gon::Sinatra.request_env || Gon::Sinatra.request != request.object_id
+ Gon::Sinatra.request = request.object_id
+ Gon::Sinatra.request_env = request.env
end
+ Gon::Sinatra
end
end
end
end
-Sinatra::Application.send :include, Gon::Sinatra::Helpers
-Sinatra::Base.send :include, Gon::Sinatra::GonHelpers
+Sinatra::Base.helpers Gon::Sinatra::GonHelpers, Gon::Sinatra::Helpers

0 comments on commit c350f65

Please sign in to comment.