Skip to content

Commit ab8422b

Browse files
committed
Merge pull request #115 from abhirao/question_presenter
Question presenter
2 parents ae6bc2f + 76862a9 commit ab8422b

7 files changed

Lines changed: 105 additions & 16 deletions

File tree

app/controllers/questions_controller.rb

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class QuestionsController < InheritedController
2-
before_filter :set_support
32
load_and_authorize_resource
3+
prepend_before_filter :set_presenter
4+
prepend_before_filter :set_support
45

56
def create
67
@question = Question.create params[:question]
@@ -18,22 +19,31 @@ def update
1819
end
1920

2021
def collection
21-
if @support
22-
@questions ||= end_of_association_chain.supports.newest_first.paginate(:page => params[:page])
23-
else
24-
@questions ||= end_of_association_chain.no_supports.newest_first.paginate(:page => params[:page])
25-
end
22+
@questions = @presenter.apply_scope(end_of_association_chain).newest_first.paginate(:page => params[:page])
2623
end
27-
24+
2825
def collection_url
29-
@support ? support_questions_path : questions_path
26+
collection_path
3027
end
28+
29+
def collection_path
30+
@presenter.collection_path
31+
end
32+
3133
def new_resource_path
32-
@support ? new_support_question_path : new_question_path
34+
@presenter.new_resource_path
35+
end
36+
37+
def resource_url(*params)
38+
resource_path(params)
3339
end
3440

35-
def resource_path(*resource)
36-
@support ? support_question_path(*resource) : question_path(*resource)
41+
def resource_path(*other)
42+
if other[0]
43+
@presenter.resource_path(other)
44+
else
45+
@presenter.resource_path(resource)
46+
end
3747
end
3848

3949
def edit_resource_path
@@ -47,4 +57,12 @@ def set_support
4757
end
4858
end
4959

60+
def set_presenter
61+
if @support
62+
@presenter = SupportPresenter.new(resource)
63+
else
64+
@presenter = QuestionPresenter.new(resource)
65+
end
66+
end
67+
5068
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class QuestionPresenter
2+
include Rails.application.routes.url_helpers
3+
4+
def initialize(q)
5+
@question = q
6+
end
7+
8+
def resource_path(question)
9+
if question
10+
question_path(question)
11+
else
12+
question_path(@question)
13+
end
14+
end
15+
16+
def new_resource_path
17+
new_question_path
18+
end
19+
20+
def collection_path
21+
questions_path
22+
end
23+
24+
def edit_resource_path
25+
edit_question_path
26+
end
27+
28+
def apply_scope(chain)
29+
chain.no_supports
30+
end
31+
32+
def answers_path
33+
question_answer_path(@question)
34+
end
35+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class SupportPresenter
2+
include Rails.application.routes.url_helpers
3+
4+
def initialize(sq)
5+
@support_question = sq
6+
end
7+
8+
def page_title
9+
"Support Questions"
10+
end
11+
12+
def new_resource_path
13+
new_support_question_path
14+
end
15+
16+
def collection_path
17+
support_questions_path
18+
end
19+
20+
def edit_resource_path
21+
edit_support_question_path
22+
end
23+
24+
def resource_path(question)
25+
if question
26+
support_question_path(question)
27+
else
28+
support_question_path(@question)
29+
end
30+
end
31+
32+
def apply_scope(chain)
33+
chain.supports
34+
end
35+
end

app/views/questions/_form.html.haml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
- curr_module = @support ? '/support' : ''
2-
= simple_form_for(resource, :url => curr_module.concat(@form_url)) do |f|
1+
= simple_form_for(resource, :url => @form_url) do |f|
32
= f.error_notification
43

54
.inputs

app/views/shared/_ask.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
%h2 Have A Question?
33
%p Ask away! No question is too big or too small.
44
- if current_user
5-
= link_to "Ask a Question", new_question_path, :class => "btn success"
5+
= link_to "Ask a Question", new_resource_path, :class => "btn success"
66
- else
77
%p Log in to ask a question
88
= link_to "Log In", login_path, :class => "btn success"

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
module HacketyHackCom
1313
class Application < Rails::Application
14-
config.autoload_paths += %W(#{config.root}/lib)
14+
config.autoload_paths << "#{config.root}/lib" << "#{config.root}/presenters"
1515

1616
config.encoding = "utf-8"
1717

config/routes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
match "/download" => redirect("/downloads/latest")
1010

1111
scope '/support', :as => 'support' do
12-
resources :questions, :controller => 'questions'
12+
resources :questions, :controller => 'questions' do
13+
resources :answers
14+
end
1315
end
1416

1517
constraints(ApiConstraint) do

0 commit comments

Comments
 (0)