paginary 0.0.1.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +16 -0
  3. data/Gemfile.lock +93 -0
  4. data/LICENSE +19 -0
  5. data/README.md +7 -0
  6. data/Rakefile +37 -0
  7. data/VERSION +1 -0
  8. data/lib/paginary.rb +7 -0
  9. data/lib/paginary/helpers/page_builder.rb +57 -0
  10. data/lib/paginary/helpers/pagination_helper.rb +9 -0
  11. data/lib/paginary/railtie.rb +16 -0
  12. data/lib/paginary/relation/paginated.rb +43 -0
  13. data/lib/paginary/relation/query_methods.rb +15 -0
  14. data/paginary.gemspec +96 -0
  15. data/test/dummy/app/controllers/application_controller.rb +3 -0
  16. data/test/dummy/app/controllers/widgets_controller.rb +83 -0
  17. data/test/dummy/app/models/widget.rb +2 -0
  18. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  19. data/test/dummy/app/views/widgets/_form.html.erb +17 -0
  20. data/test/dummy/app/views/widgets/edit.html.erb +6 -0
  21. data/test/dummy/app/views/widgets/index.html.erb +21 -0
  22. data/test/dummy/app/views/widgets/new.html.erb +5 -0
  23. data/test/dummy/app/views/widgets/show.html.erb +5 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +42 -0
  26. data/test/dummy/config/boot.rb +13 -0
  27. data/test/dummy/config/database.yml +3 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/test.rb +35 -0
  30. data/test/dummy/config/routes.rb +60 -0
  31. data/test/dummy/db/schema.rb +8 -0
  32. data/test/test_helper.rb +39 -0
  33. data/test/unit/helpers/page_builder_test.rb +68 -0
  34. data/test/unit/helpers/pagination_helper_test.rb +30 -0
  35. data/test/unit/relation/paginated_test.rb +74 -0
  36. data/test/unit/relation/query_methods_test.rb +42 -0
  37. metadata +141 -0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ *.sqlite3
