Skip to content

Commit af2e831

Browse files
committed
Extract Decorator Step One
* Create empty decorator class * Move method to decorator
1 parent 15f5b96 commit af2e831

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

example_app/app/models/survey.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class Survey < ActiveRecord::Base
2-
NO_ANSWER = "You haven't answered this question".freeze
3-
42
include ActiveModel::ForbiddenAttributesProtection
53

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

2018
def summary_or_hidden_answer(summarizer, question, answered_by)
2119
if hide_unanswered_question?(question, answered_by)
22-
hide_answer_to_question(question)
20+
UnansweredQuestionHider.new.hide_answer_to_question(question)
2321
else
2422
question.summary_using(summarizer)
2523
end
@@ -28,8 +26,4 @@ def summary_or_hidden_answer(summarizer, question, answered_by)
2826
def hide_unanswered_question?(question, answered_by)
2927
answered_by && !question.answered_by?(answered_by)
3028
end
31-
32-
def hide_answer_to_question(question)
33-
Summary.new(question.title, NO_ANSWER)
34-
end
3529
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class UnansweredQuestionHider
2+
NO_ANSWER = "You haven't answered this question".freeze
3+
4+
def hide_answer_to_question(question)
5+
Summary.new(question.title, NO_ANSWER)
6+
end
7+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'spec_helper'
2+
3+
describe UnansweredQuestionHider, '#hide_answer_to_question' do
4+
it 'returns a hidden summary' do
5+
question = build_stubbed(:question)
6+
hider = UnansweredQuestionHider.new
7+
8+
result = hider.hide_answer_to_question(question)
9+
10+
result.title.should eq question.title
11+
result.value.should eq UnansweredQuestionHider::NO_ANSWER
12+
end
13+
end

0 commit comments

Comments
 (0)