neat-pages 0.1.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +2 -0
  5. data/LICENSE +1 -1
  6. data/README.md +58 -22
  7. data/app/assets/javascripts/neat_pages/base.coffee +8 -1
  8. data/lib/.reek +12 -0
  9. data/lib/neat-pages.rb +2 -1
  10. data/lib/neat_pages/base.rb +5 -7
  11. data/lib/neat_pages/engine.rb +3 -1
  12. data/lib/neat_pages/errors.rb +9 -0
  13. data/lib/neat_pages/helpers.rb +8 -10
  14. data/lib/neat_pages/helpers/builder.rb +26 -16
  15. data/lib/neat_pages/helpers/navigation.rb +39 -27
  16. data/lib/neat_pages/helpers/status.rb +14 -0
  17. data/lib/neat_pages/implants/action_controller_implant.rb +3 -0
  18. data/lib/neat_pages/implants/mongoid_implant.rb +12 -10
  19. data/lib/neat_pages/{implants/railtie.rb → railtie.rb} +7 -4
  20. data/neat-pages.gemspec +23 -0
  21. data/spec/controllers/application_controller_spec.rb +24 -0
  22. data/spec/dummy/Rakefile +6 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  24. data/spec/dummy/app/controllers/examples_controller.rb +11 -0
  25. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  26. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  27. data/spec/dummy/bin/bundle +3 -0
  28. data/spec/dummy/bin/rails +4 -0
  29. data/spec/dummy/bin/rake +4 -0
  30. data/spec/dummy/config.ru +4 -0
  31. data/spec/dummy/config/application.rb +18 -0
  32. data/spec/dummy/config/boot.rb +5 -0
  33. data/spec/dummy/config/database.yml +5 -0
  34. data/spec/dummy/config/environment.rb +5 -0
  35. data/spec/dummy/config/environments/development.rb +29 -0
  36. data/spec/dummy/config/environments/production.rb +80 -0
  37. data/spec/dummy/config/environments/test.rb +36 -0
  38. data/spec/dummy/config/initializers/standard_rails_initializers.rb +9 -0
  39. data/spec/dummy/config/locales/en.yml +5 -0
  40. data/spec/dummy/config/routes.rb +3 -0
  41. data/spec/dummy/db/test.sqlite3 +0 -0
  42. data/spec/neat_pages/base_spec.rb +31 -5
  43. data/spec/neat_pages/helpers/builder_spec.rb +109 -0
  44. data/spec/neat_pages/helpers/navigation_spec.rb +105 -0
  45. data/spec/neat_pages/helpers/status_spec.rb +65 -0
  46. data/spec/neat_pages/helpers_spec.rb +67 -0
  47. data/spec/neat_pages/implants/mongoid_implant_spec.rb +43 -0
  48. data/spec/spec_helper.rb +21 -6
  49. data/spec/support/views_helper.rb +16 -0
  50. data/vendor/assets/javascripts/jquery.hashchange.min.js +1 -1
  51. metadata +134 -42
  52. data/lib/neat_pages/implants.rb +0 -12
@@ -1,4 +1,16 @@
1
+ #*************************************************************************************
2
+ # Status take care of generating the html code that show the results position and the
3
+ # total results.
4
+ #
5
+ # Output Example
6
+ #
7
+ # 30 to 40 / 300
8
+ #*************************************************************************************
1
9
  class NeatPages::Helpers::Status < NeatPages::Helpers::Builder
10
+ delegate :empty?, to: :pagination
11
+ delegate :offset, to: :pagination
12
+ delegate :out_of_bound?, to: :pagination
13
+
2
14
  def generate
3
15
  return '' if empty? or out_of_bound?
4
16
 
@@ -7,6 +19,8 @@ class NeatPages::Helpers::Status < NeatPages::Helpers::Builder
7
19
  return build_status from, to
8
20
  end
9
21
 
22
+ private
23
+
10
24
  def build_status(from, to)
11
25
  reset_builder
12
26
 
@@ -1,3 +1,6 @@
1
+ #*************************************************************************************
2
+ # Insert methods in the Action Controller of a Rails project.
3
+ #*************************************************************************************
1
4
  module NeatPages::Implants::ActionControllerImplant
2
5
  extend ActiveSupport::Concern
3
6
 
