Module: Unity::Captcha::ViewHelpers

Defined in:
lib/unity/captcha/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#captcha_for(form = nil, options = {}) ⇒ String

Helper method to generate a Unity Captcha field in forms

Parameters:

  • form (FormBuilder) (defaults to: nil)

    The form object

  • options (Hash) (defaults to: {})

    Options for customizing the captcha

Options Hash (options):

  • :label (String)

    The label for the captcha field

  • :shapes (Array<String>)

    List of shapes to use

  • :error_msg (String)

    Error message to display

  • :success_msg (String)

    Success message to display

  • :form_id (String)

    ID for the form (defaults to “mc-form”)

  • :canvas_id (String)

    ID for the canvas (defaults to “mc-canvas”)

  • :html_options (Hash)

    Additional HTML options for the canvas

Returns:

  • (String)

    HTML markup for the captcha



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
# File 'lib/unity/captcha/view_helpers.rb', line 17

def captcha_for(form = nil, options = {})
  # Create the captcha object if not passed
  @captcha ||= Unity::Captcha::Captcha.new
  
  options = {
    label: 'Please draw the shape in the box to submit the form:',
    shapes: ['triangle', 'x', 'rectangle', 'circle', 'check', 'zigzag', 'arrow', 'delete', 'pigtail', 'star'],
    error_msg: 'Please try again.',
    success_msg: 'Captcha passed!',
    form_id: 'mc-form',
    canvas_id: 'mc-canvas',
    html_options: {}
  }.merge(options)
  
  # Include required assets
  output = javascript_include_tag("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js") unless options[:skip_jquery]
  output = (output || '').html_safe
  output += javascript_include_tag("jquery.motionCaptcha.1.0", "jquery.placeholder.1.1.1.min")
  output += stylesheet_link_tag("jquery.motionCaptcha.1.0")
  
  # Generate the form ID - if we're in a form, use that form's ID or set it
  form_id = options[:form_id]
  if form.present?
    # If we're in a form_for/simple_form_for block
    form.object_name # make sure it's a form object
    
    # Math captcha question field
    output += form.label :captcha, @captcha.question
    output += form.text_field :captcha
    output += form.hidden_field :captcha_secret, value: @captcha.encrypt
    
    # Drawing captcha
    output += (:p, options[:label].html_safe + 
               (:a, ' (new shape)', href: '#', 
                          onclick: "window.location.reload()", 
                          title: "Click for a new shape"))
    output += (:canvas, '', { id: options[:canvas_id] }.merge(options[:html_options]))
    
    # Hidden action field
    action_path = options[:action_path] || request.path
    output += form.hidden_field :mc_action, value: action_path
  else
    # If we're just rendering the captcha standalone
    output += label_tag :captcha, @captcha.question
    output += text_field_tag :captcha
    output += hidden_field_tag :captcha_secret, @captcha.encrypt
    
    # Drawing captcha
    output += (:p, options[:label].html_safe + 
               (:a, ' (new shape)', href: '#', 
                          onclick: "window.location.reload()", 
                          title: "Click for a new shape"))
    output += (:canvas, '', { id: options[:canvas_id] }.merge(options[:html_options]))
    
    # Hidden action field
    action_path = options[:action_path] || request.path
    output += hidden_field_tag :mc_action, action_path
  end
  
  # Initialize the javascript
  shapes_json = options[:shapes].to_json
  script = "  <script type=\"text/javascript\">\n    jQuery(document).ready(function($) {\n      $('#\#{form_id}').motionCaptcha({\n        shapes: \#{shapes_json},\n        errorMsg: \"\#{options[:error_msg]}\",\n        successMsg: \"\#{options[:success_msg]}\"\n      });\n      $(\"input.placeholder\").placeholder();\n    });\n  </script>\n  JAVASCRIPT\n  \n  output += script.html_safe\n  output\nend\n"