Sha256: b0b6fd7201fd24ffa52abee9a1336ff683e15cb05df804cff340da97f96414b9

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

module InvisibleCaptcha
  module ViewHelpers

    def invisible_captcha(resource = nil, method = nil)
      build_invisible_captcha(resource, method)
    end

    private

    def build_invisible_captcha(resource = nil, method = nil)
      resource   = resource ? resource.to_s : InvisibleCaptcha.fake_field
      label      = InvisibleCaptcha.sentence_for_humans
      html_id    = generate_html_id(resource)

      content_tag(:div, :id => html_id) do
        insert_inline_css(html_id) +
        label_tag(build_label_name(resource, method), label) +
        text_field_tag(build_text_field_name(resource, method))
      end.html_safe
    end

    def generate_html_id(resource)
      "#{resource}_#{Time.now.to_i}"
    end

    def insert_inline_css(container_id)
      content_tag(:style, :type => 'text/css', :media => 'screen', :scoped => 'scoped') do
       "##{container_id} { display:none; }"
      end
    end

    def build_label_name(resource, method = nil)
      if method.present?
        "#{resource}_#{method}"
      else
        resource
      end
    end

    def build_text_field_name(resource, method = nil)
      if method.present?
        "#{resource}[#{method}]"
      else
        resource
      end
    end
  end
end

ActionView::Base.send :include, InvisibleCaptcha::ViewHelpers

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
invisible_captcha-0.6.5 lib/invisible_captcha/view_helpers.rb
invisible_captcha-0.6.4 lib/invisible_captcha/view_helpers.rb
invisible_captcha-0.6.2 lib/invisible_captcha/view_helpers.rb
invisible_captcha-0.6.1 lib/invisible_captcha/view_helpers.rb
invisible_captcha-0.6.0 lib/invisible_captcha/view_helpers.rb
invisible_captcha-0.5.0 lib/invisible_captcha/view_helpers.rb