Add filter form for movies

master
Guillaume Dott 2015-10-30 12:59:58 +01:00
parent 510bf65dcd
commit 6e0eab0c74
5 changed files with 31 additions and 3 deletions

View File

@ -1,3 +1,4 @@
require 'librarix/filter'
require 'librarix/menu' require 'librarix/menu'
require 'librarix/the_movie_db' require 'librarix/the_movie_db'
@ -22,7 +23,7 @@ module Librarix
helpers Librarix::Menu::Helper helpers Librarix::Menu::Helper
get '/' do get '/' do
slim :index, locals: {movies: Librarix::Redis::Movie.all.sort_by(&:release_date).reverse} slim :index, locals: {movies: Librarix::Filter.new(params).movies}
end end
get '/search' do get '/search' do

View File

@ -6,7 +6,7 @@ h2 {
margin-top: 0; margin-top: 0;
} }
#search { body > form {
display: flex; display: flex;
input { input {

View File

@ -0,0 +1,23 @@
module Librarix
class Filter
attr_reader :movies, :params
def initialize(params)
@params = params
filter
end
private
def filter
@movies = Librarix::Redis::Movie.all
by_title if @params.key?('title')
@movies.sort_by!(&:release_date).reverse!
end
def by_title
movies.select! { |movie| movie.title.downcase.include?(params['title']) }
end
end
end

View File

@ -1 +1 @@
body{margin:10px}h2{margin-top:0}#search{display:flex}#search input{border-radius:0.4em;border:solid 3px #f4f4f4}#search input[type="submit"]{background-color:#f4f4f4}#search input[type="text"]{flex-grow:1;font-size:1.4em;padding:5px}ul{list-style-type:none;margin:0;padding:0}ul li{padding:10px 0}ul li:nth-child(even){background-color:#f4f4f4}.movie{display:flex;flex-flow:row wrap;align-items:center;justify-content:center}.movie .poster{margin-right:10px}.movie .informations{align-self:flex-start;flex:1 1 300px} body{margin:10px}h2{margin-top:0}body>form{display:flex}body>form input{border-radius:0.4em;border:solid 3px #f4f4f4}body>form input[type="submit"]{background-color:#f4f4f4}body>form input[type="text"]{flex-grow:1;font-size:1.4em;padding:5px}ul{list-style-type:none;margin:0;padding:0}ul li{padding:10px 0}ul li:nth-child(even){background-color:#f4f4f4}.movie{display:flex;flex-flow:row wrap;align-items:center;justify-content:center}.movie .poster{margin-right:10px}.movie .informations{align-self:flex-start;flex:1 1 300px}

View File

@ -1,3 +1,7 @@
- content_for(:title) { 'Index' } - content_for(:title) { 'Index' }
form method="get" action="/" id="filter"
input type="text" name="title" value="#{params['title']}" autocomplete="off"
input type="submit" value="Filter"
== slim :list, locals: {movies: movies} == slim :list, locals: {movies: movies}