diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 36b4f4ba..d80ef834 100644 --- a/app/controllers/questions_controller.rb +++ b/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 diff --git a/app/presenters/question_presenter.rb b/app/presenters/question_presenter.rb new file mode 100644 index 00000000..aaaba577 --- /dev/null +++ b/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 diff --git a/app/presenters/support_presenter.rb b/app/presenters/support_presenter.rb new file mode 100644 index 00000000..2075cf09 --- /dev/null +++ b/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 diff --git a/app/views/questions/_form.html.haml b/app/views/questions/_form.html.haml index 6a21dc7f..20f8843e 100644 --- a/app/views/questions/_form.html.haml +++ b/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 diff --git a/app/views/shared/_ask.html.haml b/app/views/shared/_ask.html.haml index 0fd3ec35..24935a4b 100644 --- a/app/views/shared/_ask.html.haml +++ b/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" \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index bdfec545..d2382e0a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -11,7 +11,7 @@ module HacketyHackCom class Application < Rails::Application - config.autoload_paths += %W(#{config.root}/lib) + config.autoload_paths << "#{config.root}/lib" << "#{config.root}/presenters" config.encoding = "utf-8" diff --git a/config/routes.rb b/config/routes.rb index f9144702..e139c368 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,9 @@ match "/download" => redirect("/downloads/latest") scope '/support', :as => 'support' do - resources :questions, :controller => 'questions' + resources :questions, :controller => 'questions' do + resources :answers + end end constraints(ApiConstraint) do