kaminari 0.14.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +19 -0
  3. data/.document +1 -4
  4. data/.gitignore +2 -1
  5. data/.travis.yml +91 -10
  6. data/CHANGELOG.md +636 -0
  7. data/CONTRIBUTING.md +22 -0
  8. data/Gemfile +3 -1
  9. data/README.md +573 -0
  10. data/Rakefile +43 -20
  11. data/gemfiles/active_record_41.gemfile +28 -0
  12. data/gemfiles/active_record_42.gemfile +28 -0
  13. data/gemfiles/active_record_50.gemfile +24 -0
  14. data/gemfiles/active_record_51.gemfile +24 -0
  15. data/gemfiles/active_record_52.gemfile +24 -0
  16. data/gemfiles/active_record_60.gemfile +26 -0
  17. data/gemfiles/active_record_edge.gemfile +33 -0
  18. data/kaminari.gemspec +23 -28
  19. data/lib/kaminari/version.rb +3 -1
  20. data/lib/kaminari.rb +4 -37
  21. data/misc/bug_report_template.rb +57 -0
  22. metadata +67 -229
  23. data/.gemtest +0 -0
  24. data/.rspec +0 -2
  25. data/CHANGELOG +0 -351
  26. data/README.rdoc +0 -295
  27. data/app/views/kaminari/_first_page.html.erb +0 -11
  28. data/app/views/kaminari/_first_page.html.haml +0 -9
  29. data/app/views/kaminari/_first_page.html.slim +0 -10
  30. data/app/views/kaminari/_gap.html.erb +0 -8
  31. data/app/views/kaminari/_gap.html.haml +0 -8
  32. data/app/views/kaminari/_gap.html.slim +0 -9
  33. data/app/views/kaminari/_last_page.html.erb +0 -11
  34. data/app/views/kaminari/_last_page.html.haml +0 -9
  35. data/app/views/kaminari/_last_page.html.slim +0 -10
  36. data/app/views/kaminari/_next_page.html.erb +0 -11
  37. data/app/views/kaminari/_next_page.html.haml +0 -9
  38. data/app/views/kaminari/_next_page.html.slim +0 -10
  39. data/app/views/kaminari/_page.html.erb +0 -12
  40. data/app/views/kaminari/_page.html.haml +0 -10
  41. data/app/views/kaminari/_page.html.slim +0 -11
  42. data/app/views/kaminari/_paginator.html.erb +0 -23
  43. data/app/views/kaminari/_paginator.html.haml +0 -18
  44. data/app/views/kaminari/_paginator.html.slim +0 -19
  45. data/app/views/kaminari/_prev_page.html.erb +0 -11
  46. data/app/views/kaminari/_prev_page.html.haml +0 -9
  47. data/app/views/kaminari/_prev_page.html.slim +0 -10
  48. data/config/locales/kaminari.yml +0 -19
  49. data/gemfiles/active_record_30.gemfile +0 -7
  50. data/gemfiles/active_record_31.gemfile +0 -7
  51. data/gemfiles/active_record_32.gemfile +0 -7
  52. data/gemfiles/data_mapper_12.gemfile +0 -12
  53. data/gemfiles/mongo_mapper.gemfile +0 -7
  54. data/gemfiles/mongoid_24.gemfile +0 -7
  55. data/gemfiles/mongoid_30.gemfile +0 -7
  56. data/gemfiles/sinatra.gemfile +0 -10
  57. data/lib/generators/kaminari/config_generator.rb +0 -16
  58. data/lib/generators/kaminari/templates/kaminari_config.rb +0 -10
  59. data/lib/generators/kaminari/views_generator.rb +0 -118
  60. data/lib/kaminari/config.rb +0 -49
  61. data/lib/kaminari/engine.rb +0 -4
  62. data/lib/kaminari/grape.rb +0 -4
  63. data/lib/kaminari/helpers/action_view_extension.rb +0 -151
  64. data/lib/kaminari/helpers/paginator.rb +0 -175
  65. data/lib/kaminari/helpers/sinatra_helpers.rb +0 -138
  66. data/lib/kaminari/helpers/tags.rb +0 -95
  67. data/lib/kaminari/hooks.rb +0 -37
  68. data/lib/kaminari/models/active_record_extension.rb +0 -22
  69. data/lib/kaminari/models/active_record_model_extension.rb +0 -20
  70. data/lib/kaminari/models/active_record_relation_methods.rb +0 -31
  71. data/lib/kaminari/models/array_extension.rb +0 -60
  72. data/lib/kaminari/models/configuration_methods.rb +0 -34
  73. data/lib/kaminari/models/data_mapper_collection_methods.rb +0 -15
  74. data/lib/kaminari/models/data_mapper_extension.rb +0 -48
  75. data/lib/kaminari/models/mongo_mapper_extension.rb +0 -18
  76. data/lib/kaminari/models/mongoid_criteria_methods.rb +0 -23
  77. data/lib/kaminari/models/mongoid_extension.rb +0 -33
  78. data/lib/kaminari/models/page_scope_methods.rb +0 -41
  79. data/lib/kaminari/models/plucky_criteria_methods.rb +0 -18
  80. data/lib/kaminari/railtie.rb +0 -7
  81. data/lib/kaminari/sinatra.rb +0 -5
  82. data/spec/config/config_spec.rb +0 -76
  83. data/spec/fake_app/active_record/config.rb +0 -3
  84. data/spec/fake_app/active_record/models.rb +0 -57
  85. data/spec/fake_app/data_mapper/config.rb +0 -1
  86. data/spec/fake_app/data_mapper/models.rb +0 -27
  87. data/spec/fake_app/mongo_mapper/config.rb +0 -2
  88. data/spec/fake_app/mongo_mapper/models.rb +0 -9
  89. data/spec/fake_app/mongoid/config.rb +0 -16
  90. data/spec/fake_app/mongoid/models.rb +0 -22
  91. data/spec/fake_app/rails_app.rb +0 -55
  92. data/spec/fake_app/sinatra_app.rb +0 -22
  93. data/spec/fake_gem.rb +0 -4
  94. data/spec/helpers/action_view_extension_spec.rb +0 -262
  95. data/spec/helpers/helpers_spec.rb +0 -135
  96. data/spec/helpers/sinatra_helpers_spec.rb +0 -170
  97. data/spec/helpers/tags_spec.rb +0 -140
  98. data/spec/models/active_record/active_record_relation_methods_spec.rb +0 -30
  99. data/spec/models/active_record/default_per_page_spec.rb +0 -32
  100. data/spec/models/active_record/max_per_page_spec.rb +0 -32
  101. data/spec/models/active_record/scopes_spec.rb +0 -166
  102. data/spec/models/array_spec.rb +0 -121
  103. data/spec/models/data_mapper/data_mapper_spec.rb +0 -155
  104. data/spec/models/mongo_mapper/mongo_mapper_spec.rb +0 -72
  105. data/spec/models/mongoid/mongoid_spec.rb +0 -110
  106. data/spec/requests/users_spec.rb +0 -53
  107. data/spec/spec_helper.rb +0 -32
  108. data/spec/spec_helper_for_sinatra.rb +0 -35
  109. data/spec/support/database_cleaner.rb +0 -16
  110. data/spec/support/matchers.rb +0 -52
@@ -1,151 +0,0 @@
1
- module Kaminari
2
- # = Helpers
3
- module ActionViewExtension
4
- # A helper that renders the pagination links.
5
- #
6
- # <%= paginate @articles %>
7
- #
8
- # ==== Options
9
- # * <tt>:window</tt> - The "inner window" size (4 by default).
10
- # * <tt>:outer_window</tt> - The "outer window" size (0 by default).
11
- # * <tt>:left</tt> - The "left outer window" size (0 by default).
12
- # * <tt>:right</tt> - The "right outer window" size (0 by default).
13
- # * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.)
14
- # * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
15
- # * <tt>:remote</tt> - Ajax? (false by default)
16
- # * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
17
- def paginate(scope, options = {}, &block)
18
- paginator = Kaminari::Helpers::Paginator.new self, options.reverse_merge(:current_page => scope.current_page, :total_pages => scope.total_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
19
- paginator.to_s
20
- end
21
-
22
- # A simple "Twitter like" pagination link that creates a link to the previous page.
23
- #
24
- # ==== Examples
25
- # Basic usage:
26
- #
27
- # <%= link_to_previous_page @items, 'Previous Page' %>
28
- #
29
- # Ajax:
30
- #
31
- # <%= link_to_previous_page @items, 'Previous Page', :remote => true %>
32
- #
33
- # By default, it renders nothing if there are no more results on the previous page.
34
- # You can customize this output by passing a block.
35
- #
36
- # <%= link_to_previous_page @users, 'Previous Page' do %>
37
- # <span>At the Beginning</span>
38
- # <% end %>
39
- def link_to_previous_page(scope, name, options = {}, &block)
40
- params = options.delete(:params) || {}
41
- param_name = options.delete(:param_name) || Kaminari.config.param_name
42
- link_to_unless scope.first_page?, name, params.merge(param_name => (scope.current_page - 1)), options.reverse_merge(:rel => 'previous') do
43
- block.call if block
44
- end
45
- end
46
-
47
- # A simple "Twitter like" pagination link that creates a link to the next page.
48
- #
49
- # ==== Examples
50
- # Basic usage:
51
- #
52
- # <%= link_to_next_page @items, 'Next Page' %>
53
- #
54
- # Ajax:
55
- #
56
- # <%= link_to_next_page @items, 'Next Page', :remote => true %>
57
- #
58
- # By default, it renders nothing if there are no more results on the next page.
59
- # You can customize this output by passing a block.
60
- #
61
- # <%= link_to_next_page @users, 'Next Page' do %>
62
- # <span>No More Pages</span>
63
- # <% end %>
64
- def link_to_next_page(scope, name, options = {}, &block)
65
- params = options.delete(:params) || {}
66
- param_name = options.delete(:param_name) || Kaminari.config.param_name
67
- link_to_unless scope.last_page?, name, params.merge(param_name => (scope.current_page + 1)), options.reverse_merge(:rel => 'next') do
68
- block.call if block
69
- end
70
- end
71
-
72
- # Renders a helpful message with numbers of displayed vs. total entries.
73
- # Ported from mislav/will_paginate
74
- #
75
- # ==== Examples
76
- # Basic usage:
77
- #
78
- # <%= page_entries_info @posts %>
79
- # #-> Displaying posts 6 - 10 of 26 in total
80
- #
81
- # By default, the message will use the humanized class name of objects
82
- # in collection: for instance, "project types" for ProjectType models.
83
- # The namespace will be cutted out and only the last name will be used.
84
- # Override this with the <tt>:entry_name</tt> parameter:
85
- #
86
- # <%= page_entries_info @posts, :entry_name => 'item' %>
87
- # #-> Displaying items 6 - 10 of 26 in total
88
- def page_entries_info(collection, options = {})
89
- entry_name = if options[:entry_name]
90
- options[:entry_name]
91
- elsif collection.empty? || collection.is_a?(PaginatableArray)
92
- 'entry'
93
- else
94
- if collection.respond_to? :model # DataMapper
95
- collection.model.model_name.human.downcase
96
- else # AR
97
- collection.model_name.human.downcase
98
- end
99
- end
100
- entry_name = entry_name.pluralize unless collection.total_count == 1
101
-
102
- if collection.total_pages < 2
103
- t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)
104
- else
105
- first = collection.offset_value + 1
106
- last = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value
107
- t('helpers.page_entries_info.more_pages.display_entries', :entry_name => entry_name, :first => first, :last => last, :total => collection.total_count)
108
- end.html_safe
109
- end
110
-
111
- # Renders rel="next" and rel="prev" links to be used in the head.
112
- #
113
- # ==== Examples
114
- # Basic usage:
115
- #
116
- # In head:
117
- # <head>
118
- # <title>My Website</title>
119
- # <%= yield :head %>
120
- # </head>
121
- #
122
- # Somewhere in body:
123
- # <% content_for :head do %>
124
- # <%= rel_next_prev_link_tags @items %>
125
- # <% end %>
126
- #
127
- # #-> <link rel="next" href="/items/page/3" /><link rel="prev" href="/items/page/1" />
128
- #
129
- def rel_next_prev_link_tags(scope, options = {})
130
- params = options.delete(:params) || {}
131
- param_name = options.delete(:param_name) || Kaminari.config.param_name
132
-
133
- output = ""
134
-
135
- if !scope.first_page? && !scope.last_page?
136
- # If not first and not last, then output both links.
137
- output << '<link rel="next" href="' + url_for(params.merge(param_name => (scope.current_page + 1))) + '"/>'
138
- output << '<link rel="prev" href="' + url_for(params.merge(param_name => (scope.current_page - 1))) + '"/>'
139
- elsif scope.first_page?
140
- # If first page, add next link unless last page.
141
- output << '<link rel="next" href="' + url_for(params.merge(param_name => (scope.current_page + 1))) + '"/>' unless scope.last_page?
142
- else
143
- # If last page, add prev link unless first page.
144
- output << '<link rel="prev" href="' + url_for(params.merge(param_name => (scope.current_page - 1))) + '"/>' unless scope.first_page?
145
- end
146
-
147
- output.html_safe
148
- end
149
-
150
- end
151
- end
@@ -1,175 +0,0 @@
1
- require 'active_support/inflector'
2
- require 'action_view'
3
- require 'action_view/log_subscriber'
4
- require 'action_view/context'
5
- require 'kaminari/helpers/tags'
6
-
7
- module Kaminari
8
- module Helpers
9
- # The main container tag
10
- class Paginator < Tag
11
- # so that this instance can actually "render"
12
- include ::ActionView::Context
13
-
14
- def initialize(template, options) #:nodoc:
15
- @window_options = {}.tap do |h|
16
- h[:window] = options.delete(:window) || options.delete(:inner_window) || Kaminari.config.window
17
- outer_window = options.delete(:outer_window) || Kaminari.config.outer_window
18
- h[:left] = options.delete(:left) || Kaminari.config.left
19
- h[:left] = outer_window if h[:left] == 0
20
- h[:right] = options.delete(:right) || Kaminari.config.right
21
- h[:right] = outer_window if h[:right] == 0
22
- end
23
- @template, @options = template, options
24
- @theme = @options[:theme] ? "#{@options[:theme]}/" : ''
25
- @options[:current_page] = PageProxy.new @window_options.merge(@options), @options[:current_page], nil
26
- #FIXME for compatibility. remove num_pages at some time in the future
27
- @options[:total_pages] ||= @options[:num_pages]
28
- @last = nil
29
- # initialize the output_buffer for Context
30
- @output_buffer = ActionView::OutputBuffer.new
31
- end
32
-
33
- # render given block as a view template
34
- def render(&block)
35
- instance_eval(&block) if @options[:total_pages] > 1
36
- @output_buffer
37
- end
38
-
39
- # enumerate each page providing PageProxy object as the block parameter
40
- # Because of performance reason, this doesn't actually enumerate all pages but pages that are seemingly relevant to the paginator.
41
- # "Relevant" pages are:
42
- # * pages inside the left outer window plus one for showing the gap tag
43
- # * pages inside the inner window plus one on the left plus one on the right for showing the gap tags
44
- # * pages inside the right outer window plus one for showing the gap tag
45
- def each_relevant_page
46
- return to_enum(:each_relevant_page) unless block_given?
47
-
48
- relevant_pages(@window_options.merge(@options)).each do |i|
49
- yield PageProxy.new(@window_options.merge(@options), i, @last)
50
- end
51
- end
52
- alias each_page each_relevant_page
53
-
54
- def relevant_pages(options)
55
- left_window_plus_one = 1.upto(options[:left] + 1).to_a
56
- right_window_plus_one = (options[:total_pages] - options[:right]).upto(options[:total_pages]).to_a
57
- inside_window_plus_each_sides = (options[:current_page] - options[:window] - 1).upto(options[:current_page] + options[:window] + 1).to_a
58
-
59
- (left_window_plus_one + inside_window_plus_each_sides + right_window_plus_one).uniq.sort.reject {|x| (x < 1) || (x > options[:total_pages])}
60
- end
61
- private :relevant_pages
62
-
63
- def page_tag(page)
64
- @last = Page.new @template, @options.merge(:page => page)
65
- end
66
-
67
- %w[first_page prev_page next_page last_page gap].each do |tag|
68
- eval <<-DEF
69
- def #{tag}_tag
70
- @last = #{tag.classify}.new @template, @options
71
- end
72
- DEF
73
- end
74
-
75
- def to_s #:nodoc:
76
- subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}
77
- return super @window_options.merge(@options).merge :paginator => self unless subscriber
78
-
79
- # dirty hack to suppress logging render_partial
80
- class << subscriber
81
- alias_method :render_partial_with_logging, :render_partial
82
- # do nothing
83
- def render_partial(event); end
84
- end
85
-
86
- ret = super @window_options.merge(@options).merge :paginator => self
87
-
88
- class << subscriber
89
- alias_method :render_partial, :render_partial_with_logging
90
- undef :render_partial_with_logging
91
- end
92
- ret
93
- end
94
-
95
- # Wraps a "page number" and provides some utility methods
96
- class PageProxy
97
- include Comparable
98
-
99
- def initialize(options, page, last) #:nodoc:
100
- @options, @page, @last = options, page, last
101
- end
102
-
103
- # the page number
104
- def number
105
- @page
106
- end
107
-
108
- # current page or not
109
- def current?
110
- @page == @options[:current_page]
111
- end
112
-
113
- # the first page or not
114
- def first?
115
- @page == 1
116
- end
117
-
118
- # the last page or not
119
- def last?
120
- @page == @options[:total_pages]
121
- end
122
-
123
- # the previous page or not
124
- def prev?
125
- @page == @options[:current_page] - 1
126
- end
127
-
128
- # the next page or not
129
- def next?
130
- @page == @options[:current_page] + 1
131
- end
132
-
133
- # within the left outer window or not
134
- def left_outer?
135
- @page <= @options[:left]
136
- end
137
-
138
- # within the right outer window or not
139
- def right_outer?
140
- @options[:total_pages] - @page < @options[:right]
141
- end
142
-
143
- # inside the inner window or not
144
- def inside_window?
145
- (@options[:current_page] - @page).abs <= @options[:window]
146
- end
147
-
148
- # The last rendered tag was "truncated" or not
149
- def was_truncated?
150
- @last.is_a? Gap
151
- end
152
-
153
- def to_i
154
- number
155
- end
156
-
157
- def to_s
158
- number.to_s
159
- end
160
-
161
- def +(other)
162
- to_i + other.to_i
163
- end
164
-
165
- def -(other)
166
- to_i - other.to_i
167
- end
168
-
169
- def <=>(other)
170
- to_i <=> other.to_i
171
- end
172
- end
173
- end
174
- end
175
- end
@@ -1,138 +0,0 @@
1
- require 'active_support/core_ext/object'
2
- require 'active_support/core_ext/string'
3
-
4
- begin
5
-
6
- require 'padrino-helpers'
7
- module Kaminari::Helpers
8
- module SinatraHelpers
9
- class << self
10
- def registered(app)
11
- app.register Padrino::Helpers
12
- app.helpers HelperMethods
13
- end
14
-
15
- alias included registered
16
- end
17
-
18
- class ActionViewTemplateProxy
19
- include Padrino::Helpers::OutputHelpers
20
- include Padrino::Helpers::TagHelpers
21
- include Padrino::Helpers::AssetTagHelpers
22
- include Padrino::Helpers::FormatHelpers
23
- include Padrino::Helpers::TranslationHelpers
24
-
25
- def initialize(opts={})
26
- @current_path = opts[:current_path]
27
- @param_name = (opts[:param_name] || :page).to_sym
28
- @current_params = opts[:current_params]
29
- @current_params.delete(@param_name)
30
- end
31
-
32
- def render(*args)
33
- base = ActionView::Base.new.tap do |a|
34
- a.view_paths << File.expand_path('../../../../app/views', __FILE__)
35
- end
36
- base.render(*args)
37
- end
38
-
39
- def url_for(params)
40
- extra_params = {}
41
- if page = params[@param_name] and page != 1
42
- extra_params[@param_name] = page
43
- end
44
- query = @current_params.merge(extra_params)
45
- @current_path + (query.empty? ? '' : "?#{query.to_query}")
46
- end
47
-
48
- def link_to_unless(condition, name, options = {}, html_options = {}, &block)
49
- options = url_for(options) if options.is_a? Hash
50
- if condition
51
- if block_given?
52
- block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
53
- else
54
- name
55
- end
56
- else
57
- link_to(name, options, html_options)
58
- end
59
- end
60
-
61
- def params
62
- @current_params
63
- end
64
- end
65
-
66
- module HelperMethods
67
- # A helper that renders the pagination links - for Sinatra.
68
- #
69
- # <%= paginate @articles %>
70
- #
71
- # ==== Options
72
- # * <tt>:window</tt> - The "inner window" size (4 by default).
73
- # * <tt>:outer_window</tt> - The "outer window" size (0 by default).
74
- # * <tt>:left</tt> - The "left outer window" size (0 by default).
75
- # * <tt>:right</tt> - The "right outer window" size (0 by default).
76
- # * <tt>:params</tt> - url_for parameters for the links (:id, :locale, etc.)
77
- # * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
78
- # * <tt>:remote</tt> - Ajax? (false by default)
79
- # * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
80
- def paginate(scope, options = {}, &block)
81
- current_path = env['PATH_INFO'] rescue nil
82
- current_params = Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {}
83
- paginator = Kaminari::Helpers::Paginator.new(
84
- ActionViewTemplateProxy.new(:current_params => current_params, :current_path => current_path, :param_name => options[:param_name] || Kaminari.config.param_name),
85
- options.reverse_merge(:current_page => scope.current_page, :total_pages => scope.total_pages, :per_page => scope.limit_value, :param_name => Kaminari.config.param_name, :remote => false)
86
- )
87
- paginator.to_s
88
- end
89
-
90
- # A simple "Twitter like" pagination link that creates a link to the next page.
91
- # Works on Sinatra.
92
- #
93
- # ==== Examples
94
- # Basic usage:
95
- #
96
- # <%= link_to_next_page @items, 'Next Page' %>
97
- #
98
- # Ajax:
99
- #
100
- # <%= link_to_next_page @items, 'Next Page', :remote => true %>
101
- #
102
- # By default, it renders nothing if there are no more results on the next page.
103
- # You can customize this output by passing a parameter <tt>:placeholder</tt>.
104
- #
105
- # <%= link_to_next_page @items, 'Next Page', :placeholder => %{<span>No More Pages</span>} %>
106
- #
107
- def link_to_next_page(scope, name, options = {})
108
- params = options.delete(:params) || (Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {})
109
- param_name = options.delete(:param_name) || Kaminari.config.param_name
110
- placeholder = options.delete(:placeholder)
111
- query = params.merge(param_name => (scope.current_page + 1))
112
- unless scope.last_page?
113
- link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'next')
114
- else
115
- placeholder
116
- end
117
- end
118
- end
119
- end
120
- end
121
-
122
- if defined? I18n
123
- I18n.load_path += Dir.glob(File.expand_path('../../../../config/locales/*.yml', __FILE__))
124
- end
125
-
126
- rescue LoadError
127
-
128
- $stderr.puts "[!]You shold install `padrino-helpers' gem if you want to use kaminari's pagination helpers with Sinatra."
129
- $stderr.puts "[!]Kaminari::Helpers::SinatraHelper does nothing now..."
130
-
131
- module Kaminari::Helpers
132
- module SinatraHelper
133
- def self.registered(*)
134
- end
135
- end
136
- end
137
-
138
- end
@@ -1,95 +0,0 @@
1
- module Kaminari
2
- module Helpers
3
- # A tag stands for an HTML tag inside the paginator.
4
- # Basically, a tag has its own partial template file, so every tag can be
5
- # rendered into String using its partial template.
6
- #
7
- # The template file should be placed in your app/views/kaminari/ directory
8
- # with underscored class name (besides the "Tag" class. Tag is an abstract
9
- # class, so _tag parital is not needed).
10
- # e.g.) PrevLink -> app/views/kaminari/_prev_link.html.erb
11
- #
12
- # When no matching template were found in your app, the engine's pre
13
- # installed template will be used.
14
- # e.g.) Paginator -> $GEM_HOME/kaminari-x.x.x/app/views/kaminari/_paginator.html.erb
15
- class Tag
16
- def initialize(template, options = {}) #:nodoc:
17
- @template, @options = template, options.dup
18
- @param_name = @options.delete(:param_name)
19
- @theme = @options[:theme] ? "#{@options.delete(:theme)}/" : ''
20
- @params = @options[:params] ? template.params.merge(@options.delete :params) : template.params
21
- end
22
-
23
- def to_s(locals = {}) #:nodoc:
24
- @template.render :partial => "kaminari/#{@theme}#{self.class.name.demodulize.underscore}", :locals => @options.merge(locals)
25
- end
26
-
27
- def page_url_for(page)
28
- @template.url_for @params.merge(@param_name => (page <= 1 ? nil : page))
29
- end
30
- end
31
-
32
- # Tag that contains a link
33
- module Link
34
- # target page number
35
- def page
36
- raise 'Override page with the actual page value to be a Page.'
37
- end
38
- # the link's href
39
- def url
40
- page_url_for page
41
- end
42
- def to_s(locals = {}) #:nodoc:
43
- super locals.merge(:url => url)
44
- end
45
- end
46
-
47
- # A page
48
- class Page < Tag
49
- include Link
50
- # target page number
51
- def page
52
- @options[:page]
53
- end
54
- def to_s(locals = {}) #:nodoc:
55
- super locals.merge(:page => page)
56
- end
57
- end
58
-
59
- # Link with page number that appears at the leftmost
60
- class FirstPage < Tag
61
- include Link
62
- def page #:nodoc:
63
- 1
64
- end
65
- end
66
-
67
- # Link with page number that appears at the rightmost
68
- class LastPage < Tag
69
- include Link
70
- def page #:nodoc:
71
- @options[:total_pages]
72
- end
73
- end
74
-
75
- # The "previous" page of the current page
76
- class PrevPage < Tag
77
- include Link
78
- def page #:nodoc:
79
- @options[:current_page] - 1
80
- end
81
- end
82
-
83
- # The "next" page of the current page
84
- class NextPage < Tag
85
- include Link
86
- def page #:nodoc:
87
- @options[:current_page] + 1
88
- end
89
- end
90
-
91
- # Non-link tag that stands for skipped pages...
92
- class Gap < Tag
93
- end
94
- end
95
- end
@@ -1,37 +0,0 @@
1
- module Kaminari
2
- class Hooks
3
- def self.init
4
- ActiveSupport.on_load(:active_record) do
5
- require 'kaminari/models/active_record_extension'
6
- ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
7
- end
8
-
9
- begin; require 'data_mapper'; rescue LoadError; end
10
- if defined? ::DataMapper
11
- require 'dm-aggregates'
12
- require 'kaminari/models/data_mapper_extension'
13
- ::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection
14
- ::DataMapper::Model.append_extensions Kaminari::DataMapperExtension::Model
15
- # ::DataMapper::Model.send :extend, Kaminari::DataMapperExtension::Model
16
- end
17
-
18
- begin; require 'mongoid'; rescue LoadError; end
19
- if defined? ::Mongoid
20
- require 'kaminari/models/mongoid_extension'
21
- ::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria
22
- ::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
23
- end
24
-
25
- ActiveSupport.on_load(:mongo_mapper) do
26
- require 'kaminari/models/mongo_mapper_extension'
27
- ::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
28
- ::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
29
- end
30
- require 'kaminari/models/array_extension'
31
-
32
- ActiveSupport.on_load(:action_view) do
33
- ::ActionView::Base.send :include, Kaminari::ActionViewExtension
34
- end
35
- end
36
- end
37
- end
@@ -1,22 +0,0 @@
1
- require 'kaminari/models/active_record_model_extension'
2
-
3
- module Kaminari
4
- module ActiveRecordExtension
5
- extend ActiveSupport::Concern
6
- included do
7
- # Future subclasses will pick up the model extension
8
- class << self
9
- def inherited_with_kaminari(kls) #:nodoc:
10
- inherited_without_kaminari kls
11
- kls.send(:include, Kaminari::ActiveRecordModelExtension) if kls.superclass == ActiveRecord::Base
12
- end
13
- alias_method_chain :inherited, :kaminari
14
- end
15
-
16
- # Existing subclasses pick up the model extension as well
17
- self.descendants.each do |kls|
18
- kls.send(:include, Kaminari::ActiveRecordModelExtension) if kls.superclass == ActiveRecord::Base
19
- end
20
- end
21
- end
22
- end
@@ -1,20 +0,0 @@
1
- require 'kaminari/models/active_record_relation_methods'
2
-
3
- module Kaminari
4
- module ActiveRecordModelExtension
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- self.send(:include, Kaminari::ConfigurationMethods)
9
-
10
- # Fetch the values at the specified page number
11
- # Model.page(5)
12
- self.scope Kaminari.config.page_method_name, Proc.new {|num|
13
- limit(default_per_page).offset(default_per_page * ([num.to_i, 1].max - 1))
14
- } do
15
- include Kaminari::ActiveRecordRelationMethods
16
- include Kaminari::PageScopeMethods
17
- end
18
- end
19
- end
20
- end
@@ -1,31 +0,0 @@
1
- module Kaminari
2
- module ActiveRecordRelationMethods
3
- # a workaround for AR 3.0.x that returns 0 for #count when page > 1
4
- # if +limit_value+ is specified, load all the records and count them
5
- if ActiveRecord::VERSION::STRING < '3.1'
6
- def count #:nodoc:
7
- limit_value ? length : super
8
- end
9
- end
10
-
11
- def total_count #:nodoc:
12
- # #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
13
- @total_count ||= begin
14
- c = except(:offset, :limit, :order)
15
-
16
- # Remove includes only if they are irrelevant
17
- c = c.except(:includes) unless references_eager_loaded_tables?
18
-
19
- # a workaround to count the actual model instances on distinct query because count + distinct returns wrong value in some cases. see https://github.com/amatsuda/kaminari/pull/160
20
- uses_distinct_sql_statement = c.to_sql =~ /DISTINCT/i
21
- if uses_distinct_sql_statement
22
- c.length
23
- else
24
- # .group returns an OrderdHash that responds to #count
25
- c = c.count
26
- c.respond_to?(:count) ? c.count : c
27
- end
28
- end
29
- end
30
- end
31
- end