Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

part 2 of assignment 2 done

can filter based on rating
  • Loading branch information...
commit 9c4149ba54af642e7aba21fc1a2334ebec6c0806 1 parent f840577
@chaitanyakuber authored
View
14 app/controllers/movies_controller.rb
@@ -7,8 +7,18 @@ def show
end
def index
- @movies = (params.key?(:sort)) ? Movie.find(:all, :order=>params[:sort]+' ASC') : Movie.all;
- @sort = params[:sort] if params.key?(:sort)
+ @ratings = params[:ratings]
+ if params[:ratings] && params[:sort_by]
+ @movies = Movie.find(:all, :order=>params[:sort_by]+' ASC', :conditions => ["rating IN (?)", params[:ratings].keys])
+ elsif params[:ratings]
+ @movies = Movie.find(:all, :conditions => ["rating IN (?)", params[:ratings].keys])
+ elsif params[:sort_by]
+ @movies = Movie.find(:all, :order=>params[:sort_by]+' ASC')
+ else
+ @movies = Movie.all
+ end
+
+ @all_ratings = Movie.ratings
end
def new
View
10 app/models/movie.rb
@@ -1,2 +1,12 @@
class Movie < ActiveRecord::Base
+ ALL_RATINGS = ["G", "PG", "PG-13", "R"]
+
+ def self.ratings
+ return ALL_RATINGS
+ end
+
+ def self.filter_on_ratings(ratings)
+ where(rating: ratings)
+ end
+
end
View
11 app/views/movies/index.html.haml
@@ -1,12 +1,19 @@
-# This file is app/views/movies/index.html.haml
%h1 All Movies
+= form_tag movies_path, :method => :get do
+ Include:
+ - @all_ratings.each do |rating|
+ = rating
+ = check_box_tag "ratings[#{rating}]", 1, (@ratings != nil and @ratings.include?(rating)), :id => 'ratings_#{rating}'
+ = submit_tag 'Refresh'
+
%table#movies
%thead
%tr
- %th{:class => ("hilite" if @sort == "title")}= link_to "Movie Title", movies_path({:sort=>:title}), :id=>'title_header'
+ %th{:class => ("hilite" if @sort_by == :title.to_s)}= link_to 'Movie Title', movies_path(:sort_by => :title, :ratings => params[:ratings]), :id => 'title_header'
%th Rating
- %th{:class => ("hilite" if @sort == "release_date")}= link_to "Release Date", movies_path({:sort=>:release_date}), :id=>'release_date_header'
+ %th{:class => ("hilite" if @sort_by == :release_date.to_s)}= link_to 'Release Date', movies_path(:sort_by => :release_date, :ratings => params[:ratings]), :id => 'release_date_header'
%th More Info
%tbody
- @movies.each do |movie|
Please sign in to comment.
Something went wrong with that request. Please try again.