Module: ABNValidations

Defined in:
lib/abn.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.enableObject



49
50
51
# File 'lib/abn.rb', line 49

def enable
  ActiveRecord::Base.extend ABNValidations
end

Instance Method Details

#validates_abn_correctness_of(*args) ⇒ Object

Validates whether the value of the specified attribute conforms to the Australian Business Number (ABN) format.

validates_abn_correctness_of will automatically non-destructively strip any formatting and whitespace before validation



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/abn.rb', line 59

def validates_abn_correctness_of(*args)

  # Set up our configuration for this validation
  configuration = { :on => :save, :allow_nil => false, :message => "is not a valid ABN" }
  configuration.update(args.extract_options!)

  # iterate through each field we've set up to validate the local numericality of
  validates_each(args, configuration) do |instance, attr_name, value|

    # get our raw value (ie, what was last sent to the instance...)
    raw_value = instance.send("#{attr_name}_before_type_cast") || value

    # skip our processing if we've got a legal nil...
    next if (configuration[:allow_nil] and raw_value.nil?)

    unless ABN.valid?(raw_value)
      instance.errors.add(attr_name, configuration[:message])
    end
  end
end