From c4fe00086198f5a2e8e350451f79c7fdda67e458 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 26 Apr 2015 09:42:23 -0400 Subject: [PATCH] Initial pagination implementation --- Gemfile | 2 ++ Gemfile.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++ app.rb | 6 +----- environments.rb | 22 +++++++++++++++++----- jobs/leaders.rb | 6 +----- views/index.erb | 2 ++ 6 files changed, 68 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 2ec22dc..6fa2251 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,8 @@ gem 'sinatra-redirect-with-flash' gem 'rake' gem 'json' gem 'mechanize' +gem 'padrino-helpers' +gem 'kaminari', :require => 'kaminari/sinatra' group :production do gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index 6c04275..344e4eb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,19 @@ GEM remote: https://rubygems.org/ specs: + actionpack (4.2.1) + actionview (= 4.2.1) + activesupport (= 4.2.1) + rack (~> 1.6) + rack-test (~> 0.6.2) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.1) + actionview (4.2.1) + activesupport (= 4.2.1) + builder (~> 3.1) + erubis (~> 2.7.0) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.1) activemodel (4.2.1) activesupport (= 4.2.1) builder (~> 3.1) @@ -18,12 +31,21 @@ GEM builder (3.2.2) domain_name (0.5.24) unf (>= 0.0.5, < 1.0.0) + erubis (2.7.0) foreman (0.78.0) thor (~> 0.19.1) http-cookie (1.0.2) domain_name (~> 0.5) + http_router (0.5.4) + rack (>= 1.0.0) + url_mount (~> 0.2.1) i18n (0.7.0) json (1.8.2) + kaminari (0.16.3) + actionpack (>= 3.0.0) + activesupport (>= 3.0.0) + loofah (2.0.1) + nokogiri (>= 1.5.9) mechanize (2.7.3) domain_name (~> 0.5, >= 0.5.1) http-cookie (~> 1.0) @@ -41,10 +63,29 @@ GEM nokogiri (1.6.6.2) mini_portile (~> 0.6.0) ntlm-http (0.1.1) + padrino-core (0.9.21) + activesupport (>= 3.0.0) + http_router (~> 0.5.4) + sinatra (>= 1.1.0) + thor (>= 0.14.3) + tzinfo + padrino-helpers (0.9.21) + i18n (>= 0.4.1) + padrino-core (= 0.9.21) pg (0.18.1) rack (1.6.0) rack-protection (1.5.3) rack + rack-test (0.6.3) + rack (>= 1.0) + rails-deprecated_sanitizer (1.0.3) + activesupport (>= 4.2.0.alpha) + rails-dom-testing (1.0.6) + activesupport (>= 4.2.0.beta, < 5.0) + nokogiri (~> 1.6.0) + rails-deprecated_sanitizer (>= 1.0.1) + rails-html-sanitizer (1.0.2) + loofah (~> 2.0) rake (10.4.2) sinatra (1.4.6) rack (~> 1.4) @@ -66,6 +107,8 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.7.1) + url_mount (0.2.1) + rack webrobots (0.1.1) PLATFORMS @@ -75,7 +118,9 @@ DEPENDENCIES activerecord foreman json + kaminari mechanize + padrino-helpers pg rake sinatra diff --git a/app.rb b/app.rb index 15b8a53..759e94b 100644 --- a/app.rb +++ b/app.rb @@ -1,6 +1,3 @@ -require 'sinatra' -require 'sinatra/activerecord' -require 'json' require './environments' require './models/raid' @@ -12,8 +9,7 @@ set :public_folder, 'public' # CONTROLLER get '/' do - @raids = Raid.all.reverse - + @raids = Raid.order('created_at DESC').page(params[:page]) leaders = Leader.all last_update = leaders.first ? leaders.first.created_at : DateTime.new(0) rebuild_leaders if last_update + 300 < DateTime.now diff --git a/environments.rb b/environments.rb index 535a00c..a6d0569 100644 --- a/environments.rb +++ b/environments.rb @@ -1,17 +1,29 @@ +require 'rubygems' +require 'bundler' +Bundler.require + +register Kaminari::Helpers::SinatraHelpers + +require './models/raid' +require './models/leader' +require './helpers/raids_helper' +require './jobs/leaders' + + configure :development do - set :database, 'sqlite3:dev.db' - set :show_exceptions, true + set :database, 'sqlite3:dev.db' + set :show_exceptions, true end configure :production do - db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb') + db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb') - ActiveRecord::Base.establish_connection( + ActiveRecord::Base.establish_connection( :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, :host => db.host, :username => db.user, :password => db.password, :database => db.path[1..-1], :encoding => 'utf8' - ) + ) end diff --git a/jobs/leaders.rb b/jobs/leaders.rb index a327430..9388ca4 100644 --- a/jobs/leaders.rb +++ b/jobs/leaders.rb @@ -1,8 +1,4 @@ -require 'sinatra/activerecord' -require 'mechanize' -require 'nokogiri' - -require './models/leader' +require './environments' def rebuild_leaders puts "rebuilding_leaders called" diff --git a/views/index.erb b/views/index.erb index fa8cf99..7321870 100644 --- a/views/index.erb +++ b/views/index.erb @@ -21,6 +21,8 @@ <% end %> + + <%= paginate @raids %>