Permalink
Browse files

Remove duplication in Answer

* Removes smell Duplicated Code
* Applies refactoring Extract Method
  • Loading branch information...
jferris committed Dec 6, 2012
1 parent 8698fd4 commit 1e35c6824b6aa2a420fa44c6e5451a24ea8ebb8d
Showing with 8 additions and 3 deletions.
  1. +8 −3 example_app/app/models/answer.rb
@@ -7,11 +7,16 @@ class Answer < ActiveRecord::Base
validates :text, presence: true

def self.for_user(user)
joins(:completion).where(completions: { user_id: user.id }).last ||
NullAnswer.new
joins(:completion).where(completions: { user_id: user.id }).last_or_null
end

def self.most_recent
order(:created_at).last || NullAnswer.new
order(:created_at).last_or_null
end

private

def self.last_or_null
last || NullAnswer.new
end
end

0 comments on commit 1e35c68

Please sign in to comment.