sinatra-paginate 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ == 0.1.0 (2012-06-18)
2
+
3
+ * Initial version.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+
3
+ gem 'haml'
4
+ gem 'nokogiri'
5
+ gem 'rack-test'
6
+ gem 'sinatra'
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ haml (3.1.6)
5
+ nokogiri (1.5.4)
6
+ rack (1.4.1)
7
+ rack-protection (1.2.0)
8
+ rack
9
+ rack-test (0.6.1)
10
+ rack (>= 1.0)
11
+ sinatra (1.3.2)
12
+ rack (~> 1.3, >= 1.3.6)
13
+ rack-protection (~> 1.2)
14
+ tilt (~> 1.3, >= 1.3.3)
15
+ tilt (1.3.3)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ haml
22
+ nokogiri
23
+ rack-test
24
+ sinatra
@@ -0,0 +1,34 @@
1
+ # Sinatra Paginate
2
+
3
+ A simple Sinatra pagination helper.
4
+
5
+ ## Example
6
+
7
+ ```ruby
8
+ require 'sinatra/base'
9
+ require 'sinatra/paginate'
10
+
11
+ Struct.new('Result', :total, :size, :tuples)
12
+
13
+ class MyApp < Sinatra::Base
14
+ register Sinatra::Paginate
15
+ get '/' do
16
+ @result = Struct::Result.new(User.count, 10, User.all(limit: 10, offset: params[:page].to_i * 10)
17
+ haml :index
18
+ end
19
+ end
20
+ ```
21
+
22
+ ```haml
23
+ -# views/index.haml
24
+
25
+ %ul
26
+ - @result.tuples.each do |user|
27
+ %li
28
+ %span= user.id
29
+ %span= user.name
30
+
31
+ = paginate @result
32
+ ```
33
+ # License
34
+ [Creative Commons Attribution - CC BY](http://creativecommons.org/licenses/by/3.0)
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+ require 'sinatra/base'
3
+ require 'uri'
4
+
5
+ module Sinatra
6
+ module Paginate
7
+ DEFAULTS = {items_per_page: 10, width: 5, renderer: 'haml', labels: {first: '«', last: '»'}}
8
+
9
+ def paginate resource, options = {}
10
+ raise ArgumentError, 'resource should respond to :total' unless resource.respond_to?(:total)
11
+ raise ArgumentError, 'resource should respond to :size' unless resource.respond_to?(:size)
12
+
13
+ options = DEFAULTS.merge(options)
14
+ view = options.fetch(:view, paginate_haml)
15
+ renderer = options.fetch(:renderer)
16
+ send(renderer, view, layout: false, locals: paginate_options(resource, options))
17
+ end
18
+
19
+ def paginate_options resource, options
20
+ last = (resource.total / options[:items_per_page].to_f).ceil
21
+ page = [1, params['page'].to_i].max
22
+ from = [1, page - (options[:width] >> 1)].max
23
+ to = [from + options[:width] - 1, last].min
24
+
25
+ while (to - from + 1) < options[:width] && (to < last || from > 1)
26
+ to += 1 if to < last
27
+ from -= 1 if from > 1
28
+ end
29
+ {page: page, from: from, to: to, last: last, uri: options.fetch(:uri, request.path_info), labels: options.fetch(:labels)}
30
+ end
31
+
32
+ def paginate_haml
33
+ @@haml ||= File.read(__FILE__).sub %r{^.*__END__}m, ''
34
+ end
35
+
36
+ def page
37
+ page = params['page'].to_i
38
+ page > 0 ? page : 1
39
+ end
40
+
41
+ def pagination_url *path
42
+ params = path[-1].respond_to?(:to_hash) ? path.delete_at(-1).to_hash : {}
43
+ params = params.empty? ? '' : '?' + URI.escape(params.map{|*a| a.join('=')}.join('&')).to_s
44
+ ['/', path.compact.map(&:to_s)].flatten.join('/').gsub(%r{/+}, '/') + params
45
+ end
46
+
47
+ def self.registered app
48
+ app.helpers self
49
+ end
50
+ end # Paginate
51
+ end # Sinatra
52
+
53
+ __END__
54
+ :css
55
+ ul.sinatra-pagination > li {
56
+ float: left;
57
+ margin-right: 10px;
58
+ display: inline-block;
59
+ list-style-type: none;
60
+ }
61
+
62
+ - pagination_css = {-1 => 'btn-prev', 1 => 'btn-next', 0 => 'btn-primary'}
63
+ %ul.sinatra-pagination
64
+ %li
65
+ %a.btn.btn-first{href: pagination_url(uri, params.merge('page' => 1))}= labels[:first]
66
+ - (from..to).each do |n|
67
+ %li
68
+ %a{href: pagination_url(uri, params.merge('page' => n)), class: "btn #{pagination_css[n - page]}"}= n
69
+ %li
70
+ %a.btn.btn-last{href: pagination_url(uri, params.merge('page' => last))}= labels[:last]
@@ -0,0 +1,19 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'nokogiri'
4
+ require 'rack/test'
5
+ require 'sinatra/paginate'
6
+ require 'minitest/autorun'
7
+
8
+ Struct.new('Result', :total, :size)
9
+
10
+ class MiniTest::Spec
11
+ include Rack::Test::Methods
12
+
13
+ def app &block
14
+ if block
15
+ @app = Class.new(Sinatra::Base) { register Sinatra::Paginate; instance_eval(&block)}
16
+ end
17
+ @app
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+ require 'helper'
3
+
4
+ describe 'haml pagination' do
5
+ before do
6
+ app do
7
+ get '/' do
8
+ paginate Struct::Result.new(60, 5), items_per_page: 5
9
+ end
10
+ end
11
+ end
12
+
13
+ def pagination_li
14
+ Nokogiri::HTML(last_response.body).css('ul.sinatra-pagination > li')
15
+ end
16
+
17
+ def status
18
+ last_response.status
19
+ end
20
+
21
+ def pagination_current
22
+ pagination_li.at('.btn-primary').parent
23
+ end
24
+
25
+ it 'should render pagination' do
26
+ get '/'
27
+ assert_equal 200, status
28
+ assert_equal 7, pagination_li.count # first, 1..5, last
29
+ assert_equal '1', pagination_current.text.strip
30
+ assert_equal '«', pagination_li.at('.btn-first').text
31
+ assert_equal '»', pagination_li.at('.btn-last').text
32
+ end
33
+
34
+ it 'should paginate to page 2' do
35
+ get '/', page: 2
36
+ assert_equal 200, status
37
+ assert_equal '2', pagination_current.text.strip
38
+ assert_equal '/?page=1', pagination_li.at('.btn-first')['href']
39
+ assert_equal '/?page=12', pagination_li.at('.btn-last')['href']
40
+ assert_equal %w(« 1 2 3 4 5 »), pagination_li.map {|li| li.text.strip}
41
+ end
42
+
43
+ it 'should paginate to page n' do
44
+ get '/', page: 5
45
+ assert_equal 200, status
46
+ assert_equal '5', pagination_current.text.strip
47
+ assert_equal %w(« 3 4 5 6 7 »), pagination_li.map {|li| li.text.strip}
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-paginate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bharanee Rathna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack-test
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Simple Sinatra pagination helper.
63
+ email:
64
+ - deepfryed@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - test/helper.rb
70
+ - test/test_haml.rb
71
+ - lib/sinatra/paginate.rb
72
+ - README.md
73
+ - Gemfile
74
+ - Gemfile.lock
75
+ - CHANGELOG
76
+ homepage: http://github.com/deepfryed/sinatra-paginate
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Sinatra pagination helper.
100
+ test_files: []