Class: Incline::Recaptcha::Tag

Inherits:
ActionView::Helpers::Tags::Base
  • Object
show all
Defined in:
lib/incline/recaptcha.rb

Overview

Defines a reCAPTCHA tag that can be used to supply a field in a model with a hash of values.

Basically we define two fields for the model attribute, one for :remote_ip and one for :response. The :remote_ip field is set automatically and shouldn’t be changed. The :response field is set when the user completes the challenge.

Incline::Recaptcha::Tag.new(my_model, :is_robot).render

<input type="hidden" name="my_model[is_robot][remote_ip]" id="my_model_is_robot_remote_ip" value="10.11.12.13">
<input type="hidden" name="my_model[is_robot][response]" id="my_model_is_robot_response" value="">

Incline::Recaptcha::verify model: my_model, attribute: :is_robot

Instance Method Summary collapse

Instance Method Details

#renderObject

Generates the reCAPTCHA data.



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/incline/recaptcha.rb', line 41

def render
  remote_ip =
      if @template_object&.respond_to?(:request) && @template_object.send(:request)&.respond_to?(:remote_ip)
        @template_object.request.remote_ip
      else
        ENV['REMOTE_ADDR']
      end

  if Incline::Recaptcha::disabled?
    # very simple, if recaptcha is disabled, send the IP and 'disabled' to the form.
    # for validation, recaptcha must still be disabled or it will fail.
    return tag('input', type: 'hidden', id: tag_id, name: tag_name, value: "#{remote_ip}|disabled")
  end

  # reCAPTCHA is not disabled, so put everything we need into the form.
  ret =   tag('input', type: 'hidden', id: tag_id, name: tag_name, value: remote_ip)
  ret +=  "\n"

  div_id = tag_id + '_div'

  ret +=  tag('div', { class: 'form-group' }, true)
  ret +=  tag('div', { id: div_id }, true)
  ret +=  "</div></div>\n".html_safe

  sitekey = CGI::escape_html(Incline::Recaptcha::public_key)
  onload = 'onload_' + tag_id
  callback = 'update_' + tag_id
  tabindex = @options[:tab_index].to_s.to_i
  theme = make_valid(@options[:theme], VALID_THEMES, :light).to_s
  type = make_valid(@options[:type], VALID_TYPES, :image).to_s
  size = make_valid(@options[:size], VALID_SIZES, :normal).to_s


  ret += <<-EOS.html_safe
<script type="text/javascript">
// <![CDATA[
function #{onload}() {
  grecaptcha.render(#{div_id.inspect}, {
    "sitekey"     : #{sitekey.inspect},
    "callback"    : #{callback.inspect},
    "tabindex"    : #{tabindex},
    "theme"       : #{theme.inspect},
    "type"        : #{type.inspect},
    "size"        : #{size.inspect}
  });
}
function #{callback}(response) {
  var fld = $('##{tag_id}');
  var val = fld.val();
  if (val) { val = val.split('|'); val = val[0]; } else { val = ''; }
  fld.val(val + '|' + response);
}
// ]]>
</script>
  EOS

  Incline::Recaptcha::onload_callbacks << onload

  ret.html_safe
end