Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Discussion.search method and refactor DiscussionController

  • Loading branch information...
commit 770ef48f9c15593ae3b0e9fe543e2c11f64c7d61 1 parent 097c3e2
Jia Brown authored
View
29 app/controllers/discussions_controller.rb
@@ -2,33 +2,8 @@ class DiscussionsController < ApplicationController
before_filter :find_course
def index
- if params[:state] == "closed"
- @discussions = case params[:category]
- when "conversation"
- @course.discussions.inactive.conversations
- when "review"
- @course.discussions.inactive.reviews
- when "evaluation"
- @course.discussions.inactive.evaluations
- else
- @course.discussions.inactive.conversations
- end
-
- @discussions = DiscussionListDecorator.new(@discussions)
- else
- @discussions = case params[:category]
- when "conversation"
- @course.discussions.active.conversations
- when "review"
- @course.discussions.active.reviews
- when "evaluation"
- @course.discussions.active.evaluations
- else
- @course.discussions.active.conversations
- end
- end
-
- @discussions = DiscussionListDecorator.new(@discussions)
+ matches = Discussion.search(params)
+ @discussions = DiscussionListDecorator.new(matches)
end
private
View
6 app/decorators/discussion_list_decorator.rb
@@ -31,19 +31,19 @@ def matched_discussions
private
def conversations_link
- link_params = h.params.merge(:category => "conversation")
+ link_params = h.params.merge(:category => "conversations")
h.link_to "Conversations", h.course_discussions_path(link_params)
end
def reviews_link
- link_params = h.params.merge(:category => "review")
+ link_params = h.params.merge(:category => "reviews")
h.link_to "Reviews", h.course_discussions_path(link_params)
end
def evaluations_link
- link_params = h.params.merge(:category => "evaluation")
+ link_params = h.params.merge(:category => "evaluations")
h.link_to "Evaluations", h.course_discussions_path(link_params)
end
View
24 app/models/discussion.rb
@@ -1,6 +1,20 @@
-class Discussion < ActiveRecord::Base
- belongs_to :course
+class Discussion < ActiveRecord::Base
+ VALID_CATEGORIES = ["conversations", "reviews", "evaluations"]
+ belongs_to :course
+
+ def self.search(params)
+ results = params["state"] =="closed" ? inactive : active
+
+ category = params["category"]
+
+ if VALID_CATEGORIES.include?(category)
+ results.send(category)
+ else
+ results.conversations
+ end
+ end
+
def self.active
where(:archived => false)
end
@@ -10,14 +24,14 @@ def self.inactive
end
def self.conversations
- where(:category => "conversation")
+ where(:category => "conversations")
end
def self.reviews
- where(:category => "review")
+ where(:category => "reviews")
end
def self.evaluations
- where(:category => "evaluation")
+ where(:category => "evaluations")
end
end
View
6 db/seeds.rb
@@ -18,7 +18,7 @@
conversations.each do |e|
course.discussions.find_or_create_by_subject(
- :subject => e, :category => "conversation"
+ :subject => e, :category => "conversations"
)
end
@@ -28,7 +28,7 @@
evaluations.each do |e|
course.discussions.find_or_create_by_subject(
- :subject => e, :category => "evaluation"
+ :subject => e, :category => "evaluations"
)
end
@@ -38,6 +38,6 @@
reviews.each do |e|
course.discussions.find_or_create_by_subject(
- :subject => e, :category => "review"
+ :subject => e, :category => "reviews"
)
end
Please sign in to comment.
Something went wrong with that request. Please try again.