Skip to content

Commit

Permalink
Extract Decorator Step One
Browse files Browse the repository at this point in the history
* Create empty decorator class
* Move method to decorator
  • Loading branch information
jferris committed Feb 22, 2013
1 parent 15f5b96 commit af2e831
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 1 addition & 7 deletions example_app/app/models/survey.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class Survey < ActiveRecord::Base
NO_ANSWER = "You haven't answered this question".freeze

include ActiveModel::ForbiddenAttributesProtection

validates_presence_of :title
Expand All @@ -19,7 +17,7 @@ def summaries_using(summarizer, options = {})

def summary_or_hidden_answer(summarizer, question, answered_by)
if hide_unanswered_question?(question, answered_by)
hide_answer_to_question(question)
UnansweredQuestionHider.new.hide_answer_to_question(question)
else
question.summary_using(summarizer)
end
Expand All @@ -28,8 +26,4 @@ def summary_or_hidden_answer(summarizer, question, answered_by)
def hide_unanswered_question?(question, answered_by)
answered_by && !question.answered_by?(answered_by)
end

def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
end
end
7 changes: 7 additions & 0 deletions example_app/app/models/unanswered_question_hider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class UnansweredQuestionHider
NO_ANSWER = "You haven't answered this question".freeze

def hide_answer_to_question(question)
Summary.new(question.title, NO_ANSWER)
end
end
13 changes: 13 additions & 0 deletions example_app/spec/models/unanswered_question_hider_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe UnansweredQuestionHider, '#hide_answer_to_question' do
it 'returns a hidden summary' do
question = build_stubbed(:question)
hider = UnansweredQuestionHider.new

result = hider.hide_answer_to_question(question)

result.title.should eq question.title
result.value.should eq UnansweredQuestionHider::NO_ANSWER
end
end

0 comments on commit af2e831

Please sign in to comment.