Module: Rack::Recaptcha::Helpers
- Defined in:
- lib/rack/recaptcha/helpers.rb
Constant Summary collapse
- DEFAULT =
{ :height => 300, :width => 500, :row => 3, :cols => 5 }
Instance Method Summary collapse
-
#recaptcha_tag(type = :noscript, options = {}) ⇒ Object
Helper method to output a recaptcha form.
-
#recaptcha_valid? ⇒ Boolean
Helper to return whether the recaptcha was accepted.
Instance Method Details
#recaptcha_tag(type = :noscript, options = {}) ⇒ Object
Helper method to output a recaptcha form. Some of the available types you can have are:
:challenge - Returns a javascript recaptcha form :noscript - Return a non-javascript recaptcha form :ajax - Return a ajax recaptcha form
You also have a few available options:
For :ajax:
:display - You can adjust the display of the ajax form. An example:
recaptcha_tag :ajax, :display => {:theme => 'red'}.
For :challenge and :noscript
:public_key - Set the public key. Overrides the key set in Middleware option
:height - Adjust the height of the form
:width - Adjust the width of the form
:row - Adjust the rows for the challenge field
:cols - Adjust the column for the challenge field
:language - Set the language
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rack/recaptcha/helpers.rb', line 33 def recaptcha_tag(type= :noscript, ={}) = DEFAULT.merge() [:public_key] ||= Rack::Recaptcha.public_key path = [:ssl] ? Rack::Recaptcha::API_SECURE_URL : Rack::Recaptcha::API_URL params = "k=#{[:public_key]}" params += "&hl=" + uri_parser.escape([:language].to_s) if [:language] = request.env['recaptcha.msg'] if defined?(request) params += "&error=" + uri_parser.escape() unless .nil? html = case type.to_sym when :challenge %{<script type="text/javascript" src="#{path}/challenge?#{params}"> </script>}.gsub(/^ +/, '') when :noscript %{<noscript> <iframe src="#{path}/noscript?#{params}" height="#{[:height]}" width="#{[:width]}" frameborder="0"></iframe><br> <textarea name="recaptcha_challenge_field" rows="#{[:row]}" cols="#{[:cols]}"></textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"> </noscript>}.gsub(/^ +/, '') when :ajax %{<div id="ajax_recaptcha"></div> <script type="text/javascript" src="#{path}/js/recaptcha_ajax.js"></script> <script type="text/javascript"> Recaptcha.create('#{[:public_key]}', document.getElementById('ajax_recaptcha')#{[:display] ? ',RecaptchaOptions' : ''}); </script>}.gsub(/^ +/, '') else '' end if [:display] %{<script type="text/javascript"> var RecaptchaOptions = #{[:display].to_json}; </script>}.gsub(/^ +/, '') else '' end + html end |