Permalink
Browse files

HW2 - part b - filtering via checkboxes while retaining the sortBy fu…

…nctionality
  • Loading branch information...
1 parent 455cdf7 commit 1afb3a0ccdba9e2f12521b88d9cccafc5a1248f5 @chinpira committed Aug 15, 2012
Showing with 27 additions and 6 deletions.
  1. +12 −3 app/controllers/movies_controller.rb
  2. +3 −0 app/models/movie.rb
  3. +12 −3 app/views/movies/index.html.haml
@@ -8,12 +8,21 @@ def show
def index
sort_by = params[:sort]
+ if params.has_key?(:ratings)
+ ratings = params[:ratings].keys
+ else
+ ratings = []
+ end
+ params.has_key?(:ratings) ? @ratings_selected = params[:ratings].keys : @ratings_selected = []
+ @all_ratings = Movie.possible_ratings
+ puts "Here are ratings: #{ratings}"
+ #@all_ratings = ['G','PG','PG-13','R']
if sort_by == "sortByTitle"
- @movies = Movie.find(:all, :order => 'title ASC')
+ @movies = Movie.find(:all, :order => 'title ASC', :conditions => [ "rating IN (?)", ratings])
elsif sort_by == "sortByDate"
- @movies = Movie.find(:all, :order => 'release_date ASC')
+ @movies = Movie.find(:all, :order => 'release_date ASC', :conditions => [ "rating IN (?)", ratings])
else
- @movies = Movie.all
+ @movies = Movie.find(:all, :conditions => [ "rating IN (?)", ratings])
end
end
View
@@ -1,2 +1,5 @@
class Movie < ActiveRecord::Base
+ def self.possible_ratings
+ self.select(:rating).map(&:rating).uniq.sort
+ end
end
@@ -1,12 +1,21 @@
-# This file is app/views/movies/index.html.haml
%h1 All Movies
-
+= puts "Params: #{params}"
+= form_tag movies_path, :method => :get do
+ Include:
+ - @all_ratings.each do |rating|
+ = rating
+ - if (@ratings_selected.include?(rating))
+ = check_box_tag "ratings[#{rating}]", 1, true, :id => "ratings_#{rating}"
+ - else
+ = check_box_tag "ratings[#{rating}]", 1, false, :id => "ratings_#{rating}"
+ = submit_tag 'Refresh', :id => "ratings_submit"
%table#movies
%thead
%tr
- %th{:class => (params[:sort] == "sortByTitle" ? 'hilite' : nil)}= link_to "Movie Title", movies_path(:sort => "sortByTitle"), :id => "title_header"
+ %th{:class => (params[:sort] == "sortByTitle" ? 'hilite' : nil)}= link_to "Movie Title", movies_path({:sort => "sortByTitle", :ratings => params[:ratings]}), :id => "title_header"
%th Rating
- %th{:class => (params[:sort] == "sortByDate" ? 'hilite' : nil)}= link_to "Release Date", movies_path(:sort => "sortByDate"), :id => "release_date_header"
+ %th{:class => (params[:sort] == "sortByDate" ? 'hilite' : nil)}= link_to "Release Date", movies_path(:sort => "sortByDate",:ratings => params[:ratings]), :id => "release_date_header"
%th More Info
%tbody
- @movies.each do |movie|

0 comments on commit 1afb3a0

Please sign in to comment.