Skip to content

Instantly share code, notes, and snippets.

@adelcambre
Created December 19, 2008 19:21
Show Gist options
  • Save adelcambre/38084 to your computer and use it in GitHub Desktop.
Save adelcambre/38084 to your computer and use it in GitHub Desktop.
commit 57bcc45907480d3b1341f4d254e95bd961e479bc
Author: Andy Delcambre <adelcambre@engineyard.com>
Date: Fri Dec 19 11:21:04 2008 -0800
moved #signup to Contact from Account as you are signing up a contact not an account. Refactored to handle newer dm-salesforce
diff --git a/config/dependencies.rb b/config/dependencies.rb
index 0ba7974..a4a158d 100644
--- a/config/dependencies.rb
+++ b/config/dependencies.rb
@@ -8,5 +8,5 @@ add_gem 'hoe'
add_dependency 'extlib', '>= 0.9.8', :require => 'extlib'
add_dependency 'dm-core', '>= 0.9.7', :require => 'dm-core'
-add_dependency 'dm-salesforce', '>= 0.9.7.1', :require => 'dm-salesforce'
+add_dependency 'dm-salesforce', '= 0.9.7.6', :require => 'dm-salesforce'
add_dependency 'dm-validations', '>= 0.9.7', :require => 'dm-validations'
diff --git a/lib/models/account.rb b/lib/models/account.rb
index b7d5e2d..ec915d8 100644
--- a/lib/models/account.rb
+++ b/lib/models/account.rb
@@ -35,14 +35,6 @@ module EY
self.class.first(:id => parent_id)
end
- validates_with_block :general do
- if contacts.all? {|c| c.valid?}
- true
- else
- [false, "Invalid contact information supplied"]
- end
- end
-
def self.engineyard
@engineyard ||= get(EySalesforceModels.environment.engineyard_account_id)
end
@@ -51,18 +43,6 @@ module EY
@customers ||= all(:id.not => EySalesforceModels.environment.engineyard_account_id)
end
- def self.signup(name, contact_info_hash)
- account = new(:name => name)
- account.save
- Contact.signup_with(account, contact_info_hash)
- account.destroy unless account.valid?
- account
- end
-
- def customer
- Customer.first(:salesforce_account_id => id[0..14])
- end
-
def contacts_to_notify
contacts.all(:notify => true)
end
diff --git a/lib/models/contact.rb b/lib/models/contact.rb
index d92ead4..af619a4 100644
--- a/lib/models/contact.rb
+++ b/lib/models/contact.rb
@@ -43,7 +43,15 @@ module EY
validates_with_block :self_service_user do
if @failed_to_enable_self_service
- [false, "Could not enable your Engine Yard login"]
+ [false, @failed_to_enable_self_service.inspect]
+ else
+ true
+ end
+ end
+
+ validates_with_block :account do
+ if @failed_to_create_account
+ [false, @failed_to_create_account.inspect]
else
true
end
@@ -54,25 +62,40 @@ module EY
:email => email,
:active => true,
:contact_id => self.id,
- :last_name => "UNUSED",
+ :first_name => first_name,
+ :last_name => last_name,
:time_zone_sid_key => 'America/Los_Angeles',
:locale_sid_key => 'en_US',
:language_locale_key => 'en_US'
)
user.save
- rescue SalesforceAPI::Connection::CreateError
- @failed_to_enable_self_service = true
- ensure
- valid?
+ @failed_to_enable_self_service = user.errors unless user.valid?
+ user
end
+
+ def self.signup(contact_info)
+ account_name = contact_info.delete(:account_name)
- def self.signup_with(account, contact_info)
- contact = account.contacts.build(contact_info)
- if contact.save
- contact.enable_self_service
+ account = Account.create(:name => account_name)
+ if not account or not account.valid?
+ @failed_to_create_account = account.errors
+ puts account.inspect
+ return Contact.new(contact_info.merge(:account => account))
+ end
+
+ contact = Contact.create(contact_info.merge(:account => account))
+ if not contact or not contact.valid?
+ account.destroy
+ return contact
+ end
+
+
+ user = contact.enable_self_service
+ if not user or not user.valid?
+ contact.destroy
+ account.destroy
+ return contact
end
- contact
- rescue SalesforceAPI::Connection::CreateError
contact
end
end
diff --git a/lib/models/self_service_user.rb b/lib/models/self_service_user.rb
index d35a93f..95fb905 100644
--- a/lib/models/self_service_user.rb
+++ b/lib/models/self_service_user.rb
@@ -17,6 +17,7 @@ module EY
property :email, String
# unused, only for create
+ property :first_name, String
property :last_name, String
property :locale_sid_key, String
property :language_locale_key, String
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 0039723..b4c7323 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -10,38 +10,4 @@ module EY::Salesforce
Account.customers.size.should == Account.all.size - 1
end
end
-
- describe "Signing up for an Account" do
- describe "with invalid account information" do
- it "should not create the account" do
- contact_info_hash = {:first_name => "Per", :last_name => "Son", :email => 'person@company.com'}
- account = Account.signup('', contact_info_hash)
- account.should_not be_valid
- account.should have(1).contacts
- account.contacts.first.should_not be_valid
- end
- end
-
- describe "with valid contact information" do
- it "completes successfully" do
- contact_info_hash = {:first_name => "Per", :last_name => "Son", :email => 'person@company.com'}
- account = Account.signup('Adidas Corporation', contact_info_hash)
- account.should be_valid
- account.should have(1).contacts
- end
- end
-
- describe "with an invalid contact email address" do
- it "should not create an account" do
- contact_info_hash = {:first_name => "Per", :last_name => "Son", :email => 'person'}
- account = Account.signup('Adidas Corporation', contact_info_hash)
- account.should_not be_valid
- account.errors.on(:general).should == ["Invalid contact information supplied"]
- account.should have(1).contacts
- account.contacts.first.should_not be_valid
- account.contacts.first.errors.on(:email).should == ["Email has an invalid format"]
- Account.first(:name => 'Adidas Corporation').should be_nil
- end
- end
- end
end
diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb
index 1152409..c0fd0bc 100644
--- a/spec/models/contact_spec.rb
+++ b/spec/models/contact_spec.rb
@@ -31,8 +31,38 @@ module EY::Salesforce
end
end
end
+
+ describe "Signing up" do
+ describe "with invalid account information" do
+ it "should not create the account" do
+ contact = Contact.signup(:account_name => '', :first_name => "Per", :last_name => "Son", :email => 'person@company.com')
+ contact.should_not be_valid
+ contact.account.should_not be_valid
+ end
+ end
+
+ describe "with valid information" do
+ it "completes successfully" do
+ contact = Contact.signup(:account_name => 'Adidas Corporation', :first_name => "Per", :last_name => "Son", :email => 'person@company.com')
+ contact.should be_valid
+ contact.first_name.should == "Per"
+ contact.account.should be_valid
+ contact.account.name.should == "Adidas Corporation"
+ contact.self_service_user.should be_active
+ end
+ end
- describe "A existing Contact" do
+ describe "with an invalid contact email address" do
+ it "should not create an account" do
+ contact = Contact.signup(:account_name => 'Adidas Corporation', :first_name => "Per", :last_name => "Son", :email => 'person')
+ contact.should_not be_valid
+ contact.account.reload.should be_nil
+ end
+ end
+ end
+
+
+ describe "An existing Contact" do
describe "when enabling self-service" do
describe "when the username is available" do
it "completes successfully" do
@@ -48,12 +78,17 @@ module EY::Salesforce
describe "when it does not belong to an account" do
it "does not create a contact" do
- contact = Contact.new(:first_name => "Per", :last_name => "Son", :email => 'person@company.com')
- contact.save!
+ account = Account.create(:name => "Adidas Corporation")
+ contact = Contact.new(:first_name => "Per", :last_name => "Son", :email => 'person@company.com', :account => account)
+ contact.save
+ account.destroy
+ contact.reload
contact.enable_self_service
contact.should_not be_valid
- contact.errors.on(:self_service_user).should == ["Could not enable your Engine Yard login"]
+ contact.errors.on(:self_service_user).detect do |x|
+ x =~ /Contacts not associated with an account are not eligible for Self-Service user creation/
+ end.should_not be_nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment