-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathtransaction.rb
More file actions
62 lines (53 loc) · 2.08 KB
/
Copy pathtransaction.rb
File metadata and controls
62 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# encoding: utf-8
module SEPA
class Transaction
include ActiveModel::Validations
extend Converter
DEFAULT_REQUESTED_DATE = Date.new(1999, 1, 1).freeze
attr_accessor :name,
:iban,
:bic,
:bban, # bankgiro 5748964
:bban_proprietary, # BGNR
:clearing_code, # SESBA
:clearing_bank_identifier, # 9960
:amount,
:instruction,
:reference,
:remittance_information,
:requested_date,
:batch_booking,
:currency,
:debtor_address,
:creditor_address
convert :name, :instruction, :reference, :remittance_information, to: :text
convert :amount, to: :decimal
validates_length_of :name, within: 1..70
validates_length_of :currency, is: 3
validates_length_of :instruction, within: 1..35, allow_nil: true
validates_length_of :reference, within: 1..35, allow_nil: true
validates_length_of :remittance_information, within: 1..140, allow_nil: true
validates_numericality_of :amount, greater_than: 0
validates_presence_of :requested_date
validates_inclusion_of :batch_booking, :in => [true, false]
validates_with BICValidator, IBANValidator, message: "%{value} is invalid"
validates :iban, presence: true, unless: :bban
validates :bban, presence: true, unless: :iban
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
self.requested_date ||= DEFAULT_REQUESTED_DATE
self.reference ||= 'NOTPROVIDED'
self.batch_booking = true if self.batch_booking.nil?
self.currency ||= 'EUR'
end
protected
def validate_requested_date_after(min_requested_date)
return unless requested_date.is_a?(Date)
if requested_date != DEFAULT_REQUESTED_DATE && requested_date < min_requested_date
errors.add(:requested_date, "must be greater or equal to #{min_requested_date}, or nil")
end
end
end
end