@@ -1,15 +1,17 @@
1
- module NeatPages::Implants
2
- module MongoidCriteriaImplant
3
- def paginate(current)
4
- if current
5
- current.set_total_items self.count
1
+ #*************************************************************************************
2
+ # Insert the method paginate in a Mongoid model. This method will be use the limit
3
+ # the number of results returned by a query.
4
+ #*************************************************************************************
5
+ module NeatPages::Implants::MongoidCriteriaImplant
6
+ def paginate(current)
7
+ if current
8
+ current.set_total_items self.count
6
9
 
7
- raise NeatPages::OutOfBound if current.out_of_bound?
10
+ raise NeatPages::OutOfBound if current.out_of_bound?
8
11
 
9
- return self.offset(current.offset).limit(current.limit)
10
- else
11
- raise 'You need to initialize the pagination'
12
- end
12
+ return self.offset(current.offset).limit(current.limit)
13
+ else
14
+ raise NeatPages::Uninitalized, 'You need to initialize the pagination'
13
15
  end
14
16
  end
15
17
  end
@@ -1,6 +1,4 @@
1
- require 'neat_pages/implants/action_controller_implant'
2
-
3
- module NeatPages::Implants
1
+ module NeatPages
4
2
  class Railtie < Rails::Railtie
5
3
  initializer "neat-pages" do |app|
6
4
  ActiveSupport.on_load :action_controller do
@@ -14,9 +12,14 @@ module NeatPages::Implants
14
12
  Mime::Type.register "text/html", :neatpage if not Mime::Type.lookup_by_extension :neatpage
15
13
  end
16
14
  end
15
+
16
+ module Implants
17
+ end
17
18
  end
18
19
 
19
20
  dir = File.expand_path(File.dirname(__FILE__))
20
21
 
21
- I18n.load_path << File.join(dir, '../../../config/locales', 'fr.yml')
22
+ I18n.load_path << File.join(dir, '../../config/locales', 'fr.yml')
22
23
 
