Skip to content

Commit

Permalink
Use Sinatra#helpers API properly
Browse files Browse the repository at this point in the history
* Also use Sinatra::Base only
  - ensures Padrino compat
  • Loading branch information
skade committed Jan 11, 2012
1 parent 3b039b7 commit c350f65
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions lib/gon/sinatra/helpers.rb
Expand Up @@ -3,51 +3,38 @@
module Gon module Gon
module Sinatra module Sinatra
module Helpers module Helpers
def self.included base def include_gon(options = {})
base.send(:include, InstanceMethods) if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present?
end data = Gon::Sinatra.all_variables

namespace = options[:namespace] || 'gon'
module InstanceMethods script = "<script>window." + namespace + " = {};"
def include_gon(options = {}) unless options[:camel_case]
if Gon::Sinatra.request_env && Gon::Sinatra.all_variables.present? data.each do |key, val|
data = Gon::Sinatra.all_variables script += namespace + "." + key.to_s + '=' + val.to_json + ";"
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
end end
script += "</script>"
script
else else
"" data.each do |key, val|
script += namespace + "." + key.to_s.camelize(:lower) + '=' + val.to_json + ";"
end
end end
script += "</script>"
script
else
""
end end
end end
end end


module GonHelpers module GonHelpers
def self.included base def gon
base.send(:include, InstanceMethods) if !Gon::Sinatra.request_env || Gon::Sinatra.request != request.object_id
end Gon::Sinatra.request = request.object_id

Gon::Sinatra.request_env = request.env
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
end end
Gon::Sinatra
end end
end end
end end
end end


Sinatra::Application.send :include, Gon::Sinatra::Helpers Sinatra::Base.helpers Gon::Sinatra::GonHelpers, Gon::Sinatra::Helpers
Sinatra::Base.send :include, Gon::Sinatra::GonHelpers

0 comments on commit c350f65

Please sign in to comment.