module Recaptcha::ClientHelper

Public Instance Methods

recaptcha_tags(options = {}) click to toggle source

Your public API can be specified in the options hash or preferably using the Configuration.

# File lib/recaptcha/client_helper.rb, line 5
def recaptcha_tags(options = {})
  # Default options
  key   = options[:public_key] ||= Recaptcha.configuration.public_key
  raise RecaptchaError, "No public key specified." unless key
  error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "")
  uri   = Recaptcha.configuration.api_server_url(options[:ssl])
  html  = ""
  if options[:display]
    html << %Q{<script type="text/javascript">\n}
    html << %Q{  var RecaptchaOptions = #{options[:display].to_json};\n}
    html << %Q{</script>\n}
  end
  if options[:ajax]
    html << %Q{<div id="dynamic_recaptcha"></div>}
    html << %Q{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n}
    html << %Q{<script type="text/javascript">\n}
    html << %Q{  Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? ',RecaptchaOptions' : ''});}
    html << %Q{</script>\n}
  else
    html << %Q{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
    html << %Q{#{error ? "&amp;error=#{CGI::escape(error)}" : ""}"></script>\n}
    unless options[:noscript] == false
      html << %Q{<noscript>\n  }
      html << %Q{<iframe src="#{uri}/noscript?k=#{key}" }
      html << %Q{height="#{options[:iframe_height] ||= 300}" }
      html << %Q{width="#{options[:iframe_width]   ||= 500}" }
      html << %Q{style="border:none;"></iframe><br/>\n  }
      html << %Q{<textarea name="recaptcha_challenge_field" }
      html << %Q{rows="#{options[:textarea_rows] ||= 3}" }
      html << %Q{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n  }
      html << %Q{<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>}
      html << %Q{</noscript>\n}
    end
  end
  return html.html_safe
end