3
+ *.log
4
+ test/dummy/tmp/**/*
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "paginary", :path => File.dirname(__FILE__)
4
+ gem "rails", "3.0.1"
5
+ gem "rake"
6
+ gem "jeweler"
7
+
8
+ platforms :ruby do
9
+ gem "sqlite3-ruby"
10
+ end
11
+
12
+ platforms :jruby do
13
+ gem "jdbc-sqlite3", :require => "jdbc/sqlite3"
14
+ gem "activerecord-jdbc-adapter", "1.0.0.beta2"
15
+ gem "jruby-openssl", :require => false # Silence openssl warnings.
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,93 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paginary (0.0.1.pre1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionmailer (3.0.1)
11
+ actionpack (= 3.0.1)
12
+ mail (~> 2.2.5)
13
+ actionpack (3.0.1)
14
+ activemodel (= 3.0.1)
15
+ activesupport (= 3.0.1)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4.1)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.12)
21
+ rack-test (~> 0.5.4)
22
+ tzinfo (~> 0.3.23)
23
+ activemodel (3.0.1)
24
+ activesupport (= 3.0.1)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4.1)
27
+ activerecord (3.0.1)
28
+ activemodel (= 3.0.1)
29
+ activesupport (= 3.0.1)
30
+ arel (~> 1.0.0)
31
+ tzinfo (~> 0.3.23)
32
+ activeresource (3.0.1)
33
+ activemodel (= 3.0.1)
34
+ activesupport (= 3.0.1)
35
+ activesupport (3.0.1)
36
+ arel (1.0.1)
37
+ activesupport (~> 3.0.0)
38
+ builder (2.1.2)
39
+ erubis (2.6.6)
40
+ abstract (>= 1.0.0)
41
+ gemcutter (0.6.1)
42
+ git (1.2.5)
43
+ i18n (0.4.1)
44
+ jeweler (1.4.0)
45
+ gemcutter (>= 0.1.0)
46
+ git (>= 1.2.5)
47
+ rubyforge (>= 2.0.0)
48
+ json_pure (1.4.6)
49
+ mail (2.2.7)
50
+ activesupport (>= 2.3.6)
51
+ mime-types
52
+ treetop (>= 1.4.5)
53
+ mime-types (1.16)
54
+ polyglot (0.3.1)
55
+ rack (1.2.1)
56
+ rack-mount (0.6.13)
57
+ rack (>= 1.0.0)
58
+ rack-test (0.5.6)
59
+ rack (>= 1.0)
60
+ rails (3.0.1)
61
+ actionmailer (= 3.0.1)
62
+ actionpack (= 3.0.1)
63
+ activerecord (= 3.0.1)
64
+ activeresource (= 3.0.1)
65
+ activesupport (= 3.0.1)
66
+ bundler (~> 1.0.0)
67
+ railties (= 3.0.1)
68
+ railties (3.0.1)
69
+ actionpack (= 3.0.1)
70
+ activesupport (= 3.0.1)
71
+ rake (>= 0.8.4)
72
+ thor (~> 0.14.0)
73
+ rake (0.8.7)
74
+ rubyforge (2.0.4)
75
+ json_pure (>= 1.1.7)
76
+ sqlite3-ruby (1.3.1)
77
+ thor (0.14.3)
78
+ treetop (1.4.8)
79
+ polyglot (>= 0.3.1)
80
+ tzinfo (0.3.23)
81
+
82
+ PLATFORMS
83
+ ruby
84
+
85
+ DEPENDENCIES
86
+ activerecord-jdbc-adapter (= 1.0.0.beta2)
87
+ jdbc-sqlite3
88
+ jeweler
89
+ jruby-openssl
90
+ paginary!
91
+ rails (= 3.0.1)
92
+ rake
93
+ sqlite3-ruby
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Voormedia B.V.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ Paginary – View-based pagination for Rails
2
+ ==========================================
3
+
4
+ Paginary is a simple plugin for Rails 3 that allows you to paginate in your
5
+ views only. No need to touch your models or controllers.
6
+
7
+ Paginary is currently in alpha status.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require "jeweler"
3
+ require "rake/testtask"
4
+
5
+ Jeweler::Tasks.new do |spec|
6
+ spec.name = "paginary"
7
+ spec.rubyforge_project = "paginary"
8
+ spec.summary = "View-based pagination for Rails."
9
+ spec.description = "Simple, view-based pagination for Rails, built on top of Active Record 3 awesomeness."
10
+
11
+ spec.authors = ["Rolf Timmermans"]
12
+ spec.email = "r.timmermans@voormedia.com"
13
+ spec.homepage = "http://rails-erd.rubyforge.org/"
14
+
15
+ spec.add_development_dependency "rails"
16
+ spec.add_development_dependency "sqlite3-ruby"
17
+ end
18
+
19
+ Jeweler::GemcutterTasks.new
20
+
21
+ Jeweler::RubyforgeTasks.new do |rubyforge|
22
+ rubyforge.doc_task = "rdoc"
23
+ rubyforge.remote_doc_path = "doc"
24
+ end
25
+
26
+ Rake::TestTask.new do |test|
27
+ test.pattern = "test/**/*_test.rb"
28
+ end
29
+
30
+ task :default => :test
31
+
32
+ begin
33
+ require "hanna/rdoctask"
34
+ Rake::RDocTask.new do |rdoc|
35
+ end
36
+ rescue LoadError
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1.pre1
data/lib/paginary.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "paginary/helpers/page_builder"
2
+ require "paginary/helpers/pagination_helper"
3
+
4
+ require "paginary/relation/paginated"
5
+ require "paginary/relation/query_methods"
6
+
7
+ require "paginary/railtie"
@@ -0,0 +1,57 @@
1
+ module Paginary
2
+ module Helpers
3
+ class PageBuilder
4
+ attr_reader :template, :relation
5
+ alias_method :items, :relation
6
+
7
+ delegate :content_tag, :link_to, :params, :translate, :to => :template
8
+
9
+ def initialize(template, relation, options = {})
10
+ @template = template
11
+ @param_name = options.delete(:param) || :page
12
+ @relation = relation.paginated? ? relation : relation.paginate(params[@param_name], options)
13
+ end
14
+
15
+ def page_url(page)
16
+ template.url_for params.merge(@param_name => page)
17
+ end
18
+
19
+ def previous_url
20
+ page_url @relation.current_page - 1
21
+ end
22
+
23
+ def next_url
24
+ page_url @relation.current_page + 1
25
+ end
26
+
27
+ def links
28
+ return unless @relation.paginated?
29
+ content_tag :div, previous_link + page_links + next_link, :class => "pagination"
30
+ end
31
+
32
+ def page_links
33
+ (1..@relation.page_count).collect do |page|
34
+ link_to content_tag(:span, page), page_url(page), :class => "page#{@relation.current_page == page ? " selected" : ""}"
35
+ end.inject(:+)
36
+ end
37
+
38
+ def previous_link
39
+ text = content_tag(:span, translate("previous", :default => "< Previous"))
40
+ unless @relation.first_page?
41
+ link_to text, previous_url, :class => "previous"
42
+ else
43
+ content_tag :span, text, :class => "previous disabled"
44
+ end
45
+ end
46
+
47
+ def next_link
48
+ text = content_tag(:span, translate("previous", :default => "Next >"))
49
+ unless @relation.last_page?
50
+ link_to text, next_url, :class => "next"
51
+ else
52
+ content_tag :span, text, :class => "next disabled"
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,9 @@
1
+ module Paginary
2
+ module Helpers
3
+ module PaginationHelper
4
+ def paginate(relation, *args, &block)
5
+ capture(PageBuilder.new(self, relation, *args), &block)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Paginary
2
+ class Railtie < Rails::Railtie
3
+ initializer "paginary.query_methods" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ ActiveRecord::Relation.send :include, Paginary::Relation::QueryMethods
6
+ ActiveRecord::Base.singleton_class.delegate :paginate, :paginated?, :to => :scoped
7
+ end
8
+ end
9
+
10
+ initializer "paginary.pagination_helper" do
11
+ ActiveSupport.on_load(:action_view) do
12
+ ActionView::Base.send :include, Paginary::Helpers::PaginationHelper
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ require "active_support/core_ext/array/extract_options"
2
+
3
+ module Paginary
4
+ module Relation
5
+ module Paginated
6
+ attr_accessor :items_per_page, :current_page
7
+
8
+ def paginate!(*args)
9
+ options = args.extract_options!
10
+ self.items_per_page = options[:per_page] || 50
11
+ self.current_page = args.first ? args.first.to_i : 1
12
+ self.limit_value = items_per_page
13
+ self.offset_value = items_per_page * (current_page - 1)
14
+ end
15
+
16
+ def current_page=(page)
17
+ raise ActiveRecord::RecordNotFound, "page #{page} out of bounds, expected 1..#{page_count}" unless page.between?(1, page_count)
18
+ @current_page = page
19
+ end
20
+
21
+ def paginated?
22
+ page_count > 1
23
+ end
24
+
25
+ def page_count
26
+ # Integer arithmetic is simpler and faster.
27
+ @page_count ||= item_count > 0 ? (item_count - 1) / items_per_page + 1 : 1
28
+ end
29
+
30
+ def item_count
31
+ @item_count ||= except(:includes, :limit, :offset).count
32
+ end
33
+
34
+ def first_page?
35
+ current_page == 1
36
+ end
37
+
38
+ def last_page?
39
+ current_page == page_count
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ module Paginary
2
+ module Relation
3
+ module QueryMethods
4
+ def paginate(*args)
5
+ clone.extending(Paginated).tap do |relation|
6
+ relation.paginate!(*args)
7
+ end
8
+ end
9
+
10
+ def paginated?
11
+ false
12
+ end
13
+ end
14
+ end
15
+ end
data/paginary.gemspec ADDED
@@ -0,0 +1,96 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{paginary}
8
+ s.version = "0.0.1.pre1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Rolf Timmermans"]
12
+ s.date = %q{2010-10-20}
13
+ s.description = %q{Simple, view-based pagination for Rails, built on top of Active Record 3 awesomeness.}
14
+ s.email = %q{r.timmermans@voormedia.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/paginary.rb",
28
+ "lib/paginary/helpers/page_builder.rb",
29
+ "lib/paginary/helpers/pagination_helper.rb",
30
+ "lib/paginary/railtie.rb",
31
+ "lib/paginary/relation/paginated.rb",
32
+ "lib/paginary/relation/query_methods.rb",
33
+ "paginary.gemspec",
34
+ "test/dummy/app/controllers/application_controller.rb",
35
+ "test/dummy/app/controllers/widgets_controller.rb",
36
+ "test/dummy/app/models/widget.rb",
37
+ "test/dummy/app/views/layouts/application.html.erb",
38
+ "test/dummy/app/views/widgets/_form.html.erb",
39
+ "test/dummy/app/views/widgets/edit.html.erb",
40
+ "test/dummy/app/views/widgets/index.html.erb",
41
+ "test/dummy/app/views/widgets/new.html.erb",
42
+ "test/dummy/app/views/widgets/show.html.erb",
43
+ "test/dummy/config.ru",
44
+ "test/dummy/config/application.rb",
45
+ "test/dummy/config/boot.rb",
46
+ "test/dummy/config/database.yml",
47
+ "test/dummy/config/environment.rb",
48
+ "test/dummy/config/environments/test.rb",
49
+ "test/dummy/config/routes.rb",
50
+ "test/dummy/db/schema.rb",
51
+ "test/test_helper.rb",
52
+ "test/unit/helpers/page_builder_test.rb",
53
+ "test/unit/helpers/pagination_helper_test.rb",
54
+ "test/unit/relation/paginated_test.rb",
55
+ "test/unit/relation/query_methods_test.rb"
56
+ ]
57
+ s.homepage = %q{http://rails-erd.rubyforge.org/}
58
+ s.rdoc_options = ["--charset=UTF-8"]
59
+ s.require_paths = ["lib"]
60
+ s.rubyforge_project = %q{paginary}
61
+ s.rubygems_version = %q{1.3.7}
62
+ s.summary = %q{View-based pagination for Rails.}
63
+ s.test_files = [
64
+ "test/dummy/app/controllers/application_controller.rb",
65
+ "test/dummy/app/controllers/widgets_controller.rb",
66
+ "test/dummy/app/models/widget.rb",
67
+ "test/dummy/config/application.rb",
68
+ "test/dummy/config/boot.rb",
69
+ "test/dummy/config/environment.rb",
70
+ "test/dummy/config/environments/test.rb",
71
+ "test/dummy/config/routes.rb",
72
+ "test/dummy/db/schema.rb",
73
+ "test/test_helper.rb",
74
+ "test/unit/helpers/page_builder_test.rb",
75
+ "test/unit/helpers/pagination_helper_test.rb",
76
+ "test/unit/relation/paginated_test.rb",
77
+ "test/unit/relation/query_methods_test.rb"
78
+ ]
79
+
80
+ if s.respond_to? :specification_version then
81
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
82
+ s.specification_version = 3
83
+
84
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
85
+ s.add_development_dependency(%q<rails>, [">= 0"])
86
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
87
+ else
88
+ s.add_dependency(%q<rails>, [">= 0"])
89
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
90
+ end
91
+ else
92
+ s.add_dependency(%q<rails>, [">= 0"])
93
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
94
+ end
95
+ end
96
+
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,83 @@
1
+ class WidgetsController < ApplicationController
2
+ # GET /widgets
3
+ # GET /widgets.xml
4
+ def index
5
+ @widgets = Widget.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.xml { render :xml => @widgets }
10
+ end
11
+ end
12
+
13
+ # GET /widgets/1
14
+ # GET /widgets/1.xml
15
+ def show
16
+ @widget = Widget.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.xml { render :xml => @widget }
21
+ end
22
+ end
23
+
24
+ # GET /widgets/new
25
+ # GET /widgets/new.xml
26
+ def new
27
+ @widget = Widget.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.xml { render :xml => @widget }
32
+ end
33
+ end
34
+
35
+ # GET /widgets/1/edit
36
+ def edit
37
+ @widget = Widget.find(params[:id])
38
+ end
39
+
40
+ # POST /widgets
41
+ # POST /widgets.xml
42
+ def create
43
+ @widget = Widget.new(params[:widget])
44
+
45
+ respond_to do |format|
46
+ if @widget.save
47
+ format.html { redirect_to(@widget, :notice => 'Widget was successfully created.') }
48
+ format.xml { render :xml => @widget, :status => :created, :location => @widget }
49
+ else
50
+ format.html { render :action => "new" }
51
+ format.xml { render :xml => @widget.errors, :status => :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /widgets/1
57
+ # PUT /widgets/1.xml
58
+ def update
59
+ @widget = Widget.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @widget.update_attributes(params[:widget])
63
+ format.html { redirect_to(@widget, :notice => 'Widget was successfully updated.') }
64
+ format.xml { head :ok }
65
+ else
66
+ format.html { render :action => "edit" }
67
+ format.xml { render :xml => @widget.errors, :status => :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /widgets/1
73
+ # DELETE /widgets/1.xml
74
+ def destroy
75
+ @widget = Widget.find(params[:id])
76
+ @widget.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to(widgets_url) }
80
+ format.xml { head :ok }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,2 @@
1
+ class Widget < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,17 @@
1
+ <%= form_for(@widget) do |f| %>
2
+ <% if @widget.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@widget.errors.count, "error") %> prohibited this widget from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @widget.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="actions">
15
+ <%= f.submit %>
16
+ </div>
17
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing widget</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @widget %> |
6
+ <%= link_to 'Back', widgets_path %>
@@ -0,0 +1,21 @@
1
+ <h1>Listing widgets</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th></th>
6
+ <th></th>
7
+ <th></th>
8
+ </tr>
9
+
10
+ <% @widgets.each do |widget| %>
11
+ <tr>
12
+ <td><%= link_to 'Show', widget %></td>
13
+ <td><%= link_to 'Edit', edit_widget_path(widget) %></td>
14
+ <td><%= link_to 'Destroy', widget, :confirm => 'Are you sure?', :method => :delete %></td>
15
+ </tr>
16
+ <% end %>
17
+ </table>
18
+
19
+ <br />
20
+
21
+ <%= link_to 'New Widget', new_widget_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New widget</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', widgets_path %>
@@ -0,0 +1,5 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+
4
+ <%= link_to 'Edit', edit_widget_path(@widget) %> |
5
+ <%= link_to 'Back', widgets_path %>
@@ -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 Dummy::Application
@@ -0,0 +1,42 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # If you have a Gemfile, require the gems listed there, including any gems
6
+ # you've limited to :test, :development, or :production.
7
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
8
+
9
+ module Dummy
10
+ class Application < Rails::Application
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Custom directories with classes and modules you want to be autoloadable.
16
+ # config.autoload_paths += %W(#{config.root}/extras)
17
+
18
+ # Only load the plugins named here, in the order given (default is alphabetical).
19
+ # :all can be used as a placeholder for all plugins not explicitly named.
20
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
21
+
22
+ # Activate observers that should always be running.
23
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
24
+
25
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
26
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
27
+ # config.time_zone = 'Central Time (US & Canada)'
28
+
29
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
30
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
31
+ # config.i18n.default_locale = :de
32
+
33
+ # JavaScript files you want as :defaults (application.js is always included).
34
+ config.action_view.javascript_expansions[:defaults] = %w()
35
+
36
+ # Configure the default encoding used in templates for Ruby 1.9.
37
+ config.encoding = "utf-8"
38
+
39
+ # Configure sensitive parameters which will be filtered from the log file.
40
+ config.filter_parameters += [:password]
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
5
+ begin
6
+ ENV['BUNDLE_GEMFILE'] = gemfile
7
+ require 'bundler'
8
+ Bundler.setup
9
+ rescue Bundler::GemNotFound => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Try running `bundle install`."
12
+ exit!
13
+ end if File.exist?(gemfile)
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -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,35 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.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
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Tell Action Mailer not to deliver emails to the real world.
24
+ # The :test delivery method accumulates sent emails in the
25
+ # ActionMailer::Base.deliveries array.
26
+ config.action_mailer.delivery_method = :test
27
+
28
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
29
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
30
+ # like if you have constraints or database-specific column types
31
+ # config.active_record.schema_format = :sql
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,60 @@
1
+ Dummy::Application.routes.draw do
2
+ resources :widgets
3
+
4
+ # The priority is based upon order of creation:
5
+ # first created -> highest priority.
6
+
7
+ # Sample of regular route:
8
+ # match 'products/:id' => 'catalog#view'
9
+ # Keep in mind you can assign values other than :controller and :action
10
+
11
+ # Sample of named route:
12
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
13
+ # This route can be invoked with purchase_url(:id => product.id)
14
+
15
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
16
+ # resources :products
17
+
18
+ # Sample resource route with options:
19
+ # resources :products do
20
+ # member do
21
+ # get 'short'
22
+ # post 'toggle'
23
+ # end
24
+ #
25
+ # collection do
26
+ # get 'sold'
27
+ # end
28
+ # end
29
+
30
+ # Sample resource route with sub-resources:
31
+ # resources :products do
32
+ # resources :comments, :sales
33
+ # resource :seller
34
+ # end
35
+
36
+ # Sample resource route with more complex sub-resources
37
+ # resources :products do
38
+ # resources :comments
39
+ # resources :sales do
40
+ # get 'recent', :on => :collection
41
+ # end
42
+ # end
43
+
44
+ # Sample resource route within a namespace:
45
+ # namespace :admin do
46
+ # # Directs /admin/products/* to Admin::ProductsController
47
+ # # (app/controllers/admin/products_controller.rb)
48
+ # resources :products
49
+ # end
50
+
51
+ # You can have the root of your site routed with "root"
52
+ # just remember to delete public/index.html.
53
+ # root :to => "welcome#index"
54
+
55
+ # See how all your routes lay out with "rake routes"
56
+
57
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
58
+ # Note: This route will make all actions in every controller accessible via GET requests.
59
+ # match ':controller(/:action(/:id(.:format)))'
60
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.suppress_messages do
2
+ ActiveRecord::Schema.define do
3
+ create_table :widgets do |t|
4
+ t.boolean :deleted, :default => false
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,39 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ ENV["RAILS_ENV"] = "test"
5
+ require File.expand_path("dummy/config/environment", File.dirname(__FILE__))
6
+ require File.expand_path("dummy/db/schema", File.dirname(__FILE__))
7
+ require "test/unit"
8
+
9
+ ActiveRecord::Base.connection.class.class_eval do
10
+ def execute_with_query_record(sql, name = nil, &block)
11
+ ($queries_executed ||= []) << sql
12
+ execute_without_query_record(sql, name, &block)
13
+ end
14
+ alias_method_chain :execute, :query_record
15
+ end
16
+
17
+ class ActiveSupport::TestCase
18
+ setup :create_widgets
19
+ teardown :destroy_widgets
20
+
21
+ def count_queries
22
+ $queries_executed = []
23
+ yield
24
+ $queries_executed.size
25
+ end
26
+
27
+ def assert_equal_relation(expected, actual)
28
+ assert_kind_of ActiveRecord::Relation, actual
29
+ assert_equal expected.to_a, actual.to_a
30
+ end
31
+
32
+ def create_widgets
33
+ 177.times { Widget.connection.insert "INSERT INTO widgets (deleted) VALUES (0)" }
34
+ end
35
+
36
+ def destroy_widgets
37
+ Widget.delete_all
38
+ end
39
+ end
@@ -0,0 +1,68 @@
1
+ require File.expand_path("../../test_helper", File.dirname(__FILE__))
2
+
3
+ class PaginationHelperTest < ActionView::TestCase
4
+ include Paginary::Helpers::PaginationHelper
5
+
6
+ delegate :params, :to => :controller
7
+
8
+ def url_options
9
+ { :controller => "widgets", :action => "index" }
10
+ end
11
+
12
+ def setup
13
+ @klass = Widget
14
+ @relation = @klass.unscoped
15
+ end
16
+
17
+ def builder(*args)
18
+ builder = nil
19
+ paginate(*args) { |b| builder = b }
20
+ builder
21
+ end
22
+
23
+ # Items ====================================================================
24
+ test "items should return paginated relation" do
25
+ assert_equal_relation @relation.paginate, builder(@relation).items
26
+ end
27
+
28
+ # Decorated links ==========================================================
29
+ test "links should return nil if there is only one page" do
30
+ assert_nil builder(@relation.paginate(:per_page => 12345)).links
31
+ end
32
+
33
+ test "links should return complete page links" do
34
+ assert_dom_equal %Q{<div class="pagination">} +
35
+ %Q{<span class="previous disabled"><span>&lt; Previous</span></span>} +
36
+ %Q{<a href="/widgets?page=1" class="page selected"><span>1</span></a>} +
37
+ %Q{<a href="/widgets?page=2" class="page"><span>2</span></a>} +
38
+ %Q{<a href="/widgets?page=3" class="page"><span>3</span></a>} +
39
+ %Q{<a href="/widgets?page=4" class="page"><span>4</span></a>} +
40
+ %Q{<a href="/widgets?page=2" class="next"><span>Next &gt;</span></a>} +
41
+ %Q{</div>}, builder(@relation).links
42
+ end
43
+
44
+ test "page_links should return page links" do
45
+ assert_dom_equal %Q{<a href="/widgets?page=1" class="page selected"><span>1</span></a>} +
46
+ %Q{<a href="/widgets?page=2" class="page"><span>2</span></a>} +
47
+ %Q{<a href="/widgets?page=3" class="page"><span>3</span></a>} +
48
+ %Q{<a href="/widgets?page=4" class="page"><span>4</span></a>}, builder(@relation).page_links
49
+ end
50
+
51
+ test "next_link should return link to next page" do
52
+ assert_dom_equal %Q{<a href="/widgets?page=2" class="next"><span>Next &gt;</span></a>}, builder(@relation).next_link
53
+ end
54
+
55
+ test "next_link should return disabled link to next page if current page is last" do
56
+ params[:page] = (@relation.count.to_f / 50).ceil
57
+ assert_dom_equal %Q{<span class="next disabled"><span>Next &gt;</span></span>}, builder(@relation).next_link
58
+ end
59
+
60
+ test "previous_link should return link to previous page" do
61
+ params[:page] = (@relation.count.to_f / 50).ceil
62
+ assert_dom_equal %Q{<a href="/widgets?page=#{params[:page] - 1}" class="previous"><span>&lt; Previous</span></a>}, builder(@relation).previous_link
63
+ end
64
+
65
+ test "previous_link should return disabled link to previous page if current page is first" do
66
+ assert_dom_equal %Q{<span class="previous disabled"><span>&lt; Previous</span></span>}, builder(@relation).previous_link
67
+ end
68
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path("../../test_helper", File.dirname(__FILE__))
2
+
3
+ class PaginationHelperTest < ActionView::TestCase
4
+ include Paginary::Helpers::PaginationHelper
5
+
6
+ delegate :params, :to => :controller
7
+
8
+ def url_options
9
+ { :controller => "widgets", :action => "index" }
10
+ end
11
+
12
+ def setup
13
+ @klass = Widget
14
+ @relation = @klass.unscoped
15
+ end
16
+
17
+ # Pagination ===============================================================
18
+ test "paginate should yield page builder" do
19
+ builder = nil
20
+ paginate(@relation) { |b| builder = b }
21
+ assert_kind_of Paginary::Helpers::PageBuilder, builder
22
+ end
23
+
24
+ test "paginate should query database for total number of items only once" do
25
+ num = count_queries do
26
+ paginate(@relation) { |page| page.links; page.items.to_a }
27
+ end
28
+ assert_equal 2, num
29
+ end
30
+ end
@@ -0,0 +1,74 @@
1
+ require File.expand_path("../../test_helper", File.dirname(__FILE__))
2
+
3
+ class PaginatedTest < ActiveRecord::TestCase
4
+ def setup
5
+ @klass = Widget
6
+ @relation = @klass.scoped
7
+ end
8
+
9
+ # Relation methods =========================================================
10
+ test "item_count should return number of items without pagination" do
11
+ assert_equal @klass.count, @relation.paginate.item_count
12
+ end
13
+
14
+ test "page_count should return number of pages" do
15
+ assert_equal (@klass.count.to_f / 17).ceil, @relation.paginate(:per_page => 17).page_count
16
+ end
17
+
18
+ test "current_page should return one by default" do
19
+ assert_equal 1, @relation.paginate.current_page
20
+ end
21
+
22
+ test "current_page should return given page" do
23
+ assert_equal 3, @relation.paginate(3).current_page
24
+ end
25
+
26
+ test "last_page? should return false if there is a next page" do
27
+ assert_equal false, @relation.paginate(1).last_page?
28
+ end
29
+
30
+ test "last_page? should return true if there is no next page" do
31
+ assert_equal true, @relation.paginate((@klass.count.to_f / 50).ceil).last_page?
32
+ end
33
+
34
+ test "first_page? should return false if there is a previous page" do
35
+ assert_equal false, @relation.paginate(2).first_page?
36
+ end
37
+
38
+ test "first_page? should return true if there is no previous page" do
39
+ assert_equal true, @relation.paginate(1).first_page?
40
+ end
41
+
42
+ # Page validation ==========================================================
43
+ test "paginate should raise error if given page is less than one" do
44
+ assert_raises ActiveRecord::RecordNotFound do
45
+ @relation.paginate(0)
46
+ end
47
+ end
48
+
49
+ test "paginate should raise error if given page does not exist" do
50
+ assert_raises ActiveRecord::RecordNotFound do
51
+ @relation.paginate(1239)
52
+ end
53
+ end
54
+
55
+ # Page calculation =========================================================
56
+ test "page_count should return 1 if there are no items" do
57
+ assert_equal 1, @relation.where(:deleted => true).paginate.page_count
58
+ end
59
+
60
+ test "page_count should return 1 if number of items is within number of items per page" do
61
+ 16.times { Widget.create! :deleted => true }
62
+ assert_equal 1, @relation.where(:deleted => true).paginate(:per_page => 17).page_count
63
+ end
64
+
65
+ test "page_count should return 1 if number of items is equal to number of items per page" do
66
+ 17.times { Widget.create! :deleted => true }
67
+ assert_equal 1, @relation.where(:deleted => true).paginate(:per_page => 17).page_count
68
+ end
69
+
70
+ test "page_count should return 2 if number of items is larger than number of items per page" do
71
+ 18.times { Widget.create! :deleted => true }
72
+ assert_equal 2, @relation.where(:deleted => true).paginate(:per_page => 17).page_count
73
+ end
74
+ end
@@ -0,0 +1,42 @@
1
+ require File.expand_path("../../test_helper", File.dirname(__FILE__))
2
+
3
+ class QueryMethodsTest < ActiveRecord::TestCase
4
+ def setup
5
+ @klass = Widget
6
+ @relation = @klass.scoped
7
+ end
8
+
9
+ # Relation methods =========================================================
10
+ test "paginate should return first fifty items by default" do
11
+ assert_equal_relation @relation.limit(50), @relation.paginate
12
+ end
13
+
14
+ test "paginate should return second fifty items if page number is two" do
15
+ assert_equal_relation @relation.offset(50).limit(50), @relation.paginate(2)
16
+ end
17
+
18
+ test "paginate should return first n items as specified by per_page" do
19
+ assert_equal_relation @relation.limit(37), @relation.paginate(:per_page => 37)
20
+ end
21
+
22
+ test "paginated? should return true if paginate has been called" do
23
+ assert_equal true, @relation.paginate.paginated?
24
+ end
25
+
26
+ test "paginated? should return false if paginate has not been called" do
27
+ assert_equal false, @relation.paginated?
28
+ end
29
+
30
+ test "paginated? should return false if everything fits on one page" do
31
+ assert_equal false, @relation.paginate(:per_page => 1234).paginated?
32
+ end
33
+
34
+ # Class methods ============================================================
35
+ test "paginate on class should return first fifty items by default" do
36
+ assert_equal_relation @relation.limit(50), @klass.paginate
37
+ end
38
+
39
+ test "paginated? on class should return false" do
40
+ assert_equal false, @klass.paginated?
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paginary
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ - pre1
10
+ version: 0.0.1.pre1
11
+ platform: ruby
12
+ authors:
13
+ - Rolf Timmermans
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-20 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: sqlite3-ruby
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :development
46
+ version_requirements: *id002
47
+ description: Simple, view-based pagination for Rails, built on top of Active Record 3 awesomeness.
48
+ email: r.timmermans@voormedia.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - LICENSE
55
+ - README.md
56
+ files:
57
+ - .gitignore
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - VERSION
64
+ - lib/paginary.rb
65
+ - lib/paginary/helpers/page_builder.rb
66
+ - lib/paginary/helpers/pagination_helper.rb
67
+ - lib/paginary/railtie.rb
68
+ - lib/paginary/relation/paginated.rb
69
+ - lib/paginary/relation/query_methods.rb
70
+ - paginary.gemspec
71
+ - test/dummy/app/controllers/application_controller.rb
72
+ - test/dummy/app/controllers/widgets_controller.rb
73
+ - test/dummy/app/models/widget.rb
74
+ - test/dummy/app/views/layouts/application.html.erb
75
+ - test/dummy/app/views/widgets/_form.html.erb
76
+ - test/dummy/app/views/widgets/edit.html.erb
77
+ - test/dummy/app/views/widgets/index.html.erb
78
+ - test/dummy/app/views/widgets/new.html.erb
79
+ - test/dummy/app/views/widgets/show.html.erb
80
+ - test/dummy/config.ru
81
+ - test/dummy/config/application.rb
82
+ - test/dummy/config/boot.rb
83
+ - test/dummy/config/database.yml
84
+ - test/dummy/config/environment.rb
85
+ - test/dummy/config/environments/test.rb
86
+ - test/dummy/config/routes.rb
87
+ - test/dummy/db/schema.rb
88
+ - test/test_helper.rb
89
+ - test/unit/helpers/page_builder_test.rb
90
+ - test/unit/helpers/pagination_helper_test.rb
91
+ - test/unit/relation/paginated_test.rb
92
+ - test/unit/relation/query_methods_test.rb
93
+ has_rdoc: true
94
+ homepage: http://rails-erd.rubyforge.org/
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options:
99
+ - --charset=UTF-8
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">"
114
+ - !ruby/object:Gem::Version
115
+ segments:
116
+ - 1
117
+ - 3
118
+ - 1
119
+ version: 1.3.1
120
+ requirements: []
121
+
122
+ rubyforge_project: paginary
123
+ rubygems_version: 1.3.7
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: View-based pagination for Rails.
127
+ test_files:
128
+ - test/dummy/app/controllers/application_controller.rb
129
+ - test/dummy/app/controllers/widgets_controller.rb
130
+ - test/dummy/app/models/widget.rb
131
+ - test/dummy/config/application.rb
132
+ - test/dummy/config/boot.rb
133
+ - test/dummy/config/environment.rb
134
+ - test/dummy/config/environments/test.rb
135
+ - test/dummy/config/routes.rb
136
+ - test/dummy/db/schema.rb
137
+ - test/test_helper.rb
138
+ - test/unit/helpers/page_builder_test.rb
139
+ - test/unit/helpers/pagination_helper_test.rb
140
+ - test/unit/relation/paginated_test.rb
141
+ - test/unit/relation/query_methods_test.rb