Skip to content

Instantly share code, notes, and snippets.

@Lordnibbler
Created June 29, 2012 19:32
Show Gist options
  • Select an option

  • Save Lordnibbler/3020141 to your computer and use it in GitHub Desktop.

Select an option

Save Lordnibbler/3020141 to your computer and use it in GitHub Desktop.
#post_test.rb
should "add points to post.user when updating title" do
@user = FactoryGirl.create(:user)
@post = FactoryGirl.create(:post, :title => nil, :story => nil, :user_id => @user.id)
pts_before = @user.points
@post.update_attributes(:title => "new title")
pts_after = @user.points
assert_not_equal(pts_before, pts_after)
#user's points == 10 here, even though it JUST showed 12 inside the post#add_points method
end
#post.rb
before_save :add_points
def add_points
#self.user.points == 10 here
# changed from nil to something, so add pts to user
if ( !title_change.nil? && !self.user.nil? && !self.title_points_redeemed? )
self.user.update_attributes(:points => self.user.points + 2) if title_change[0].blank?
self.update_attributes(:title_points_redeemed => true)
end
if ( !story_change.nil? && !self.user.nil? && !self.story_points_redeemed? )
self.user.update_attributes(:points => self.user.points + 10) if story_change[0].blank?
self.update_attributes(:story_points_redeemed => true)
end
#self.user.points == 12 here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment