Module: RecaptchaMailhide::ActionViewHelper

Defined in:
lib/recaptcha_mailhide/helpers/action_view_helper.rb

Instance Method Summary collapse

Instance Method Details

#recaptcha_mailhide(*args, &block) ⇒ Object

Generates a link tag to a ReCAPTCHA Mailhide URL for the given email address.

If a block is given it will use it to generate the content, and takes these attributes:

  • email - The email address to hide

  • options - See options below

When no block is given it accepts two forms:

# Just email
recaptcha_mailhide(email, options = {})

# Email and content
recaptcha_mailhide(content, email, options = {})

In the first instance it will process the email with truncate_email and use it as the link content.

Options

Accepts every option supported by link_to, plus:

:popup

Set it to true to have the link open a popup window (through JavaScript and the onclick event).

Can also be a hash of sub-options, which can be :width and :height, setting the size of the popup window.

In case of not supplying your own content you can also pass options for truncate_email, e.g.:

recaptcha_mailhide('[email protected]', omission: '-', truncate_domain: true)

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/recaptcha_mailhide/helpers/action_view_helper.rb', line 40

def recaptcha_mailhide(*args, &block)
  options = args.extract_options!
  raise ArgumentError, "at least one argument is required (not counting options)" if args.empty?

  if block_given?
    url = RecaptchaMailhide.url_for(args.first)
    link_to(url, recaptcha_mailhide_options(url, options), &block)
  else
    if args.length == 1
      content = truncate_email(args.first, options)
      url = RecaptchaMailhide.url_for(args.first)
    else
      content = args.first
      url = RecaptchaMailhide.url_for(args.second)
    end
    link_to(content, url, recaptcha_mailhide_options(url, options))
  end
end

#truncate_email(email, options = {}) ⇒ Object

Truncates an email address, e.g.:

truncate_email('[email protected]') # => e…@example.com
truncate_email('[email protected]', truncate_domain: true) # => e…@e…

Options

:omission

The string to use in replacement of the removed characters.

:truncate_domain

Boolean. Whether to truncate the domain part of the address as well.



74
75
76
77
78
79
80
81
# File 'lib/recaptcha_mailhide/helpers/action_view_helper.rb', line 74

def truncate_email(email, options = {})
  return "" unless email.match(/@/)
  split_email = email.split('@')
  omission = options[:omission] || "…"
  local_part = "#{split_email.first.first}#{omission}"
  domain = options[:truncate_domain] ? "#{split_email.last.first}#{omission}" : split_email.last
  "#{local_part}@#{domain}"
end