24
+ require 'neat_pages/implants/action_controller_implant'
25
+ require 'neat_pages/implants/mongoid_implant'
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "neat-pages"
3
+ gem.description = "A simple pagination API to paginate Mongoid Models."
4
+ gem.summary = "A simple pagination API to paginate Mongoid Models."
5
+ gem.homepage = "https://github.com/demarque/neat-pages"
6
+ gem.version = "1.0.0"
7
+ gem.licenses = ["MIT"]
8
+
9
+ gem.authors = ["Sebastien Rosa"]
10
+ gem.email = ["srosa@alchimik.com"]
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.require_paths = ["lib"]
16
+
17
+ gem.add_dependency "rails", ['>= 4.0']
18
+ gem.add_development_dependency "sqlite3"
19
+ gem.add_development_dependency "rspec-rails"
20
+ gem.add_development_dependency "simplecov"
21
+ gem.add_development_dependency "simplecov-rcov-text"
22
+ gem.add_development_dependency "coveralls"
23
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe ApplicationController do
4
+ controller do
5
+ def index
6
+ paginate per_page: 20
7
+
8
+ set_pagination_header
9
+
10
+ raise NeatPages::OutOfBound
11
+ end
12
+ end
13
+
14
+ describe "#index" do
15
+ it 'is out of bound' do
16
+ get :index
17
+
18
+ expect(response.status).to eq(404)
19
+ expect(response.body).to eq "out_of_bound"
20
+ controller.pagination.per_page.should eql 20
21
+ response.headers["X-Per-Page"].should eql "20"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,11 @@
1
+ class ExamplesController < ApplicationController
2
+ protect_from_forgery
3
+
4
+ def index
5
+ render text: 'dummy'
6
+ end
7
+
8
+ def show
9
+ render text: 'dummy'
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "sprockets/railtie"
8
+ # require "rails/test_unit/railtie"
9
+
10
+ Bundler.require(*Rails.groups)
11
+
12
+ require "neat-pages"
13
+
14
+ module Dummy
15
+ class Application < Rails::Application
16
+ end
17
+ end
18
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,5 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/test.sqlite3
4
+ pool: 5
5
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application.
5
+ Dummy::Application.initialize!
@@ -0,0 +1,29 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Do not eager load code on boot.
10
+ config.eager_load = false
11
+
12
+ # Show full error reports and disable caching.
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send.
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger.
20
+ config.active_support.deprecation = :log
21
+
22
+ # Raise an error on page load if there are pending migrations
23
+ config.active_record.migration_error = :page_load
24
+
25
+ # Debug mode disables concatenation and preprocessing of assets.
26
+ # This option may cause significant delays in view rendering with a large
27
+ # number of complex assets.
28
+ config.assets.debug = true
29
+ end
@@ -0,0 +1,80 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # Code is not reloaded between requests.
5
+ config.cache_classes = true
6
+
7
+ # Eager load code on boot. This eager loads most of Rails and
8
+ # your application in memory, allowing both thread web servers
9
+ # and those relying on copy on write to perform better.
10
+ # Rake tasks automatically ignore this option for performance.
11
+ config.eager_load = true
12
+
13
+ # Full error reports are disabled and caching is turned on.
14
+ config.consider_all_requests_local = false
15
+ config.action_controller.perform_caching = true
16
+
17
+ # Enable Rack::Cache to put a simple HTTP cache in front of your application
18
+ # Add `rack-cache` to your Gemfile before enabling this.
19
+ # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
20
+ # config.action_dispatch.rack_cache = true
21
+
22
+ # Disable Rails's static asset server (Apache or nginx will already do this).
23
+ config.serve_static_assets = false
24
+
25
+ # Compress JavaScripts and CSS.
26
+ config.assets.js_compressor = :uglifier
27
+ # config.assets.css_compressor = :sass
28
+
29
+ # Do not fallback to assets pipeline if a precompiled asset is missed.
30
+ config.assets.compile = false
31
+
32
+ # Generate digests for assets URLs.
33
+ config.assets.digest = true
34
+
35
+ # Version of your assets, change this if you want to expire all your assets.
36
+ config.assets.version = '1.0'
37
+
38
+ # Specifies the header that your server uses for sending files.
39
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
40
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
41
+
42
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
43
+ # config.force_ssl = true
44
+
45
+ # Set to :debug to see everything in the log.
46
+ config.log_level = :info
47
+
48
+ # Prepend all log lines with the following tags.
49
+ # config.log_tags = [ :subdomain, :uuid ]
50
+
51
+ # Use a different logger for distributed setups.
52
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
53
+
54
+ # Use a different cache store in production.
55
+ # config.cache_store = :mem_cache_store
56
+
57
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
58
+ # config.action_controller.asset_host = "http://assets.example.com"
59
+
60
+ # Precompile additional assets.
61
+ # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
62
+ # config.assets.precompile += %w( search.js )
63
+
64
+ # Ignore bad email addresses and do not raise email delivery errors.
65
+ # Set this to true and configure the email server for immediate delivery to raise delivery errors.
66
+ # config.action_mailer.raise_delivery_errors = false
67
+
68
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
69
+ # the I18n.default_locale when a translation can not be found).
70
+ config.i18n.fallbacks = true
71
+
72
+ # Send deprecation notices to registered listeners.
73
+ config.active_support.deprecation = :notify
74
+
75
+ # Disable automatic flushing of the log to improve performance.
76
+ # config.autoflush_log = false
77
+
78
+ # Use default logging formatter so that PID and timestamp are not suppressed.
79
+ config.log_formatter = ::Logger::Formatter.new
80
+ end
@@ -0,0 +1,36 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb.
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Do not eager load code on boot. This avoids loading your whole application
11
+ # just for the purpose of running a single test. If you are using a tool that
12
+ # preloads Rails for running tests, you may have to set it to true.
13
+ config.eager_load = false
14
+
15
+ # Configure static asset server for tests with Cache-Control for performance.
16
+ config.serve_static_assets = true
17
+ config.static_cache_control = "public, max-age=3600"
18
+
19
+ # Show full error reports and disable caching.
20
+ config.consider_all_requests_local = true
21
+ config.action_controller.perform_caching = false
22
+
23
+ # Raise exceptions instead of rendering exception templates.
24
+ config.action_dispatch.show_exceptions = false
25
+
26
+ # Disable request forgery protection in test environment.
27
+ config.action_controller.allow_forgery_protection = false
28
+
29
+ # Tell Action Mailer not to deliver emails to the real world.
30
+ # The :test delivery method accumulates sent emails in the
31
+ # ActionMailer::Base.deliveries array.
32
+ config.action_mailer.delivery_method = :test
33
+
34
+ # Print deprecation notices to the stderr.
35
+ config.active_support.deprecation = :stderr
36
+ end
@@ -0,0 +1,9 @@
1
+ Rails.application.config.filter_parameters += [:password]
2
+
3
+ Dummy::Application.config.secret_key_base = 'ae3cad0b2a7fe9cf1620b58b362sdfsdf9d5236ba303b9821fb22ac023c9dc1b07ef16f27b74e96c4294f1e02c5bfc83f2dc89c02c9fc24d1b3974f6d7c14701'
4
+
5
+ Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
6
+
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
+ end
@@ -0,0 +1,5 @@
1
+ en:
2
+ neat_pages:
3
+ next_page: Next
4
+ previous_page: Previous
5
+ to: to
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ resources :examples, only: [:index, :show]
3
+ end
File without changes
@@ -1,9 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe NeatPages::Base do
4
- #*************************************************************************************
5
- # PUBLIC INSTANCE METHODS
6
- #*************************************************************************************
7
4
  describe "#empty?" do
8
5
  context "with a 20 items pagination" do
9
6
  specify { NeatPages::Base.new(0, total_items: 20).should_not be_empty }
@@ -14,6 +11,12 @@ describe NeatPages::Base do
14
11
  end
15
12
  end
16
13
 
14
+ describe "#limit" do
15
+ context "with a 100 items pagination starting at 0 and having 10 items per page" do
16
+ specify { NeatPages::Base.new(0, per_page: 10, total_items: 100).limit.should eql 10 }
17
+ end
18
+ end
19
+
17
20
  describe "#next_page" do
18
21
  context "with a 100 items pagination starting at 0 and having 10 items per page" do
19
22
  specify { NeatPages::Base.new(0, per_page: 10, total_items: 100).next_page.should eql 2 }
@@ -62,6 +65,16 @@ describe NeatPages::Base do
62
65
  end
63
66
  end
64
67
 
68
+ describe "#paginated?" do
69
+ context "with a 20 items pagination starting at page 1 and having 10 items per page" do
70
+ specify { NeatPages::Base.new(1, per_page: 10, total_items: 20).should be_paginated }
71
+ end
72
+
73
+ context "with a 5 items pagination starting at page 1 and having 10 items per page" do
74
+ specify { NeatPages::Base.new(1, per_page: 10, total_items: 5).should_not be_paginated }
75
+ end
76
+ end
77
+
65
78
  describe "#previous_page" do
66
79
  context "with a 100 items pagination starting at page 5 and having 10 items per page" do
67
80
  specify { NeatPages::Base.new(5, per_page: 10, total_items: 100).previous_page.should eql 4 }
@@ -95,7 +108,7 @@ describe NeatPages::Base do
95
108
  specify { NeatPages::Base.new(2, per_page: 10, total_items: 100).should be_previous }
96
109
  end
97
110
 
98
- context "with a 5 items pagination starting at 0 and having 10 items per page" do
111
+ context "with a 5 items pagination starting at page 0 and having 10 items per page" do
99
112
  specify { NeatPages::Base.new(0, per_page: 10, total_items: 5).should_not be_previous }
100
113
  end
101
114
  end
@@ -107,11 +120,24 @@ describe NeatPages::Base do
107
120
  its(:length) { should eql 4 }
108
121
  its(['X-Total-Items']) { should eql '200' }
109
122
  its(['X-Total-Pages']) { should eql '20' }
110
- its(['X-Per-Page']) { should eql '10' }#
123
+ its(['X-Per-Page']) { should eql '10' }
111
124
  its(['X-Current-Page']) { should eql '1' }
112
125
  end
113
126
  end
114
127
 
128
+ describe "#set_total_items" do
129
+ context "with a 100 items pagination starting at page 2 and having 10 items per page" do
130
+ let(:pagination) { NeatPages::Base.new(2, per_page: 10, total_items: 100) }
131
+
132
+ context "when changing the total items to 5" do
133
+ before { pagination.set_total_items 5 }
134
+
135
+ specify { pagination.should be_out_of_bound }
136
+ specify { pagination.total_items.should eql 5 }
137
+ end
138
+ end
139
+ end
140
+
115
141
  describe "#total_pages" do
116
142
  context "with a 100 items pagination and having 10 items per page" do
117
143
  specify { NeatPages::Base.new(0, per_page: 10, total_items: 100).total_pages.should eql 10 }