Skip to content

Commit

Permalink
Merge remote-tracking branch 'jmoline/subreq_account_id_validation'
Browse files Browse the repository at this point in the history
  • Loading branch information
bblimke committed Jul 11, 2012
2 parents c47babe + a169db8 commit 4f4d327
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/zuora/objects/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def persisted?
!new_record?
end

# convenience method, some attributes are not required
# when the object is part of a subscription request
attr_writer :subscribing
def subscribing?
@subscribing
end

# save the record by updating or creating the record.
def save
return false unless valid?
Expand Down
3 changes: 2 additions & 1 deletion lib/zuora/objects/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Zuora::Objects
class Contact < Base
belongs_to :account

validates_presence_of :account_id, :first_name, :last_name
validates_presence_of :first_name, :last_name
validates_presence_of :account_id, :unless => :subscribing?
validates_length_of :first_name, :maximum => 100
validates_length_of :last_name, :maximum => 100
validates_length_of :nick_name, :maximum => 100, :allow_nil => true
Expand Down
2 changes: 1 addition & 1 deletion lib/zuora/objects/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Zuora::Objects
class PaymentMethod < Base
belongs_to :account

validates_presence_of :account_id
validates_presence_of :account_id, :unless => :subscribing?

# Generic Validations
validates_inclusion_of :type, :in => %w(ACH Cash Check CreditCard CreditCardReferenceTransaction DebitCard Other PayPal WireTransfer)
Expand Down
1 change: 1 addition & 0 deletions lib/zuora/objects/subscribe_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def must_have_usable(ref)
obj = self.send(ref)
return errors[ref] << "must be provided" if obj.nil?
if obj.new_record? || obj.changed?
obj.subscribing = true
errors[ref] << "is invalid" unless obj.valid?
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/zuora/objects/contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
subject.errors[:account_id].should include("can't be blank")
end

it 'does not require account_id if subscribing' do
subject.subscribing = true
subject.valid?
subject.errors[:account_id].should_not include("can't be blank")
end

it "requires first_name" do
subject.errors[:first_name].should include("can't be blank")
end
Expand Down
14 changes: 14 additions & 0 deletions spec/zuora/objects/payment_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
end
end

describe "validations" do
let(:payment_method) { Zuora::Objects::PaymentMethod.new }
it "requires account_id" do
payment_method.valid?.should be_false
payment_method.errors[:account_id].should include("can't be blank")
end

it "does not require account_id when subscribing" do
payment_method.subscribing = true
payment_method.valid?.should be_false
payment_method.errors[:account_id].should_not include("can't be blank")
end
end

describe "write only attributes" do
ach = FactoryGirl.build(:payment_method_ach)
ach.write_only_attributes.should == [:ach_account_number, :credit_card_number,
Expand Down

0 comments on commit 4f4d327

Please sign in to comment.