mobile_pagination 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-gemset
19
+ .ruby-version
20
+ .bundle
21
+ vendor/ruby
22
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Luke Fender
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # MobilePagination
2
+
3
+ ## Description
4
+ Rack gem for producing minimal pagination links.
5
+
6
+ ![<Display Name>](http://i.imgur.com/qZcqfx8.png)
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'mobile_pagination'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mobile_pagination
22
+
23
+ ## Usage
24
+ Initializes with 4 options: `current_page, total_pages, query, path`
25
+
26
+ ```ruby
27
+ require 'mobile_pagination'
28
+
29
+ # http://local.m.newhomeguide.com/New-Homes/Georgia/Atlanta/?page=2
30
+
31
+ def opts
32
+ {
33
+ :current_page => params[:page], # => 2
34
+ :total_pages => total_pages, # => Int for total pages
35
+ :query => request.query_string, # => 'page=2'
36
+ :path => request.path # => '/New-Homes/Georgia/Atlanta/'
37
+ }
38
+ end
39
+
40
+ def pagination
41
+ MobilePagination::Paginate.new(opts).html
42
+ end
43
+
44
+ # in your view, you may have something like this:
45
+ ol
46
+ = pagination
47
+ ```
48
+
49
+ ### Configuration
50
+ Configuration is optional. Say for example you were paginating slides on a slideshow - instead of using page, you could configure the gem to use a different key.
51
+
52
+ ```ruby
53
+ MobilePagination.configure do |config|
54
+ config.page_key = 'slide'
55
+ end
56
+ ```
57
+
58
+ Configuration must run prior to initialization. The resulting pagination links will now contain `/?slide=2, /?slide=3` to suit your custom url structure. The default page_key is `page`.
59
+
60
+ ### Overrides
61
+
62
+ If you don't like list items, you will need to override `MobilePagination::Templates`
63
+
64
+ Just add this to the bottom of one of your view helpers:
65
+
66
+ ```ruby
67
+ module MobilePagination
68
+ module Templates
69
+
70
+ def first_page_html
71
+ "<p'>
72
+ <a title='First Page' href='#{first_page_link}'>Luke's Page</a> |
73
+ <p>"
74
+ end
75
+
76
+ def previous_page_html
77
+ "<h1>
78
+ <a title='Previous Page' href='#{previous_page_link}'>Previous Page</a> |
79
+ </h1>"
80
+ end
81
+
82
+ def next_page_html
83
+ "<span>
84
+ <a title='Next Page' href='#{next_page_link}'>Next Page</a> |
85
+ </span>"
86
+ end
87
+
88
+ def last_page_html
89
+ "<h3>
90
+ <a title='Last Page' href='#{last_page_link}'>Last Page</a>
91
+ </h3>"
92
+ end
93
+
94
+ end
95
+ end
96
+ ```
97
+
98
+ Please note, any methods ending in `_link` will need to remain, as these methods are responsible for generating the paginated urls.
99
+
100
+
101
+ ## Contributing
102
+
103
+ 1. Fork it
104
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
105
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
106
+ 4. Push to the branch (`git push origin my-new-feature`)
107
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ require_relative 'mobile_pagination/initialize'
2
+
3
+ module MobilePagination
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configure
9
+ @configuration ||= Configuration.new
10
+ yield(configuration) if block_given?
11
+ end
12
+
13
+ end
14
+
15
+ MobilePagination.configure unless block_given?
@@ -0,0 +1,11 @@
1
+ module MobilePagination
2
+ class Configuration
3
+
4
+ attr_accessor :page_key
5
+
6
+ def initialize(data={})
7
+ @page_key = data[:page_key] || 'page'
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'uri'
2
+ require_relative 'configuration'
3
+ require_relative 'utils/initialize'
4
+ require_relative 'templates'
5
+ require_relative 'paginate'
@@ -0,0 +1,78 @@
1
+ module MobilePagination
2
+ class Paginate
3
+
4
+ include Utils
5
+ include Templates
6
+
7
+ attr_reader :total_pages, :current_page, :query_params, :path
8
+
9
+ def initialize(opts)
10
+ @total_pages = opts[:total_pages].to_i || 0
11
+ @current_page = current(opts[:current_page])
12
+ @query_params = query_to_hash(opts[:query])
13
+ @path = opts[:path] || '/'
14
+ end
15
+
16
+ def first_page_link
17
+ "#{page_url}"
18
+ end
19
+
20
+ def previous_page_link
21
+ previous == 1 ? "#{first_page_link}" : "#{page_url(previous)}"
22
+ end
23
+
24
+ def next_page_link
25
+ "#{page_url(@current_page + 1)}" if @current_page < @total_pages
26
+ end
27
+
28
+ def last_page_link
29
+ "#{page_url(@total_pages)}"
30
+ end
31
+
32
+ private
33
+
34
+ def should_paginate?
35
+ @total_pages > 1
36
+ end
37
+
38
+ def previous_page?
39
+ @current_page > 1
40
+ end
41
+
42
+ def next_page?
43
+ not next_page_link.nil?
44
+ end
45
+
46
+ def current(page)
47
+ page = page.nil? ? 1 : page.to_i
48
+ page > @total_pages ? @total_pages : page
49
+ end
50
+
51
+ def previous
52
+ @current_page - 1
53
+ end
54
+
55
+ def page_url(page=nil)
56
+ page.nil? ? "#{@path}#{qs_without_key}" : "#{@path}#{qs_with_key(page)}"
57
+ end
58
+
59
+
60
+ def qs_with_key(page)
61
+ "?#{hash_to_query(opts_with_key(page))}"
62
+ end
63
+
64
+ def qs_without_key
65
+ "?#{hash_to_query(opts_without_key)}"
66
+ end
67
+
68
+ def opts_with_key(page)
69
+ @query_params.merge({ MobilePagination.configuration.page_key => page })
70
+ end
71
+
72
+ def opts_without_key
73
+ @query_params.delete(MobilePagination.configuration.page_key)
74
+ @query_params
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,39 @@
1
+ module MobilePagination
2
+ module Templates
3
+
4
+ def first_page_html
5
+ "<li>
6
+ <a title='First Page' href='#{first_page_link}'>First Page</a> |
7
+ </li>"
8
+ end
9
+
10
+ def previous_page_html
11
+ "<li>
12
+ <a title='Previous Page' href='#{previous_page_link}'>Previous Page</a> |
13
+ </li>"
14
+ end
15
+
16
+ def next_page_html
17
+ "<li>
18
+ <a title='Next Page' href='#{next_page_link}'>Next Page</a> |
19
+ </li>"
20
+ end
21
+
22
+ def last_page_html
23
+ "<li>
24
+ <a title='Last Page' href='#{last_page_link}'>Last Page</a>
25
+ </li>"
26
+ end
27
+
28
+ def html
29
+ return '' unless should_paginate?
30
+ ''.tap do |markup|
31
+ markup << first_page_html if previous_page?
32
+ markup << previous_page_html if previous_page?
33
+ markup << next_page_html if next_page?
34
+ markup << last_page_html if next_page?
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'version'
2
+ require_relative 'utils'
@@ -0,0 +1,13 @@
1
+ module MobilePagination
2
+ module Utils
3
+
4
+ def query_to_hash(qs)
5
+ Rack::Utils.parse_nested_query(qs) || {}
6
+ end
7
+
8
+ def hash_to_query(hash)
9
+ URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module MobilePagination
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ require 'date'
3
+ require './lib/mobile_pagination/utils/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "mobile_pagination"
7
+ gem.authors = ["Luke Fender"]
8
+ gem.email = ["lfender6445@gmail.com"]
9
+ gem.description = %q{Creates a series of pagination elements relative to current page.}
10
+ gem.summary = %q{Minimal and configurable pagination templating, best suited for smaller screenspace. Provides buttons for first, previous, next, and last page.}
11
+ gem.homepage = 'http://github.com/lfender6445/mobile_pagination'
12
+ gem.license = "MIT"
13
+
14
+ gem.add_development_dependency "rake"
15
+ gem.add_development_dependency 'simplecov'
16
+ gem.add_development_dependency 'guard-rspec'
17
+ gem.add_development_dependency 'rack-test'
18
+ gem.add_development_dependency 'rspec'
19
+
20
+ gem.required_ruby_version = '>= 1.9.3'
21
+ gem.platform = Gem::Platform::RUBY
22
+ gem.date = Date.today.to_s
23
+ gem.files = `git ls-files`.split("\n")
24
+ gem.require_paths = ["lib"]
25
+ gem.version = MobilePagination::VERSION
26
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'simplecov'
3
+ require 'bundler/setup'
4
+ require 'rack/utils'
5
+
6
+ SimpleCov.start { add_filter '/spec/' }
7
+
8
+ require 'mobile_pagination'
9
+
10
+ spec_dir = File.dirname __FILE__
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe MobilePagination do
4
+
5
+ describe 'Configuration' do
6
+
7
+ it 'page_key config defaults to page' do
8
+ config = described_class.configuration
9
+ expect(config.page_key).to eq('page')
10
+ end
11
+
12
+
13
+ it 'sets configuration options' do
14
+ MobilePagination.configure { |config| config.page_key = 'slide' }
15
+ expect(described_class.configuration.page_key).to eq('slide')
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ module MobilePagination
4
+ describe Paginate do
5
+
6
+ describe 'attributes' do
7
+ before do
8
+ @klass = described_class.new({})
9
+ end
10
+
11
+ it 'should set defaults' do
12
+ expect(@klass.total_pages).to eq 0
13
+ expect(@klass.current_page).to eq 0
14
+ expect(@klass.query_params).to eq({})
15
+ expect(@klass.path).to eq "/"
16
+ end
17
+ end
18
+
19
+ describe 'readers' do
20
+ before do
21
+ opts = {
22
+ :total_pages => 2,
23
+ :current_page => 2,
24
+ :query_params => 'a=1&b=2',
25
+ :path => '/abc/'
26
+ }
27
+ @klass = described_class.new(opts)
28
+ end
29
+
30
+ it 'should assign properties' do
31
+ expect(@klass.total_pages).to eq 2
32
+ expect(@klass.current_page).to eq 2
33
+ expect(@klass.query_params).to eq({})
34
+ expect(@klass.path).to eq "/abc/"
35
+ end
36
+ end
37
+
38
+ describe 'class methods' do
39
+ before do
40
+ MobilePagination.configure { |c| c.page_key = 'slide' }
41
+ opts = {
42
+ :total_pages => 10,
43
+ :current_page => 5,
44
+ :query => 'slide=5&community=Providence',
45
+ :path => '/Listings/Georgia/Atlanta/'
46
+ }
47
+ @klass = described_class.new(opts)
48
+ end
49
+
50
+ describe '#first_page_link' do
51
+ it 'should generate url for first page' do
52
+ expect(@klass.first_page_link).to eq('/Listings/Georgia/Atlanta/?community=Providence')
53
+ end
54
+ end
55
+
56
+ describe '#previous_page_link' do
57
+ it 'should generate url for previous page' do
58
+ expect(@klass.previous_page_link).to eq('/Listings/Georgia/Atlanta/?slide=4&community=Providence')
59
+
60
+ end
61
+ end
62
+
63
+ describe '#next_page_link' do
64
+ it 'should generate url for next page' do
65
+ expect(@klass.next_page_link).to eq('/Listings/Georgia/Atlanta/?slide=6&community=Providence')
66
+ end
67
+ end
68
+
69
+ describe '#last_page_link' do
70
+ it 'should generate url for last page' do
71
+ expect(@klass.last_page_link).to eq('/Listings/Georgia/Atlanta/?slide=10&community=Providence')
72
+ end
73
+ end
74
+
75
+ describe 'Templates' do
76
+ it '#first_page_html should return string' do
77
+ @klass.first_page_html.should be_a_kind_of(String)
78
+ end
79
+ it '#previous_page_html should return string' do
80
+ @klass.previous_page_html.should be_kind_of(String)
81
+ end
82
+ it '#next_page_html should return string' do
83
+ @klass.next_page_html.should be_kind_of(String)
84
+ end
85
+ it '#last_page_html should return string' do
86
+ @klass.last_page_html.should be_kind_of(String)
87
+ end
88
+ it '#html return blank string unless total_pages' do
89
+ @klass.html.should be_kind_of(String)
90
+ end
91
+ end
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ module MobilePagination
4
+
5
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ module MobilePagination
4
+ describe 'Utils' do
5
+ include Utils
6
+
7
+ it '#query_to_hash should covert query to hash' do
8
+ expectation = query_to_hash('a=1&b=2')
9
+ expect(expectation).to eq({'a' => '1', 'b' => '2'})
10
+ end
11
+
12
+ it '#hash_to_query should covert hash to query' do
13
+ expectation = hash_to_query({ :a => 1, :b => 2})
14
+ expect(expectation).to eq('a=1&b=2')
15
+ end
16
+
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mobile_pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Luke Fender
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: simplecov
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: guard-rspec
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
+ - !ruby/object:Gem::Dependency
63
+ name: rack-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Creates a series of pagination elements relative to current page.
95
+ email:
96
+ - lfender6445@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - Guardfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/mobile_pagination.rb
108
+ - lib/mobile_pagination/configuration.rb
109
+ - lib/mobile_pagination/initialize.rb
110
+ - lib/mobile_pagination/paginate.rb
111
+ - lib/mobile_pagination/templates.rb
112
+ - lib/mobile_pagination/utils/initialize.rb
113
+ - lib/mobile_pagination/utils/utils.rb
114
+ - lib/mobile_pagination/utils/version.rb
115
+ - mobile_pagination.gemspec
116
+ - spec/spec_helper.rb
117
+ - spec/unit/mobile_pagination_spec.rb
118
+ - spec/unit/paginate_spec.rb
119
+ - spec/unit/templates_spec.rb
120
+ - spec/unit/utils_spec.rb
121
+ homepage: http://github.com/lfender6445/mobile_pagination
122
+ licenses:
123
+ - MIT
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 1.9.3
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.25
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Minimal and configurable pagination templating, best suited for smaller screenspace.
146
+ Provides buttons for first, previous, next, and last page.
147
+ test_files: []
148
+ has_rdoc: