Skip to content

Commit

Permalink
Merge pull request #49 from phillipp/feature/ReqdExctnDt-1999-01-01
Browse files Browse the repository at this point in the history
Make 1999-01-01 the default requested_date (as proposed in #18)
  • Loading branch information
ledermann committed Feb 4, 2016
2 parents 1ae9270 + 4a1aaaa commit 6760d63
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 15 deletions.
14 changes: 13 additions & 1 deletion lib/sepa_king/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Transaction
include ActiveModel::Validations
extend Converter

DEFAULT_REQUESTED_DATE = Date.new(1999, 1, 1).freeze

attr_accessor :name, :iban, :bic, :amount, :instruction, :reference, :remittance_information, :requested_date, :batch_booking
convert :name, :instruction, :reference, :remittance_information, to: :text
convert :amount, to: :decimal
Expand All @@ -22,9 +24,19 @@ def initialize(attributes = {})
send("#{name}=", value)
end

self.requested_date ||= Date.today.next
self.requested_date ||= DEFAULT_REQUESTED_DATE
self.reference ||= 'NOTPROVIDED'
self.batch_booking = true if self.batch_booking.nil?
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
6 changes: 1 addition & 5 deletions lib/sepa_king/transaction/credit_transfer_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ class CreditTransferTransaction < Transaction

validates_inclusion_of :service_level, :in => %w(SEPA URGP)

validate do |t|
if t.requested_date.is_a?(Date)
errors.add(:requested_date, 'is in the past') if t.requested_date < Date.today
end
end
validate { |t| t.validate_requested_date_after(Date.today) }

def initialize(attributes = {})
super
Expand Down
6 changes: 2 additions & 4 deletions lib/sepa_king/transaction/direct_debit_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class DirectDebitTransaction < Transaction
validates_inclusion_of :local_instrument, in: LOCAL_INSTRUMENTS
validates_inclusion_of :sequence_type, in: SEQUENCE_TYPES

validate { |t| t.validate_requested_date_after(Date.today.next) }

validate do |t|
if creditor_account
errors.add(:creditor_account, 'is not correct') unless creditor_account.valid?
Expand All @@ -21,10 +23,6 @@ class DirectDebitTransaction < Transaction
else
errors.add(:mandate_date_of_signature, 'is not a Date')
end

if t.requested_date.is_a?(Date)
errors.add(:requested_date, 'is not in the future') if t.requested_date <= Date.today
end
end

def initialize(attributes = {})
Expand Down
2 changes: 1 addition & 1 deletion spec/credit_transfer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
end

it 'should contain <ReqdExctnDt>' do
expect(subject).to have_xml('//Document/CstmrCdtTrfInitn/PmtInf/ReqdExctnDt', Date.today.next.iso8601)
expect(subject).to have_xml('//Document/CstmrCdtTrfInitn/PmtInf/ReqdExctnDt', Date.new(1999, 1, 1).iso8601)
end

it 'should contain <PmtMtd>' do
Expand Down
2 changes: 1 addition & 1 deletion spec/credit_transfer_transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

context 'Requested date' do
it 'should allow valid value' do
expect(SEPA::CreditTransferTransaction).to accept(nil, Date.today, Date.today.next, Date.today + 2, for: :requested_date)
expect(SEPA::CreditTransferTransaction).to accept(nil, Date.new(1999, 1, 1), Date.today, Date.today.next, Date.today + 2, for: :requested_date)
end

it 'should not allow invalid value' do
Expand Down
2 changes: 1 addition & 1 deletion spec/direct_debit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
end

it 'should contain <ReqdColltnDt>' do
expect(subject).to have_xml('//Document/CstmrDrctDbtInitn/PmtInf/ReqdColltnDt', Date.today.next.iso8601)
expect(subject).to have_xml('//Document/CstmrDrctDbtInitn/PmtInf/ReqdColltnDt', Date.new(1999, 1, 1).iso8601)
end

it 'should contain <PmtMtd>' do
Expand Down
2 changes: 1 addition & 1 deletion spec/direct_debit_transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

context 'Requested date' do
it 'should allow valid value' do
expect(SEPA::DirectDebitTransaction).to accept(nil, Date.today.next, Date.today + 2, for: :requested_date)
expect(SEPA::DirectDebitTransaction).to accept(nil, Date.new(1999, 1, 1), Date.today.next, Date.today + 2, for: :requested_date)
end

it 'should not allow invalid value' do
Expand Down
2 changes: 1 addition & 1 deletion spec/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end

it 'should have default for requested_date' do
expect(SEPA::Transaction.new.requested_date).to eq(Date.today.next)
expect(SEPA::Transaction.new.requested_date).to eq(Date.new(1999, 1, 1))
end

it 'should have default for batch_booking' do
Expand Down

0 comments on commit 6760d63

Please sign in to comment.