sayso-kaminari 0.12.4.001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.document +5 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +7 -0
  4. data/.rspec +2 -0
  5. data/CHANGELOG +245 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +20 -0
  8. data/README.rdoc +217 -0
  9. data/Rakefile +22 -0
  10. data/app/views/kaminari/_first_page.html.erb +11 -0
  11. data/app/views/kaminari/_first_page.html.haml +9 -0
  12. data/app/views/kaminari/_first_page.html.slim +10 -0
  13. data/app/views/kaminari/_gap.html.erb +8 -0
  14. data/app/views/kaminari/_gap.html.haml +8 -0
  15. data/app/views/kaminari/_gap.html.slim +9 -0
  16. data/app/views/kaminari/_last_page.html.erb +11 -0
  17. data/app/views/kaminari/_last_page.html.haml +9 -0
  18. data/app/views/kaminari/_last_page.html.slim +10 -0
  19. data/app/views/kaminari/_next_page.html.erb +11 -0
  20. data/app/views/kaminari/_next_page.html.haml +9 -0
  21. data/app/views/kaminari/_next_page.html.slim +10 -0
  22. data/app/views/kaminari/_page.html.erb +12 -0
  23. data/app/views/kaminari/_page.html.haml +10 -0
  24. data/app/views/kaminari/_page.html.slim +11 -0
  25. data/app/views/kaminari/_paginator.html.erb +23 -0
  26. data/app/views/kaminari/_paginator.html.haml +18 -0
  27. data/app/views/kaminari/_paginator.html.slim +19 -0
  28. data/app/views/kaminari/_prev_page.html.erb +11 -0
  29. data/app/views/kaminari/_prev_page.html.haml +9 -0
  30. data/app/views/kaminari/_prev_page.html.slim +10 -0
  31. data/config/locales/kaminari.yml +10 -0
  32. data/kaminari.gemspec +35 -0
  33. data/lib/generators/kaminari/config_generator.rb +16 -0
  34. data/lib/generators/kaminari/templates/kaminari_config.rb +8 -0
  35. data/lib/generators/kaminari/views_generator.rb +103 -0
  36. data/lib/kaminari.rb +2 -0
  37. data/lib/kaminari/config.rb +41 -0
  38. data/lib/kaminari/engine.rb +4 -0
  39. data/lib/kaminari/helpers/action_view_extension.rb +50 -0
  40. data/lib/kaminari/helpers/paginator.rb +154 -0
  41. data/lib/kaminari/helpers/tags.rb +95 -0
  42. data/lib/kaminari/models/active_record_extension.rb +25 -0
  43. data/lib/kaminari/models/active_record_relation_methods.rb +35 -0
  44. data/lib/kaminari/models/array_extension.rb +42 -0
  45. data/lib/kaminari/models/configuration_methods.rb +20 -0
  46. data/lib/kaminari/models/fixer.rb +102 -0
  47. data/lib/kaminari/models/mongo_mapper_extension.rb +18 -0
  48. data/lib/kaminari/models/mongoid_criteria_methods.rb +18 -0
  49. data/lib/kaminari/models/mongoid_extension.rb +31 -0
  50. data/lib/kaminari/models/page_scope_methods.rb +36 -0
  51. data/lib/kaminari/models/plucky_criteria_methods.rb +18 -0
  52. data/lib/kaminari/railtie.rb +37 -0
  53. data/lib/kaminari/version.rb +3 -0
  54. data/spec/acceptance/acceptance_helper.rb +5 -0
  55. data/spec/acceptance/support/helpers.rb +5 -0
  56. data/spec/acceptance/support/paths.rb +9 -0
  57. data/spec/acceptance/users_spec.rb +53 -0
  58. data/spec/config/config_spec.rb +61 -0
  59. data/spec/fake_app.rb +80 -0
  60. data/spec/helpers/action_view_extension_spec.rb +37 -0
  61. data/spec/helpers/helpers_spec.rb +135 -0
  62. data/spec/helpers/tags_spec.rb +140 -0
  63. data/spec/models/active_record_relation_methods_spec.rb +28 -0
  64. data/spec/models/array_spec.rb +105 -0
  65. data/spec/models/default_per_page_spec.rb +29 -0
  66. data/spec/models/mongo_mapper_spec.rb +79 -0
  67. data/spec/models/mongoid_spec.rb +72 -0
  68. data/spec/models/scopes_spec.rb +149 -0
  69. data/spec/spec_helper.rb +28 -0
  70. data/spec/support/database_cleaner.rb +13 -0
  71. data/spec/support/matchers.rb +46 -0
  72. metadata +264 -0
@@ -0,0 +1,20 @@
1
+ module Kaminari
2
+ module ConfigurationMethods
3
+ extend ActiveSupport::Concern
4
+ module ClassMethods
5
+ # Overrides the default +per_page+ value per model
6
+ # class Article < ActiveRecord::Base
7
+ # paginates_per 10
8
+ # end
9
+ def paginates_per(val)
10
+ @_default_per_page = val
11
+ end
12
+
13
+ # This model's default +per_page+ value
14
+ # returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt>
15
+ def default_per_page
16
+ @_default_per_page || Kaminari.config.default_per_page
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,102 @@
1
+ module Kaminari
2
+
3
+ class Fixer
4
+
5
+ protected
6
+
7
+ def self.do_array(collection, options)
8
+ Kaminari.paginate_array(collection)
9
+ collection.instance_eval <<-EVAL
10
+ def current_page
11
+ @current_page || 1
12
+ end
13
+ def limit_value
14
+ per_page
15
+ end
16
+ def per_page
17
+ @per_page || size
18
+ end
19
+ def page(value)
20
+ @current_page = value.to_i.zero? ? 1 : value.to_i
21
+ self
22
+ end
23
+ def per(value)
24
+ @per_page = value
25
+ self
26
+ end
27
+ def total_count
28
+ #{options[:total].to_s + ' ||' if options[:total]} size
29
+ end
30
+ def num_pages
31
+ (total_count.to_f / (per_page.zero? ? 1 : per_page)).ceil
32
+ end
33
+ def offset
34
+ (current_page - 1) * per_page
35
+ end
36
+ def offset_value
37
+ offset
38
+ end
39
+ def scoped
40
+ self[offset...offset + per_page]
41
+ end
42
+ def all
43
+ scoped
44
+ end
45
+ EVAL
46
+ collection
47
+ end
48
+
49
+ def self.do_thinking_sphinx(collection, options)
50
+ collection.instance_eval <<-EVAL
51
+ def limit_value
52
+ per_page
53
+ end
54
+ def offset_value
55
+ offset
56
+ end
57
+ def page(value)
58
+ self
59
+ end
60
+ def per(value)
61
+ self
62
+ end
63
+ def total_count
64
+ total_entries
65
+ end
66
+ def scoped
67
+ self
68
+ end
69
+ def num_pages
70
+ total_pages
71
+ end
72
+ def all
73
+ scoped
74
+ end
75
+ EVAL
76
+ collection
77
+ end
78
+
79
+ def self.do_ar_relation(collection, options)
80
+ collection.instance_eval <<-EVAL
81
+ def total_count
82
+ #{options[:total]}
83
+ end
84
+ EVAL
85
+ collection
86
+ end
87
+
88
+ public
89
+
90
+ def self.do(input_collection, options = {})
91
+ collection = input_collection
92
+ if collection.is_a?(ThinkingSphinx::Search)
93
+ collection = do_thinking_sphinx(collection, options)
94
+ elsif collection.is_a?(ActiveRecord::Relation) && options[:total]
95
+ collection = do_ar_relation(collection, options)
96
+ elsif collection.is_a?(Array)
97
+ collection = do_array(collection, options)
98
+ end
99
+ collection
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), 'plucky_criteria_methods')
2
+
3
+ module Kaminari
4
+ module MongoMapperExtension
5
+ module Document
6
+ extend ActiveSupport::Concern
7
+ include Kaminari::ConfigurationMethods
8
+
9
+ included do
10
+ # Fetch the values at the specified page number
11
+ # Model.page(5)
12
+ scope :page, Proc.new {|num|
13
+ limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Kaminari
2
+ module MongoidCriteriaMethods
3
+ extend ActiveSupport::Concern
4
+ module InstanceMethods
5
+ def limit_value #:nodoc:
6
+ options[:limit]
7
+ end
8
+
9
+ def offset_value #:nodoc:
10
+ options[:skip]
11
+ end
12
+
13
+ def total_count #:nodoc:
14
+ count
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ require File.join(File.dirname(__FILE__), 'mongoid_criteria_methods')
2
+
3
+ module Kaminari
4
+ module MongoidExtension
5
+ module Criteria
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ def page(*args)
10
+ self.klass.page(*args).criteria.merge(self)
11
+ end
12
+ end
13
+ end
14
+
15
+ module Document
16
+ extend ActiveSupport::Concern
17
+ include Kaminari::ConfigurationMethods
18
+
19
+ included do
20
+ # Fetch the values at the specified page number
21
+ # Model.page(5)
22
+ scope :page, Proc.new {|num|
23
+ limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
24
+ } do
25
+ include Kaminari::MongoidCriteriaMethods
26
+ include Kaminari::PageScopeMethods
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ module Kaminari
2
+ module PageScopeMethods
3
+ extend ActiveSupport::Concern
4
+ module InstanceMethods
5
+ # Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
6
+ # Model.page(3).per(10)
7
+ def per(num)
8
+ if (n = num.to_i) <= 0
9
+ self
10
+ else
11
+ limit(n).offset(offset_value / limit_value * n)
12
+ end
13
+ end
14
+
15
+ # Total number of pages
16
+ def num_pages
17
+ (total_count.to_f / limit_value).ceil
18
+ end
19
+
20
+ # Current page number
21
+ def current_page
22
+ (offset_value / limit_value) + 1
23
+ end
24
+
25
+ # First page of the collection ?
26
+ def first_page?
27
+ current_page == 1
28
+ end
29
+
30
+ # Last page of the collection?
31
+ def last_page?
32
+ current_page >= num_pages
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ module Kaminari
2
+ module PluckyCriteriaMethods
3
+ extend ActiveSupport::Concern
4
+ module InstanceMethods
5
+ def limit_value #:nodoc:
6
+ options[:limit]
7
+ end
8
+
9
+ def offset_value #:nodoc:
10
+ options[:skip]
11
+ end
12
+
13
+ def total_count #:nodoc:
14
+ count
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ require 'rails'
2
+ # ensure ORMs are loaded *before* initializing Kaminari
3
+ begin; require 'mongoid'; rescue LoadError; end
4
+ begin; require 'mongo_mapper'; rescue LoadError; end
5
+
6
+ require File.join(File.dirname(__FILE__), 'config')
7
+ require File.join(File.dirname(__FILE__), 'helpers/action_view_extension')
8
+ require File.join(File.dirname(__FILE__), 'helpers/paginator')
9
+ require File.join(File.dirname(__FILE__), 'models/page_scope_methods')
10
+ require File.join(File.dirname(__FILE__), 'models/configuration_methods')
11
+
12
+ module Kaminari
13
+ class Railtie < ::Rails::Railtie #:nodoc:
14
+ initializer 'kaminari' do |app|
15
+ ActiveSupport.on_load(:active_record) do
16
+ require File.join(File.dirname(__FILE__), 'models/active_record_extension')
17
+ require File.join(File.dirname(__FILE__), 'models/fixer')
18
+ ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
19
+ end
20
+ if defined? ::Mongoid
21
+ require File.join(File.dirname(__FILE__), 'models/mongoid_extension')
22
+ ::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
23
+ ::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria
24
+ end
25
+ if defined? ::MongoMapper
26
+ require File.join(File.dirname(__FILE__), 'models/mongo_mapper_extension')
27
+ ::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
28
+ ::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
29
+ ::Plucky::Query.send :include, Kaminari::PageScopeMethods
30
+ end
31
+ require File.join(File.dirname(__FILE__), 'models/array_extension')
32
+ ActiveSupport.on_load(:action_view) do
33
+ ::ActionView::Base.send :include, Kaminari::ActionViewExtension
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Kaminari
2
+ VERSION = '0.12.4.001'
3
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+ require "steak"
3
+
4
+ # Put your acceptance spec helpers inside /spec/acceptance/support
5
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@@ -0,0 +1,5 @@
1
+ module HelperMethods
2
+ # Put helper methods you need to be available in all tests here.
3
+ end
4
+
5
+ RSpec.configuration.include HelperMethods, :type => :acceptance
@@ -0,0 +1,9 @@
1
+ module NavigationHelpers
2
+ # Put helper methods related to the paths in your application here.
3
+
4
+ def homepage
5
+ "/"
6
+ end
7
+ end
8
+
9
+ RSpec.configuration.include NavigationHelpers, :type => :acceptance
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/acceptance_helper')
3
+
4
+ feature 'Users' do
5
+ background do
6
+ 1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}" }
7
+ end
8
+ scenario 'navigating by pagination links' do
9
+ visit users_path
10
+
11
+ within 'nav.pagination' do
12
+ within 'span.page.current' do
13
+ page.should have_content '1'
14
+ end
15
+ within 'span.next' do
16
+ click_link 'Next ›'
17
+ end
18
+ end
19
+
20
+ within 'nav.pagination' do
21
+ within 'span.page.current' do
22
+ page.should have_content '2'
23
+ end
24
+ within 'span.last' do
25
+ click_link 'Last »'
26
+ end
27
+ end
28
+
29
+ within 'nav.pagination' do
30
+ within 'span.page.current' do
31
+ page.should have_content '4'
32
+ end
33
+ within 'span.prev' do
34
+ click_link '‹ Prev'
35
+ end
36
+ end
37
+
38
+ within 'nav.pagination' do
39
+ within 'span.page.current' do
40
+ page.should have_content '3'
41
+ end
42
+ within 'span.first' do
43
+ click_link '« First'
44
+ end
45
+ end
46
+
47
+ within 'nav.pagination' do
48
+ within 'span.page.current' do
49
+ page.should have_content '1'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Kaminari::Configuration do
4
+ subject { Kaminari.config }
5
+ describe 'default_per_page' do
6
+ context 'by default' do
7
+ its(:default_per_page) { should == 25 }
8
+ end
9
+ context 'configured via config block' do
10
+ before do
11
+ Kaminari.configure {|c| c.default_per_page = 17}
12
+ end
13
+ its(:default_per_page) { should == 17 }
14
+ after do
15
+ Kaminari.configure {|c| c.default_per_page = 25}
16
+ end
17
+ end
18
+ end
19
+
20
+ describe 'window' do
21
+ context 'by default' do
22
+ its(:window) { should == 4 }
23
+ end
24
+ end
25
+
26
+ describe 'outer_window' do
27
+ context 'by default' do
28
+ its(:outer_window) { should == 0 }
29
+ end
30
+ end
31
+
32
+ describe 'left' do
33
+ context 'by default' do
34
+ its(:left) { should == 0 }
35
+ end
36
+ end
37
+
38
+ describe 'right' do
39
+ context 'by default' do
40
+ its(:right) { should == 0 }
41
+ end
42
+ end
43
+
44
+ describe 'param_name' do
45
+ context 'by default' do
46
+ its(:param_name) { should == :page }
47
+ end
48
+
49
+ context 'configured via config block' do
50
+ before do
51
+ Kaminari.configure {|c| c.param_name = lambda { :test } }
52
+ end
53
+
54
+ its(:param_name) { should == :test }
55
+
56
+ after do
57
+ Kaminari.configure {|c| c.param_name = :page }
58
+ end
59
+ end
60
+ end
61
+ end
data/spec/fake_app.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'active_record'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+
5
+ # database
6
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
7
+ ActiveRecord::Base.establish_connection('test')
8
+
9
+ # config
10
+ app = Class.new(Rails::Application)
11
+ app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
12
+ app.config.session_store :cookie_store, :key => "_myapp_session"
13
+ app.config.active_support.deprecation = :log
14
+ app.initialize!
15
+
16
+ # routes
17
+ app.routes.draw do
18
+ resources :users
19
+ end
20
+
21
+ # models
22
+ class User < ActiveRecord::Base
23
+ has_many :authorships
24
+ has_many :readerships
25
+ has_many :books_authored, :through => :authorships, :source => :book
26
+ has_many :books_read, :through => :readerships, :source => :book
27
+
28
+ def readers
29
+ User.joins(:books_read => :authors).where(:authors_books => {:id => self})
30
+ end
31
+
32
+ scope :by_name, order(:name)
33
+ scope :by_read_count, lambda {
34
+ cols = if connection.adapter_name == "PostgreSQL"
35
+ column_names.map { |column| %{"users"."#{column}"} }.join(", ")
36
+ else
37
+ '"users"."id"'
38
+ end
39
+ group(cols).select("count(readerships.id) AS read_count, #{cols}").order('read_count DESC')
40
+ }
41
+ end
42
+ class Authorship < ActiveRecord::Base
43
+ belongs_to :user
44
+ belongs_to :book
45
+ end
46
+ class Readership < ActiveRecord::Base
47
+ belongs_to :user
48
+ belongs_to :book
49
+ end
50
+ class Book < ActiveRecord::Base
51
+ has_many :authorships
52
+ has_many :readerships
53
+ has_many :authors, :through => :authorships, :source => :user
54
+ has_many :readers, :through => :readerships, :source => :user
55
+ end
56
+
57
+ # controllers
58
+ class ApplicationController < ActionController::Base; end
59
+ class UsersController < ApplicationController
60
+ def index
61
+ @users = User.page params[:page]
62
+ render :inline => <<-ERB
63
+ <%= @users.map(&:name).join("\n") %>
64
+ <%= paginate @users %>
65
+ ERB
66
+ end
67
+ end
68
+
69
+ # helpers
70
+ Object.const_set(:ApplicationHelper, Module.new)
71
+
72
+ #migrations
73
+ class CreateAllTables < ActiveRecord::Migration
74
+ def self.up
75
+ create_table(:users) {|t| t.string :name; t.integer :age}
76
+ create_table(:books) {|t| t.string :title}
77
+ create_table(:readerships) {|t| t.integer :user_id; t.integer :book_id }
78
+ create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id }
79
+ end
80
+ end