From ae8cda3791a7dc433e5d96b784cfe1215cbc4b00 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Fri, 13 Nov 2015 12:54:45 +0100 Subject: [PATCH] Add sort and group by options --- lib/librarix/application.rb | 2 +- lib/librarix/assets/application.scss | 2 +- lib/librarix/filter.rb | 82 ++++++++++++++++++++++------ lib/librarix/helpers.rb | 4 -- lib/librarix/public/application.css | 2 +- lib/librarix/views/index.slim | 18 ++++-- lib/librarix/views/list.slim | 2 +- 7 files changed, 81 insertions(+), 31 deletions(-) diff --git a/lib/librarix/application.rb b/lib/librarix/application.rb index db93d3c..f1d9d9c 100644 --- a/lib/librarix/application.rb +++ b/lib/librarix/application.rb @@ -25,7 +25,7 @@ module Librarix helpers Librarix::Helpers get '/' do - slim :index, locals: {movies: Librarix::Filter.new(params).movies} + slim :index, locals: {filter: Librarix::Filter.new(params)} end get '/movie/:id' do |id| diff --git a/lib/librarix/assets/application.scss b/lib/librarix/assets/application.scss index 2af243a..a56773e 100644 --- a/lib/librarix/assets/application.scss +++ b/lib/librarix/assets/application.scss @@ -20,7 +20,7 @@ h2 { font-family: Verdana,Arial,sans-serif; } -ul#movies, ul#menu { +ul.movies, ul#menu { list-style-type: none; margin: 0; padding: 0; diff --git a/lib/librarix/filter.rb b/lib/librarix/filter.rb index 73951d9..71538eb 100644 --- a/lib/librarix/filter.rb +++ b/lib/librarix/filter.rb @@ -1,32 +1,78 @@ module Librarix class Filter + def self.filter_by_title(movies, title) + return movies unless title + movies.select { |movie| movie.title.downcase.include?(title) } + end + + def self.filter_by_view_state(movies, view_state) + if view_state == 'viewed' + movies.select(&:viewed?) + elsif view_state == 'not_viewed' + movies.reject(&:viewed?) + else + movies + end + end + + def self.sort(movies, sort) + if sort == 'date' + movies.sort_by(&:release_date).reverse + else + movies.sort_by(&:title) + end + end + + def self.group(movies, group, sort) + return {all: movies} unless group + + if sort == 'date' + movies.group_by { |movie| movie.release_date.year } + else + movies.group_by { |movie| movie.title[0].upcase } + end + end + attr_reader :movies, :params def initialize(params) @params = params - filter end - private + def movies + @movies ||= begin + movies = Librarix::Redis::Movie.all - def filter - @movies = Librarix::Redis::Movie.all - by_title if @params.key?('title') - by_view_state if @params.key?('view_state') + movies = self.class.filter_by_title(movies, params['title']) + movies = self.class.filter_by_view_state(movies, params['view_state']) - @movies.sort_by!(&:release_date).reverse! - end - - def by_title - movies.select! { |movie| movie.title.downcase.include?(params['title']) } - end - - def by_view_state - if params['view_state'] == 'viewed' - movies.keep_if(&:viewed?) - elsif params['view_state'] == 'not_viewed' - movies.delete_if(&:viewed?) + movies = self.class.sort(movies, sort) + movies = self.class.group(movies, group, sort) end end + + def group + @group ||= params.key?('group') && params['group'] + end + + def view_state + @view_state ||= if %w{all viewed not_viewed}.include?(params['view_state']) + params['view_state'] + else + 'all' + end + end + + def sort + @sort ||= if %w{alphabetical date}.include?(params['sort']) + params['sort'] + else + 'date' + end + end + + def maybe_search? + movies.empty? && params.key?('title') + end end end diff --git a/lib/librarix/helpers.rb b/lib/librarix/helpers.rb index 972ad43..e13e285 100644 --- a/lib/librarix/helpers.rb +++ b/lib/librarix/helpers.rb @@ -7,9 +7,5 @@ module Librarix 'default' end end - - def maybe_search?(movies) - movies.empty? && params.key?('title') - end end end diff --git a/lib/librarix/public/application.css b/lib/librarix/public/application.css index 611cc15..dd5b889 100644 --- a/lib/librarix/public/application.css +++ b/lib/librarix/public/application.css @@ -1 +1 @@ -body{margin:10px}a{text-decoration:none;color:#666}a:hover{color:#888}h2{margin-top:0;overflow:hidden;text-overflow:ellipsis;font-size:1.2em;font-family:Verdana,Arial,sans-serif}ul#movies,ul#menu{list-style-type:none;margin:0;padding:0}ul#menu{display:flex;flex-flow:row wrap;justify-content:flex-end}ul#menu li{margin:5px}form #search-main{display:flex}form #search-main input{border-radius:0.4em;border:solid 3px #f4f4f4}form #search-main input[type="submit"]{background-color:#f4f4f4}form #search-main input[type="text"]{flex-grow:1;min-width:50px;font-size:1.4em;padding:5px}.default li,.compact li{padding:10px 0}.default li:nth-child(even),.compact li:nth-child(even){background-color:#f4f4f4}.default .movie,.compact .movie{display:flex;flex-flow:row wrap;align-items:center;justify-content:center}.default .movie .poster,.compact .movie .poster{margin-right:10px}.default .movie .informations,.compact .movie .informations{align-self:flex-start;flex:1 1 300px}.poster{display:flex;flex-flow:row wrap}.poster .movie{width:160px} +body{margin:10px}a{text-decoration:none;color:#666}a:hover{color:#888}h2{margin-top:0;overflow:hidden;text-overflow:ellipsis;font-size:1.2em;font-family:Verdana,Arial,sans-serif}ul.movies,ul#menu{list-style-type:none;margin:0;padding:0}ul#menu{display:flex;flex-flow:row wrap;justify-content:flex-end}ul#menu li{margin:5px}form #search-main{display:flex}form #search-main input{border-radius:0.4em;border:solid 3px #f4f4f4}form #search-main input[type="submit"]{background-color:#f4f4f4}form #search-main input[type="text"]{flex-grow:1;min-width:50px;font-size:1.4em;padding:5px}.default li,.compact li{padding:10px 0}.default li:nth-child(even),.compact li:nth-child(even){background-color:#f4f4f4}.default .movie,.compact .movie{display:flex;flex-flow:row wrap;align-items:center;justify-content:center}.default .movie .poster,.compact .movie .poster{margin-right:10px}.default .movie .informations,.compact .movie .informations{align-self:flex-start;flex:1 1 300px}.poster{display:flex;flex-flow:row wrap}.poster .movie{width:160px} diff --git a/lib/librarix/views/index.slim b/lib/librarix/views/index.slim index 91471b7..a6897d0 100644 --- a/lib/librarix/views/index.slim +++ b/lib/librarix/views/index.slim @@ -7,17 +7,25 @@ form method="get" action="/" id="filter" input type="submit" value="Filter" div id="search-more" select name="view_state" - option value="all" selected=(params['view_state'].nil? || params['view_state'] == '' || params['view_state'] == 'all') All movies - option value="viewed" selected=(params['view_state'] == 'viewed') Only viewed - option value="not_viewed" selected=(params['view_state'] == 'not_viewed') Not viewed + option value="all" selected=(filter.view_state == 'all') All movies + option value="viewed" selected=(filter.view_state == 'viewed') Only viewed + option value="not_viewed" selected=(filter.view_state == 'not_viewed') Not viewed + select name="sort" + option value="alphabetical" selected=(filter.sort == 'alphabetical') Alphabetical + option value="date" selected=(filter.sort == 'date') Release date + input type="checkbox" name="group" id="group" checked=(filter.group) + label for="group" Group by sort select name="template" option value="default" selected=(template == 'default') Default option value="compact" selected=(template == 'compact') Compact option value="poster" selected=(template == 'poster') Poster -== slim :list, locals: {movies: movies} -- if maybe_search?(movies) +- filter.movies.each do |group, movies| + - unless group == :all + h1 = group + == slim :list, locals: {movies: movies} +- if filter.maybe_search? p | Search for strong diff --git a/lib/librarix/views/list.slim b/lib/librarix/views/list.slim index 23e0080..9a5258e 100644 --- a/lib/librarix/views/list.slim +++ b/lib/librarix/views/list.slim @@ -1,4 +1,4 @@ -ul#movies class="#{template}" +ul.movies class="#{template}" - movies.each do |movie| li == slim :movie, locals: {movie: movie}