Class: Field::CNPJ

Inherits:
Base
  • Object
show all
Defined in:
lib/field/cnpj.rb

Instance Attribute Summary

Attributes inherited from Base

#fieldset, #form, #name, #value

Instance Method Summary collapse

Methods inherited from Base

#add_attributes, #form?, #initialize

Constructor Details

This class inherits a constructor from Field::Base

Instance Method Details

#add_validations(klass, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/field/cnpj.rb', line 6

def add_validations(klass, options)
  klass.validates name, cnpj: true

  if options[:uniqueness]
    unless options[:uniqueness].is_a?(Hash) && options[:uniqueness][:model]
      raise Field::Error, "Must specify a model to validate uniqueness"
    end

    required = { 
      attribute:   name, 
      allow_nil:   true, 
      allow_blank: true
    }

    klass.validates name, uniqueness: options[:uniqueness].merge(required)
    options.reject! { |k| k == :uniqueness }
  end

  super
end

#attributeObject



27
28
29
# File 'lib/field/cnpj.rb', line 27

def attribute
  ::Attribute::CNPJ
end

#outputObject



31
32
33
# File 'lib/field/cnpj.rb', line 31

def output
  ::CNPJ.new(value).formatted
end