Module: Panmind::Recaptcha::Helpers

Defined in:
lib/panmind/recaptcha.rb

Overview

Controller

Instance Method Summary collapse

Instance Method Details

#recaptcha(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/panmind/recaptcha.rb', line 94

def recaptcha(options = {})
  return unless Recaptcha.enabled?

  if Recaptcha.private_key.blank? || Recaptcha.public_key.blank?
    raise ConfigurationError, 'ReCaptcha keys are missing'
  end

  label_text = options.delete(:label) || 'Enter the following words'

  noscript_options = {:width => 420, :height => 320}.merge(
    options.delete(:noscript) || {})

  recaptcha_options =
    options.empty? ? '' :
    javascript_tag(%[var RecaptchaOptions = #{options.to_json}])

  label_tag('recaptcha_response_field', label_text) + recaptcha_options +
  %[<script type="text/javascript"
       src="https://api-secure.recaptcha.net/challenge?k=#{Recaptcha.public_key}">
    </script>

    <noscript>
       <iframe src="https://api-secure.recaptcha.net/noscript?k=#{Recaptcha.public_key}"
           height="#{noscript_options[:width]}" width="#{noscript_options[:height]}" frameborder="0"></iframe><br>
       <input type="text" class="text" name="recaptcha_challenge_field" tabindex="#{options[:tabindex]}"/>
       <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
    </noscript>
  ].html_safe
end