module InvisibleCaptcha::ViewHelpers

Public Instance Methods

invisible_captcha(honeypot = nil, scope = nil, options = {}) click to toggle source

Builds the honeypot html

@param honeypot [Symbol] name of honeypot, ie: subtitle => input name: subtitle @param scope [Symbol] name of honeypot scope, ie: topic => input name: topic @param options [Hash] html_options for input and invisible_captcha options

@return [String] the generated html

# File lib/invisible_captcha/view_helpers.rb, line 12
def invisible_captcha(honeypot = nil, scope = nil, options = {})
  @captcha_ocurrences = 0 unless defined?(@captcha_ocurrences)
  @captcha_ocurrences += 1

  if InvisibleCaptcha.timestamp_enabled || InvisibleCaptcha.spinner_enabled
    session[:invisible_captcha_timestamp] = Time.zone.now.iso8601
  end

  if InvisibleCaptcha.spinner_enabled && @captcha_ocurrences == 1
    session[:invisible_captcha_spinner] = InvisibleCaptcha.encode("#{session[:invisible_captcha_timestamp]}-#{current_request.remote_ip}")
  end

  build_invisible_captcha(honeypot, scope, options)
end
invisible_captcha_styles() click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 27
def invisible_captcha_styles
  if content_for?(:invisible_captcha_styles)
    content_for(:invisible_captcha_styles)
  end
end

Private Instance Methods

build_input_name(honeypot, scope = nil) click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 89
def build_input_name(honeypot, scope = nil)
  if scope.present?
    "#{scope}[#{honeypot}]"
  else
    honeypot
  end
end
build_invisible_captcha(honeypot = nil, scope = nil, options = {}) click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 39
def build_invisible_captcha(honeypot = nil, scope = nil, options = {})
  if honeypot.is_a?(Hash)
    options = honeypot
    honeypot = nil
  end

  honeypot  = honeypot ? honeypot.to_s : InvisibleCaptcha.get_honeypot
  label     = options.delete(:sentence_for_humans) || InvisibleCaptcha.sentence_for_humans
  css_class = "#{honeypot}_#{Time.zone.now.to_i}"

  styles = visibility_css(css_class, options)

  provide(:invisible_captcha_styles) do
    styles
  end if InvisibleCaptcha.injectable_styles

  content_tag(:div, class: css_class) do
    concat styles unless InvisibleCaptcha.injectable_styles
    concat label_tag(build_label_name(honeypot, scope), label)
    concat text_field_tag(build_input_name(honeypot, scope), nil, default_honeypot_options.merge(options))
    if InvisibleCaptcha.spinner_enabled
      concat hidden_field_tag("spinner", session[:invisible_captcha_spinner], id: nil)
    end
  end
end
build_label_name(honeypot, scope = nil) click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 81
def build_label_name(honeypot, scope = nil)
  if scope.present?
    "#{scope}_#{honeypot}"
  else
    honeypot
  end
end
current_request() click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 35
def current_request
  @request ||= request
end
default_honeypot_options() click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 97
def default_honeypot_options
  { autocomplete: 'off', tabindex: -1 }
end
visibility_css(css_class, options) click to toggle source
# File lib/invisible_captcha/view_helpers.rb, line 65
def visibility_css(css_class, options)
  visible = if options.key?(:visual_honeypots)
    options.delete(:visual_honeypots)
  else
    InvisibleCaptcha.visual_honeypots
  end

  return if visible

  nonce = content_security_policy_nonce if options[:nonce]

  content_tag(:style, media: 'screen', nonce: nonce) do
    ".#{css_class} {#{InvisibleCaptcha.css_strategy}}"
  end
end