Module: Giggly::Javascript::Helper

Included in:
Socialize
Defined in:
lib/giggly/javascript/helper.rb

Instance Method Summary collapse

Instance Method Details

#escape_javascript(javascript) ⇒ Object

from rails



39
40
41
# File 'lib/giggly/javascript/helper.rb', line 39

def escape_javascript(javascript)
  javascript.gsub(/\r\n|\n|\r/, "\\n").gsub("\"") { |m| "\\#{m}" }
end

#include_gigya_api(service) ⇒ Object

The Javascript helper should be totally generic to the api that is being used So that if there is a future api it can be wrapped in a class and include this



8
9
10
# File 'lib/giggly/javascript/helper.rb', line 8

def include_gigya_api(service)
  '<script type="text/javascript" src="http://cdn.gigya.com/JS/gigya.js?services=' + service.to_s + '"></script>'
end

#javascript(&block) ⇒ Object



12
13
14
15
16
17
# File 'lib/giggly/javascript/helper.rb', line 12

def javascript(&block)
  out = '<script type="text/javascript">' + "\n"
  out += yield.to_s
  out += "\n" + '</script>'
  out
end

#javascriptify_object(object) ⇒ Object

borrowed mainly from the ym4r_gm plugin



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/giggly/javascript/helper.rb', line 24

def javascriptify_object(object)
  if object.is_a? String
    "'#{escape_javascript object}'"
  elsif object.is_a? Array
    '[' + object.collect{ |o| javascriptify_variable(o) }.join(', ') + ']'
  elsif object.is_a? Hash
    '{' + object.to_a.collect{ |o| "#{o[0].to_s} : #{javascriptify_object(o[1])}" }.join(', ') + '}'
  elsif object.nil?
    'undefined'
  else
    object.to_s
  end
end

#to_var(name, object, scope_to_window = false) ⇒ Object



19
20
21
# File 'lib/giggly/javascript/helper.rb', line 19

def to_var(name, object, scope_to_window = false)
  "#{scope_to_window ? '' : 'var '}#{name.to_s} = #{javascriptify_object object};"
end