Skip to content
Browse files

Add a discussion list decorator

  • Loading branch information...
1 parent d66215c commit 097c3e26f34553b170f02c4eb64ee2762d06ceb2 @practicingruby practicingruby committed
View
4 app/controllers/discussions_controller.rb
@@ -13,6 +13,8 @@ def index
else
@course.discussions.inactive.conversations
end
+
+ @discussions = DiscussionListDecorator.new(@discussions)
else
@discussions = case params[:category]
when "conversation"
@@ -25,6 +27,8 @@ def index
@course.discussions.active.conversations
end
end
+
+ @discussions = DiscussionListDecorator.new(@discussions)
end
private
View
50 app/decorators/discussion_list_decorator.rb
@@ -0,0 +1,50 @@
+class DiscussionListDecorator < ApplicationDecorator
+ decorates :discussion
+
+ def category_filters
+ [conversations_link, reviews_link, evaluations_link].join(" | ").html_safe
+ end
+
+ def state_filters
+ output = if h.params[:state] == "closed"
+ link_params = h.params.merge(:state => "open")
+
+ "State: " +
+ h.link_to("Open", h.course_discussions_path(link_params)) +
+ " | Closed"
+ else
+ link_params = h.params.merge(:state => "closed")
+
+ "State: Open | " +
+ h.link_to("Closed", h.course_discussions_path(link_params))
+ end
+
+ output.html_safe
+ end
+
+ def matched_discussions
+ all.map do |discussion|
+ h.content_tag(:h3, discussion.subject)
+ end.join.html_safe
+ end
+
+ private
+
+ def conversations_link
+ link_params = h.params.merge(:category => "conversation")
+
+ h.link_to "Conversations", h.course_discussions_path(link_params)
+ end
+
+ def reviews_link
+ link_params = h.params.merge(:category => "review")
+
+ h.link_to "Reviews", h.course_discussions_path(link_params)
+ end
+
+ def evaluations_link
+ link_params = h.params.merge(:category => "evaluation")
+
+ h.link_to "Evaluations", h.course_discussions_path(link_params)
+ end
+end
View
23 app/views/discussions/index.html.haml
@@ -1,28 +1,11 @@
-= link_to "Conversations",
- course_discussions_path(params.merge(:category => "conversation"))
-|
-= link_to "Reviews",
- course_discussions_path(params.merge(:category => "review"))
-|
-= link_to "Evaluations",
- course_discussions_path(params.merge(:category => "evaluation"))
+= @discussions.category_filters
%br
%br
- - if params[:state] == "closed"
- State:
- = link_to "Open", course_discussions_path(params.merge(:state => "open"))
- |
- Closed
- -else
- State:
- Open
- |
- = link_to "Closed", course_discussions_path(params.merge(:state => "closed"))
+= @discussions.state_filters
%br
%br
-- @discussions.each do |discussion|
- %h3= discussion.subject
+= @discussions.matched_discussions

0 comments on commit 097c3e2

Please sign in to comment.
Something went wrong with that request. Please try again.