Sha256: a644d48e4b6fdd1e57f30a936220814f53e84440a6e2f04fd7f800f9c9785358

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Inputs
  module EffectiveTel
    class Input < Effective::FormInput
      delegate :content_tag, :text_field_tag, :to => :@template

      DEFAULT_TEL_MASK = '(999) 999-9999? x99999'
      DEFAULT_CELL_MASK = '(999) 999-9999'

      def default_options
        {cellphone: false, fax: false}
      end

      def default_input_html
        {class: 'effective_tel tel'}
      end

      def default_input_js
        {mask: DEFAULT_TEL_MASK, placeholder: '_'}
      end

      def to_html
        if options[:input_group] == false
          return text_field_tag(field_name, value, tag_options)
        end

        content_tag(:div, class: 'input-group') do
          content_tag(:span, class: 'input-group-addon') do
            content_tag(:i, '', class: "glyphicon glyphicon-#{glyphicon}").html_safe
          end +
          text_field_tag(field_name, value, tag_options)
        end
      end

      def fax?
        field_name.include?('fax') || options[:fax]
      end

      def cellphone?
        field_name.include?('cell') || options[:cellphone]
      end

      def glyphicon
        icon = 'earphone' # default

        icon = 'phone' if field_name.include?('cell')
        icon = 'phone-alt' if field_name.include?('fax')

        icon = 'phone' if options[:cellphone]
        icon = 'phone-alt' if options[:fax]

        icon
      end

      def js_options
        super.tap do |js_options|
          if (fax? || cellphone?) && js_options[:mask] == DEFAULT_TEL_MASK
            js_options[:mask] = DEFAULT_CELL_MASK
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_form_inputs-0.6.1 app/models/inputs/effective_tel/input.rb