Skip to content

Commit

Permalink
Adding presenters for question/support pages
Browse files Browse the repository at this point in the history
Replacing hack that generated the questions/supports pages
with presenters. View now delegates all logic to presenters.
Which presenter is used is determined by controller.
  • Loading branch information
abhirao committed Dec 5, 2011
1 parent c0a83ad commit 76862a9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
40 changes: 29 additions & 11 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class QuestionsController < InheritedController
before_filter :set_support
load_and_authorize_resource
prepend_before_filter :set_presenter
prepend_before_filter :set_support

def create
@question = Question.create params[:question]
Expand All @@ -18,22 +19,31 @@ def update
end

def collection
if @support
@questions ||= end_of_association_chain.supports.newest_first.paginate(:page => params[:page])
else
@questions ||= end_of_association_chain.no_supports.newest_first.paginate(:page => params[:page])
end
@questions = @presenter.apply_scope(end_of_association_chain).newest_first.paginate(:page => params[:page])
end

def collection_url
@support ? support_questions_path : questions_path
collection_path
end

def collection_path
@presenter.collection_path
end

def new_resource_path
@support ? new_support_question_path : new_question_path
@presenter.new_resource_path
end

def resource_url(*params)
resource_path(params)
end

def resource_path(*resource)
@support ? support_question_path(*resource) : question_path(*resource)
def resource_path(*other)
if other[0]
@presenter.resource_path(other)
else
@presenter.resource_path(resource)
end
end

def edit_resource_path
Expand All @@ -47,4 +57,12 @@ def set_support
end
end

def set_presenter
if @support
@presenter = SupportPresenter.new(resource)
else
@presenter = QuestionPresenter.new(resource)
end
end

end
35 changes: 35 additions & 0 deletions app/presenters/question_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class QuestionPresenter
include Rails.application.routes.url_helpers

def initialize(q)
@question = q
end

def resource_path(question)
if question
question_path(question)
else
question_path(@question)
end
end

def new_resource_path
new_question_path
end

def collection_path
questions_path
end

def edit_resource_path
edit_question_path
end

def apply_scope(chain)
chain.no_supports
end

def answers_path
question_answer_path(@question)
end
end
35 changes: 35 additions & 0 deletions app/presenters/support_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class SupportPresenter
include Rails.application.routes.url_helpers

def initialize(sq)
@support_question = sq
end

def page_title
"Support Questions"
end

def new_resource_path
new_support_question_path
end

def collection_path
support_questions_path
end

def edit_resource_path
edit_support_question_path
end

def resource_path(question)
if question
support_question_path(question)
else
support_question_path(@question)
end
end

def apply_scope(chain)
chain.supports
end
end
3 changes: 1 addition & 2 deletions app/views/questions/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
- curr_module = @support ? '/support' : ''
= simple_form_for(resource, :url => curr_module.concat(@form_url)) do |f|
= simple_form_for(resource, :url => @form_url) do |f|
= f.error_notification

.inputs
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_ask.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%h2 Have A Question?
%p Ask away! No question is too big or too small.
- if current_user
= link_to "Ask a Question", new_question_path, :class => "btn success"
= link_to "Ask a Question", new_resource_path, :class => "btn success"
- else
%p Log in to ask a question
= link_to "Log In", login_path, :class => "btn success"

0 comments on commit 76862a9

Please sign in to comment.