Created
February 10, 2012 02:01
-
-
Save janx/1785463 to your computer and use it in GitHub Desktop.
refactoring of Membership create
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb | |
index 5747a13..c5c9a05 100644 | |
--- a/app/controllers/admin/users_controller.rb | |
+++ b/app/controllers/admin/users_controller.rb | |
@@ -12,8 +12,7 @@ class Admin::UsersController < AdminController | |
@email = Email.new(email: @identity.email, confirmed: true) | |
@user = User.create!(name: @identity.name, nickname: @identity.nickname, | |
emails: [@email]) | |
- # create the membership for other domain user. | |
- Membership.create!(:account => account, :email => @email) unless account.membership_for(@user) | |
+ Membership.for!(account, @email) | |
flash[:notice] = "User created successfully" | |
else | |
error_msg = existing_email.blank? ? @identity.errors.full_messages.join(', ') : "This email address (#{@identity.email}) is already registered with Socialspring." | |
diff --git a/app/models/email.rb b/app/models/email.rb | |
index 5f9b533..2c5753c 100644 | |
--- a/app/models/email.rb | |
+++ b/app/models/email.rb | |
@@ -83,9 +83,7 @@ class Email < ActiveRecord::Base | |
def activate_domain_membership | |
unless domain_membership? | |
account = Account.for_domain(self.domain) | |
- unless account.membership_for(user) | |
- Membership.create!(:account => account, :email => self) | |
- end | |
+ Membership.for!(account, self) | |
end | |
rescue ActiveRecord::RecordInvalid | |
Rails.logger.info "Email#activate_domain_membership: account/membership not created - #{$!}" | |
diff --git a/app/models/invitation.rb b/app/models/invitation.rb | |
index b15462f..d1bc5e3 100644 | |
--- a/app/models/invitation.rb | |
+++ b/app/models/invitation.rb | |
@@ -41,7 +41,7 @@ class Invitation < ActiveRecord::Base | |
the_email = Email.new(email: self.email, confirmed: true, accounts: [self.account]) | |
end | |
if the_email.user.try(:persisted?) | |
- Membership.create!(:account => self.account, :email => the_email) unless account.membership_for(the_email.user) | |
+ Membership.for!(account, the_email) | |
user = the_email.user | |
else | |
user = User.create(emails: [the_email]) | |
diff --git a/app/models/membership.rb b/app/models/membership.rb | |
index 4e71fb9..3a66079 100644 | |
--- a/app/models/membership.rb | |
+++ b/app/models/membership.rb | |
@@ -22,6 +22,10 @@ class Membership < ActiveRecord::Base | |
validate :email_is_confirmed | |
validate :unique_user_to_account | |
+ def self.for!(account, email) | |
+ account.membership_for(email.user) || create!(account: account, email: email) | |
+ end | |
+ | |
def email_is_confirmed | |
errors.add(:email, "must be confirmed") unless email.try(:confirmed?) | |
end | |
diff --git a/spec/models/membership_spec.rb b/spec/models/membership_spec.rb | |
index 2c80fbb..ba6c6d2 100644 | |
--- a/spec/models/membership_spec.rb | |
+++ b/spec/models/membership_spec.rb | |
@@ -6,6 +6,25 @@ describe Membership do | |
lambda{ m.update_attributes(disabled: true) }.should change(Membership, :count).by(-1) | |
end | |
+ describe ".for!" do | |
+ before do | |
+ @account = Factory(:account) | |
+ @user = Factory(:user) | |
+ @email = Factory(:email, :user => @user) | |
+ end | |
+ | |
+ it "should find existing membership" do | |
+ m = Membership.create!(account: @account, email: @email) | |
+ Membership.for!(@account, @email).should == m | |
+ end | |
+ | |
+ it "should create new membership" do | |
+ m = Membership.for!(@account, @email) | |
+ m.email.should == @email | |
+ m.account.should == @account | |
+ end | |
+ end | |
+ | |
describe "#clean_old_invitations" do | |
before do | |
@account = Factory(:account) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment