Created
August 11, 2014 01:59
-
-
Save vicatcu/511ead4153c6fcb3c835 to your computer and use it in GitHub Desktop.
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
SPEC | |
describe "when a membeship is created" do | |
let!(:another_membership) { FactoryGirl.create(:membership, user: user, membership_type: membertype, | |
start: Date.parse("2013-11-15")) } | |
context "if there are other memberships associated with user" do | |
it "sets the end date for any of them that have a nil end date" do | |
expect(Membership.find_by(start: Date.parse("2012-11-15"))).not_to be_nil | |
expect(Membership.find_by(start: Date.parse("2012-11-15")).end).not_to be_nil | |
end | |
end | |
end | |
MODEL | |
after_create :enforce_at_most_one_open_for_user | |
def enforce_at_most_one_open_for_user | |
new_end_date = self.start.prev_day | |
if !self.user.nil? | |
self.user.memberships.each do |m| | |
m.end = new_end_date if m.id != self.id && m.nil? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment