Add sort and group by options
parent
c1e2fca751
commit
ae8cda3791
|
@ -25,7 +25,7 @@ module Librarix
|
||||||
helpers Librarix::Helpers
|
helpers Librarix::Helpers
|
||||||
|
|
||||||
get '/' do
|
get '/' do
|
||||||
slim :index, locals: {movies: Librarix::Filter.new(params).movies}
|
slim :index, locals: {filter: Librarix::Filter.new(params)}
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/movie/:id' do |id|
|
get '/movie/:id' do |id|
|
||||||
|
|
|
@ -20,7 +20,7 @@ h2 {
|
||||||
font-family: Verdana,Arial,sans-serif;
|
font-family: Verdana,Arial,sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
ul#movies, ul#menu {
|
ul.movies, ul#menu {
|
||||||
list-style-type: none;
|
list-style-type: none;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
|
|
@ -1,32 +1,78 @@
|
||||||
module Librarix
|
module Librarix
|
||||||
class Filter
|
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
|
attr_reader :movies, :params
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(params)
|
||||||
@params = params
|
@params = params
|
||||||
filter
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def movies
|
||||||
|
@movies ||= begin
|
||||||
|
movies = Librarix::Redis::Movie.all
|
||||||
|
|
||||||
def filter
|
movies = self.class.filter_by_title(movies, params['title'])
|
||||||
@movies = Librarix::Redis::Movie.all
|
movies = self.class.filter_by_view_state(movies, params['view_state'])
|
||||||
by_title if @params.key?('title')
|
|
||||||
by_view_state if @params.key?('view_state')
|
|
||||||
|
|
||||||
@movies.sort_by!(&:release_date).reverse!
|
movies = self.class.sort(movies, sort)
|
||||||
|
movies = self.class.group(movies, group, sort)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def by_title
|
def group
|
||||||
movies.select! { |movie| movie.title.downcase.include?(params['title']) }
|
@group ||= params.key?('group') && params['group']
|
||||||
end
|
end
|
||||||
|
|
||||||
def by_view_state
|
def view_state
|
||||||
if params['view_state'] == 'viewed'
|
@view_state ||= if %w{all viewed not_viewed}.include?(params['view_state'])
|
||||||
movies.keep_if(&:viewed?)
|
params['view_state']
|
||||||
elsif params['view_state'] == 'not_viewed'
|
else
|
||||||
movies.delete_if(&:viewed?)
|
'all'
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,9 +7,5 @@ module Librarix
|
||||||
'default'
|
'default'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_search?(movies)
|
|
||||||
movies.empty? && params.key?('title')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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}
|
||||||
|
|
|
@ -7,17 +7,25 @@ form method="get" action="/" id="filter"
|
||||||
input type="submit" value="Filter"
|
input type="submit" value="Filter"
|
||||||
div id="search-more"
|
div id="search-more"
|
||||||
select name="view_state"
|
select name="view_state"
|
||||||
option value="all" selected=(params['view_state'].nil? || params['view_state'] == '' || params['view_state'] == 'all') All movies
|
option value="all" selected=(filter.view_state == 'all') All movies
|
||||||
option value="viewed" selected=(params['view_state'] == 'viewed') Only viewed
|
option value="viewed" selected=(filter.view_state == 'viewed') Only viewed
|
||||||
option value="not_viewed" selected=(params['view_state'] == 'not_viewed') Not 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"
|
select name="template"
|
||||||
option value="default" selected=(template == 'default') Default
|
option value="default" selected=(template == 'default') Default
|
||||||
option value="compact" selected=(template == 'compact') Compact
|
option value="compact" selected=(template == 'compact') Compact
|
||||||
option value="poster" selected=(template == 'poster') Poster
|
option value="poster" selected=(template == 'poster') Poster
|
||||||
|
|
||||||
|
|
||||||
== slim :list, locals: {movies: movies}
|
- filter.movies.each do |group, movies|
|
||||||
- if maybe_search?(movies)
|
- unless group == :all
|
||||||
|
h1 = group
|
||||||
|
== slim :list, locals: {movies: movies}
|
||||||
|
- if filter.maybe_search?
|
||||||
p
|
p
|
||||||
| Search for
|
| Search for
|
||||||
strong
|
strong
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
ul#movies class="#{template}"
|
ul.movies class="#{template}"
|
||||||
- movies.each do |movie|
|
- movies.each do |movie|
|
||||||
li
|
li
|
||||||
== slim :movie, locals: {movie: movie}
|
== slim :movie, locals: {movie: movie}
|
||||||
|
|
Loading…
Reference in New Issue