Add sort and group by options

master
Guillaume Dott 2015-11-13 12:54:45 +01:00
parent c1e2fca751
commit ae8cda3791
7 changed files with 81 additions and 31 deletions

View File

@ -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|

View File

@ -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;

View File

@ -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!
movies = self.class.sort(movies, sort)
movies = self.class.group(movies, group, sort)
end
end
def by_title
movies.select! { |movie| movie.title.downcase.include?(params['title']) }
def group
@group ||= params.key?('group') && params['group']
end
def by_view_state
if params['view_state'] == 'viewed'
movies.keep_if(&:viewed?)
elsif params['view_state'] == 'not_viewed'
movies.delete_if(&:viewed?)
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

View File

@ -7,9 +7,5 @@ module Librarix
'default'
end
end
def maybe_search?(movies)
movies.empty? && params.key?('title')
end
end
end

View File

@ -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}

View File

@ -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
- filter.movies.each do |group, movies|
- unless group == :all
h1 = group
== slim :list, locals: {movies: movies}
- if maybe_search?(movies)
- if filter.maybe_search?
p
| Search for
strong

View File

@ -1,4 +1,4 @@
ul#movies class="#{template}"
ul.movies class="#{template}"
- movies.each do |movie|
li
== slim :movie, locals: {movie: movie}