From 3a1e1e046b855ff7fc1cf9a44b62cf63702bc60f Mon Sep 17 00:00:00 2001 From: Rafael George Date: Thu, 3 Oct 2013 17:06:14 -0400 Subject: [PATCH] Refactor: Topic#invite_by_email Extract Topic#email_already_existas_for? and Topic#grant_permission_to_user Fix failing spec due to missing variable in extracted method --- app/models/topic.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 33b3160e391..9d01bd55fc0 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -426,11 +426,7 @@ class Topic < ActiveRecord::Base invite = Invite.create(invited_by: invited_by, email: lower_email) unless invite.valid? - # If the email already exists, grant permission to that user - if invite.email_already_exists and private_message? - user = User.where(email: lower_email).first - topic_allowed_users.create!(user_id: user.id) - end + grant_permission_to_user(lower_email) if email_already_exists_for?(invite) return end @@ -444,6 +440,15 @@ class Topic < ActiveRecord::Base invite end + def email_already_exists_for?(invite) + invite.email_already_exists and private_message? + end + + def grant_permission_to_user(lower_email) + user = User.where(email: lower_email).first + topic_allowed_users.create!(user_id: user.id) + end + def max_post_number posts.maximum(:post_number).to_i end