Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding presenters for question/support pages

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...
commit 76862a98dde2a41716c7f2a3613c0c39f0fd607c 1 parent c0a83ad
@abhirao abhirao authored
View
40 app/controllers/questions_controller.rb
@@ -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]
@@ -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
@@ -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
View
35 app/presenters/question_presenter.rb
@@ -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
View
35 app/presenters/support_presenter.rb
@@ -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
View
3  app/views/questions/_form.html.haml
@@ -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
View
2  app/views/shared/_ask.html.haml
@@ -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"
Please sign in to comment.
Something went wrong with that request. Please try again.