Class: ActionView::Helpers::InstanceTag

Inherits:
Object
  • Object
show all
Defined in:
lib/bot-away/action_view/helpers/instance_tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#spinnerObject (readonly)

Returns the value of attribute spinner.



2
3
4
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 2

def spinner
  @spinner
end

Instance Method Details

#assuming(object) ⇒ Object



28
29
30
31
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 28

def assuming(object)
  yield if object
  object
end

#content_tag_with_obfuscation(name, content_or_options_with_block = nil, options = nil, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 62

def (name, content_or_options_with_block = nil, options = nil, *args, &block)
  if block_given?
    (name, content_or_options_with_block, options, *args, &block)
  else
    # this should cover all Rails selects.
    if spinner && options && (options.keys.include?('id') || options.keys.include?('name'))
      if name == 'select' && !content_or_options_with_block.empty?
        content = '<option selected value=""></option>'
      else
        content = ""
      end
      disguise((name, content, honeypot_options(options), *args)) +
              (name, content_or_options_with_block, obfuscate_options(options), *args)
    else
      (name, content_or_options_with_block, options, *args)
    end
  end
end

#disguise(element) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 86

def disguise(element)
  case rand(3)
    when 0 # Hidden
      element.replace "<div style='display:none;'>Leave this empty: #{element}</div>"
    when 1 # Off-screen
      element.replace "<div style='position:absolute;left:-1000px;top:-1000px;'>Don't fill this in: #{element}</div>"
    when 2 # Negligible size
      element.replace "<div style='position:absolute;width:0px;height:1px;z-index:-1;color:transparent;overflow:hidden;'>Keep this blank: #{element}</div>"
    else # this should never happen?
      disguise(element)
  end
end

#honeypot_options(options) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 19

def honeypot_options(options)
  add_default_name_and_id(options)
  assuming(spinner && options) do
    options['value'] &&= ''
    options['autocomplete'] = 'off'
    options['tabindex'] = -rand(10) - 1
  end
end

#honeypot_tag(name, options = nil, *args) ⇒ Object



33
34
35
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 33

def honeypot_tag(name, options = nil, *args)
  tag_without_honeypot(name, honeypot_options(options.dup? || {}), *args)
end

#initialize_with_spinner(object_name, method_name, template_object, object = nil) ⇒ Object



4
5
6
7
8
9
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 4

def initialize_with_spinner(object_name, method_name, template_object, object = nil)
  initialize_without_spinner(object_name, method_name, template_object, object)
  if template_object.controller.send(:protect_against_forgery?)
    @spinner = BotAway::Spinner.new(template_object.request.ip, object_name, template_object.form_authenticity_token)
  end
end

#obfuscate_options(options) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 11

def obfuscate_options(options)
  add_default_name_and_id(options)
  assuming(spinner && options) do
    options['name'] &&= spinner.encode(options['name'])
    options['id'] &&= spinner.encode(options['id'])
  end
end

#obfuscated_tag(name, options = nil, *args) ⇒ Object



37
38
39
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 37

def obfuscated_tag(name, options = nil, *args)
  tag_without_honeypot(name, obfuscate_options(options.dup? || {}), *args)
end

#tag_with_honeypot(name, options = nil, *args) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 41

def tag_with_honeypot(name, options = nil, *args)
  if spinner
    obfuscated_tag(name, options, *args) + disguise(honeypot_tag(name, options, *args))
  else
    tag_without_honeypot(name, options, *args)
  end
end

#to_label_tag_with_obfuscation(text = nil, options = {}) ⇒ Object

Special case



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bot-away/action_view/helpers/instance_tag.rb', line 50

def to_label_tag_with_obfuscation(text = nil, options = {})
  # TODO: Can this be simplified? It's pretty similar to to_label_tag_without_obfuscation...
  options = options.stringify_keys
  tag_value = options.delete("value")
  name_and_id = options.dup
  name_and_id["id"] = name_and_id["for"]
  add_default_name_and_id_for_value(tag_value, name_and_id)
  options["for"] ||= name_and_id["id"]
  options["for"] = spinner.encode(options["for"]) if spinner && options["for"]
  to_label_tag_without_obfuscation(text, options)
end