Module: Auspost::Postie::ActiveRecord::Validations

Included in:
ClassMethods
Defined in:
lib/auspost/active_record/validations.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@valid_attributes =
{
  :state    => {:accessor => :state, :message => ["%s is not found in the %s postcode", :state, :postcode] },
  :postcode => {:accessor => :postcode, :message => ["%s cannot be found", :postcode] },
  :suburb   => {:accessor => :suburb, :message => ["%s is not found in the %s postcode", :suburb, :postcode] },
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
# File 'lib/auspost/active_record/validations.rb', line 13

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#valid_attributesObject



40
41
42
# File 'lib/auspost/active_record/validations.rb', line 40

def valid_attributes
  @@valid_attributes
end

#validate_locationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/auspost/active_record/validations.rb', line 18

def validate_location
  result = location?(map_attributes)
  if result && !result.status
    result.errors.each do |error|
      message = @@valid_attributes[error.accessor][:message]
      if message.is_a?(String)
        errors.add(error.accessor, message)
      else
        mappings = message[1..-1].map do |x|
          if x.to_s.include?(".")
            methods = x.split(".")
            send(methods.first).send(methods.last).to_s.upcase
          else
            send(x).to_s.upcase
          end
        end
        errors.add(error.accessor, message.first % mappings)
      end
    end
  end
end