kaminari 0.17.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kaminari might be problematic. Click here for more details.

Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.travis.yml +26 -42
  4. data/CHANGELOG.md +566 -0
  5. data/CONTRIBUTING.md +16 -0
  6. data/Gemfile +1 -0
  7. data/README.md +545 -0
  8. data/Rakefile +21 -24
  9. data/gemfiles/active_record_41.gemfile +3 -4
  10. data/gemfiles/active_record_42.gemfile +3 -4
  11. data/gemfiles/active_record_50.gemfile +20 -0
  12. data/gemfiles/active_record_edge.gemfile +11 -7
  13. data/kaminari.gemspec +11 -17
  14. data/lib/kaminari/version.rb +2 -1
  15. data/lib/kaminari.rb +4 -39
  16. data/misc/bug_report_template.rb +57 -0
  17. metadata +41 -182
  18. data/.gemtest +0 -0
  19. data/.rspec +0 -2
  20. data/CHANGELOG.rdoc +0 -480
  21. data/README.rdoc +0 -336
  22. data/app/views/kaminari/_first_page.html.erb +0 -11
  23. data/app/views/kaminari/_first_page.html.haml +0 -9
  24. data/app/views/kaminari/_first_page.html.slim +0 -10
  25. data/app/views/kaminari/_gap.html.erb +0 -8
  26. data/app/views/kaminari/_gap.html.haml +0 -8
  27. data/app/views/kaminari/_gap.html.slim +0 -9
  28. data/app/views/kaminari/_last_page.html.erb +0 -11
  29. data/app/views/kaminari/_last_page.html.haml +0 -9
  30. data/app/views/kaminari/_last_page.html.slim +0 -10
  31. data/app/views/kaminari/_next_page.html.erb +0 -11
  32. data/app/views/kaminari/_next_page.html.haml +0 -9
  33. data/app/views/kaminari/_next_page.html.slim +0 -10
  34. data/app/views/kaminari/_page.html.erb +0 -12
  35. data/app/views/kaminari/_page.html.haml +0 -10
  36. data/app/views/kaminari/_page.html.slim +0 -11
  37. data/app/views/kaminari/_paginator.html.erb +0 -23
  38. data/app/views/kaminari/_paginator.html.haml +0 -18
  39. data/app/views/kaminari/_paginator.html.slim +0 -19
  40. data/app/views/kaminari/_prev_page.html.erb +0 -11
  41. data/app/views/kaminari/_prev_page.html.haml +0 -9
  42. data/app/views/kaminari/_prev_page.html.slim +0 -10
  43. data/config/locales/kaminari.yml +0 -19
  44. data/gemfiles/active_record_30.gemfile +0 -28
  45. data/gemfiles/active_record_31.gemfile +0 -26
  46. data/gemfiles/active_record_32.gemfile +0 -25
  47. data/gemfiles/active_record_40.gemfile +0 -26
  48. data/gemfiles/data_mapper_12.gemfile +0 -32
  49. data/gemfiles/mongo_mapper.gemfile +0 -25
  50. data/gemfiles/mongoid_30.gemfile +0 -22
  51. data/gemfiles/mongoid_31.gemfile +0 -21
  52. data/gemfiles/mongoid_40.gemfile +0 -19
  53. data/gemfiles/mongoid_50.gemfile +0 -23
  54. data/gemfiles/sinatra_13.gemfile +0 -36
  55. data/gemfiles/sinatra_14.gemfile +0 -33
  56. data/lib/generators/kaminari/config_generator.rb +0 -16
  57. data/lib/generators/kaminari/templates/kaminari_config.rb +0 -10
  58. data/lib/generators/kaminari/views_generator.rb +0 -119
  59. data/lib/kaminari/config.rb +0 -51
  60. data/lib/kaminari/engine.rb +0 -4
  61. data/lib/kaminari/grape.rb +0 -4
  62. data/lib/kaminari/helpers/action_view_extension.rb +0 -132
  63. data/lib/kaminari/helpers/paginator.rb +0 -195
  64. data/lib/kaminari/helpers/sinatra_helpers.rb +0 -176
  65. data/lib/kaminari/helpers/tags.rb +0 -108
  66. data/lib/kaminari/hooks.rb +0 -53
  67. data/lib/kaminari/models/active_record_extension.rb +0 -22
  68. data/lib/kaminari/models/active_record_model_extension.rb +0 -22
  69. data/lib/kaminari/models/active_record_relation_methods.rb +0 -42
  70. data/lib/kaminari/models/array_extension.rb +0 -66
  71. data/lib/kaminari/models/configuration_methods.rb +0 -48
  72. data/lib/kaminari/models/data_mapper_collection_methods.rb +0 -19
  73. data/lib/kaminari/models/data_mapper_extension.rb +0 -51
  74. data/lib/kaminari/models/mongo_mapper_extension.rb +0 -18
  75. data/lib/kaminari/models/mongoid_criteria_methods.rb +0 -40
  76. data/lib/kaminari/models/mongoid_extension.rb +0 -26
  77. data/lib/kaminari/models/page_scope_methods.rb +0 -73
  78. data/lib/kaminari/models/plucky_criteria_methods.rb +0 -23
  79. data/lib/kaminari/railtie.rb +0 -7
  80. data/lib/kaminari/sinatra.rb +0 -7
  81. data/spec/config/config_spec.rb +0 -91
  82. data/spec/fake_app/active_record/config.rb +0 -3
  83. data/spec/fake_app/active_record/models.rb +0 -65
  84. data/spec/fake_app/data_mapper/config.rb +0 -1
  85. data/spec/fake_app/data_mapper/models.rb +0 -27
  86. data/spec/fake_app/mongo_mapper/config.rb +0 -2
  87. data/spec/fake_app/mongo_mapper/models.rb +0 -9
  88. data/spec/fake_app/mongoid/config.rb +0 -20
  89. data/spec/fake_app/mongoid/models.rb +0 -38
  90. data/spec/fake_app/rails_app.rb +0 -56
  91. data/spec/fake_app/sinatra_app.rb +0 -22
  92. data/spec/fake_app/views/alternative/kaminari/_first_page.html.erb +0 -1
  93. data/spec/fake_app/views/alternative/kaminari/_paginator.html.erb +0 -3
  94. data/spec/fake_app/views/kaminari/bootstrap/_page.html.erb +0 -1
  95. data/spec/fake_app/views/kaminari/bootstrap/_paginator.html.erb +0 -7
  96. data/spec/fake_gem.rb +0 -20
  97. data/spec/generators/views_generator_spec.rb +0 -18
  98. data/spec/helpers/action_view_extension_spec.rb +0 -318
  99. data/spec/helpers/helpers_spec.rb +0 -144
  100. data/spec/helpers/sinatra_helpers_spec.rb +0 -223
  101. data/spec/helpers/tags_spec.rb +0 -140
  102. data/spec/models/active_record/active_record_relation_methods_spec.rb +0 -77
  103. data/spec/models/active_record/inherited_spec.rb +0 -9
  104. data/spec/models/active_record/scopes_spec.rb +0 -265
  105. data/spec/models/array_spec.rb +0 -172
  106. data/spec/models/configuration_methods_spec.rb +0 -125
  107. data/spec/models/data_mapper/data_mapper_spec.rb +0 -205
  108. data/spec/models/mongo_mapper/mongo_mapper_spec.rb +0 -84
  109. data/spec/models/mongoid/mongoid_spec.rb +0 -228
  110. data/spec/requests/users_spec.rb +0 -53
  111. data/spec/spec_helper.rb +0 -34
  112. data/spec/spec_helper_for_sinatra.rb +0 -34
  113. data/spec/support/database_cleaner.rb +0 -19
  114. data/spec/support/matchers.rb +0 -52
@@ -1,195 +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
- ActiveSupport::Deprecation.warn 'num_pages is deprecated and will be removed in Kaminari 1.0. Please use total_pages instead.' if options.has_key? :num_pages
16
- options[:num_pages] ||= options[:total_pages]
17
-
18
- @window_options = {}.tap do |h|
19
- h[:window] = options.delete(:window) || options.delete(:inner_window) || Kaminari.config.window
20
- outer_window = options.delete(:outer_window) || Kaminari.config.outer_window
21
- h[:left] = options.delete(:left) || Kaminari.config.left
22
- h[:left] = outer_window if h[:left] == 0
23
- h[:right] = options.delete(:right) || Kaminari.config.right
24
- h[:right] = outer_window if h[:right] == 0
25
- end
26
- @template, @options = template, options
27
- @theme = @options[:theme]
28
- @views_prefix = @options[:views_prefix]
29
- @window_options.merge! @options
30
- @window_options[:current_page] = @options[:current_page] = PageProxy.new(@window_options, @options[:current_page], nil)
31
-
32
- @last = nil
33
- # initialize the output_buffer for Context
34
- @output_buffer = ActionView::OutputBuffer.new
35
- end
36
-
37
- # render given block as a view template
38
- def render(&block)
39
- instance_eval(&block) if @options[:total_pages] > 1
40
- @output_buffer
41
- end
42
-
43
- # enumerate each page providing PageProxy object as the block parameter
44
- # Because of performance reason, this doesn't actually enumerate all pages but pages that are seemingly relevant to the paginator.
45
- # "Relevant" pages are:
46
- # * pages inside the left outer window plus one for showing the gap tag
47
- # * pages inside the inner window plus one on the left plus one on the right for showing the gap tags
48
- # * pages inside the right outer window plus one for showing the gap tag
49
- def each_relevant_page
50
- return to_enum(:each_relevant_page) unless block_given?
51
-
52
- relevant_pages(@window_options).each do |page|
53
- yield PageProxy.new(@window_options, page, @last)
54
- end
55
- end
56
- alias each_page each_relevant_page
57
-
58
- def relevant_pages(options)
59
- left_window_plus_one = 1.upto(options[:left] + 1).to_a
60
- right_window_plus_one = (options[:total_pages] - options[:right]).upto(options[:total_pages]).to_a
61
- inside_window_plus_each_sides = (options[:current_page] - options[:window] - 1).upto(options[:current_page] + options[:window] + 1).to_a
62
-
63
- (left_window_plus_one + inside_window_plus_each_sides + right_window_plus_one).uniq.sort.reject {|x| (x < 1) || (x > options[:total_pages])}
64
- end
65
- private :relevant_pages
66
-
67
- def page_tag(page)
68
- @last = Page.new @template, @options.merge(:page => page)
69
- end
70
-
71
- %w[first_page prev_page next_page last_page gap].each do |tag|
72
- eval <<-DEF
73
- def #{tag}_tag
74
- @last = #{tag.classify}.new @template, @options
75
- end
76
- DEF
77
- end
78
-
79
- def to_s #:nodoc:
80
- subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}
81
-
82
- # There is a logging subscriber
83
- # and we don't want it to log render_partial
84
- # It is threadsafe, but might not repress logging
85
- # consistently in a high-load environment
86
- if subscriber
87
- unless defined? subscriber.render_partial_with_logging
88
- class << subscriber
89
- alias_method :render_partial_with_logging, :render_partial
90
- attr_accessor :render_without_logging
91
- # ugly hack to make a renderer where
92
- # we can turn logging on or off
93
- def render_partial(event)
94
- render_partial_with_logging(event) unless render_without_logging
95
- end
96
- end
97
- end
98
-
99
- subscriber.render_without_logging = true
100
- ret = super @window_options.merge :paginator => self
101
- subscriber.render_without_logging = false
102
-
103
- ret
104
- else
105
- super @window_options.merge :paginator => self
106
- end
107
- end
108
-
109
- # delegates view helper methods to @template
110
- def method_missing(name, *args, &block)
111
- @template.respond_to?(name) ? @template.send(name, *args, &block) : super
112
- end
113
- private :method_missing
114
-
115
- # Wraps a "page number" and provides some utility methods
116
- class PageProxy
117
- include Comparable
118
-
119
- def initialize(options, page, last) #:nodoc:
120
- @options, @page, @last = options, page, last
121
- end
122
-
123
- # the page number
124
- def number
125
- @page
126
- end
127
-
128
- # current page or not
129
- def current?
130
- @page == @options[:current_page]
131
- end
132
-
133
- # the first page or not
134
- def first?
135
- @page == 1
136
- end
137
-
138
- # the last page or not
139
- def last?
140
- @page == @options[:total_pages]
141
- end
142
-
143
- # the previous page or not
144
- def prev?
145
- @page == @options[:current_page] - 1
146
- end
147
-
148
- # the next page or not
149
- def next?
150
- @page == @options[:current_page] + 1
151
- end
152
-
153
- # within the left outer window or not
154
- def left_outer?
155
- @page <= @options[:left]
156
- end
157
-
158
- # within the right outer window or not
159
- def right_outer?
160
- @options[:total_pages] - @page < @options[:right]
161
- end
162
-
163
- # inside the inner window or not
164
- def inside_window?
165
- (@options[:current_page] - @page).abs <= @options[:window]
166
- end
167
-
168
- # The last rendered tag was "truncated" or not
169
- def was_truncated?
170
- @last.is_a? Gap
171
- end
172
-
173
- def to_i
174
- number
175
- end
176
-
177
- def to_s
178
- number.to_s
179
- end
180
-
181
- def +(other)
182
- to_i + other.to_i
183
- end
184
-
185
- def -(other)
186
- to_i - other.to_i
187
- end
188
-
189
- def <=>(other)
190
- to_i <=> other.to_i
191
- end
192
- end
193
- end
194
- end
195
- end
@@ -1,176 +0,0 @@
1
- require 'active_support/core_ext/object'
2
- require 'active_support/core_ext/string'
3
- require 'action_dispatch/http/mime_type'
4
-
5
- begin
6
-
7
- require 'padrino-helpers'
8
- module Kaminari::Helpers
9
- module SinatraHelpers
10
- class << self
11
- def registered(app)
12
- app.register Padrino::Helpers
13
- app.helpers HelperMethods
14
- @app = app
15
- end
16
-
17
- def view_paths
18
- @app.views
19
- end
20
-
21
- alias included registered
22
- end
23
-
24
- class ActionViewTemplateProxy
25
- include Padrino::Helpers::OutputHelpers
26
- include Padrino::Helpers::TagHelpers
27
- include Padrino::Helpers::AssetTagHelpers
28
- include Padrino::Helpers::FormatHelpers
29
- include Padrino::Helpers::TranslationHelpers
30
-
31
- def initialize(opts={})
32
- @current_path = opts[:current_path]
33
- @param_name = (opts[:param_name] || :page).to_sym
34
- @current_params = opts[:current_params]
35
- @current_params.delete(@param_name)
36
- end
37
-
38
- def render(*args)
39
- base = ActionView::Base.new.tap do |a|
40
- a.view_paths << SinatraHelpers.view_paths
41
- a.view_paths << File.expand_path('../../../../app/views', __FILE__)
42
- end
43
- base.render(*args)
44
- end
45
-
46
- def url_for(params)
47
- extra_params = {}
48
- if page = params[@param_name] and page != 1
49
- extra_params[@param_name] = page
50
- end
51
- query = @current_params.merge(extra_params)
52
- @current_path + (query.empty? ? '' : "?#{query.to_query}")
53
- end
54
-
55
- def link_to_unless(condition, name, options = {}, html_options = {}, &block)
56
- options = url_for(options) if options.is_a? Hash
57
- if condition
58
- if block_given?
59
- block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
60
- else
61
- name
62
- end
63
- else
64
- link_to(name, options, html_options)
65
- end
66
- end
67
-
68
- def params
69
- @current_params
70
- end
71
- end
72
-
73
- module HelperMethods
74
- # A helper that renders the pagination links - for Sinatra.
75
- #
76
- # <%= paginate @articles %>
77
- #
78
- # ==== Options
79
- # * <tt>:window</tt> - The "inner window" size (4 by default).
80
- # * <tt>:outer_window</tt> - The "outer window" size (0 by default).
81
- # * <tt>:left</tt> - The "left outer window" size (0 by default).
82
- # * <tt>:right</tt> - The "right outer window" size (0 by default).
83
- # * <tt>:params</tt> - url_for parameters for the links (:id, :locale, etc.)
84
- # * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
85
- # * <tt>:remote</tt> - Ajax? (false by default)
86
- # * <tt>:ANY_OTHER_VALUES</tt> - Any other hash key & values would be directly passed into each tag as :locals value.
87
- def paginate(scope, options = {}, &block)
88
- current_path = env['PATH_INFO'] rescue nil
89
- current_params = Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {}
90
- paginator = Kaminari::Helpers::Paginator.new(
91
- ActionViewTemplateProxy.new(:current_params => current_params, :current_path => current_path, :param_name => options[:param_name] || Kaminari.config.param_name),
92
- 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)
93
- )
94
- paginator.to_s
95
- end
96
-
97
- # A simple "Twitter like" pagination link that creates a link to the previous page.
98
- # Works on Sinatra.
99
- #
100
- # ==== Examples
101
- # Basic usage:
102
- #
103
- # <%= link_to_previous_page @items, 'Previous Page' %>
104
- #
105
- # Ajax:
106
- #
107
- # <%= link_to_previous_page @items, 'Previous Page', :remote => true %>
108
- #
109
- # By default, it renders nothing if there are no more results on the previous page.
110
- # You can customize this output by passing a parameter <tt>:placeholder</tt>.
111
- #
112
- # <%= link_to_previous_page @users, 'Previous Page', :placeholder => %{<span>At the Beginning</span>} %>
113
- #
114
- def link_to_previous_page(scope, name, options = {})
115
- params = options.delete(:params) || (Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {})
116
- param_name = options.delete(:param_name) || Kaminari.config.param_name
117
- placeholder = options.delete(:placeholder)
118
-
119
- unless scope.first_page?
120
- query = params.merge(param_name => scope.prev_page)
121
- link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'previous')
122
- else
123
- placeholder.to_s.html_safe
124
- end
125
- end
126
-
127
- # A simple "Twitter like" pagination link that creates a link to the next page.
128
- # Works on Sinatra.
129
- #
130
- # ==== Examples
131
- # Basic usage:
132
- #
133
- # <%= link_to_next_page @items, 'Next Page' %>
134
- #
135
- # Ajax:
136
- #
137
- # <%= link_to_next_page @items, 'Next Page', :remote => true %>
138
- #
139
- # By default, it renders nothing if there are no more results on the next page.
140
- # You can customize this output by passing a parameter <tt>:placeholder</tt>.
141
- #
142
- # <%= link_to_next_page @items, 'Next Page', :placeholder => %{<span>No More Pages</span>} %>
143
- #
144
- def link_to_next_page(scope, name, options = {})
145
- params = options.delete(:params) || (Rack::Utils.parse_query(env['QUERY_STRING']).symbolize_keys rescue {})
146
- param_name = options.delete(:param_name) || Kaminari.config.param_name
147
- placeholder = options.delete(:placeholder)
148
-
149
- unless scope.last_page?
150
- query = params.merge(param_name => scope.next_page)
151
- link_to name, env['PATH_INFO'] + (query.empty? ? '' : "?#{query.to_query}"), options.reverse_merge(:rel => 'next')
152
- else
153
- placeholder.to_s.html_safe
154
- end
155
- end
156
- end
157
- end
158
- end
159
-
160
- if defined? I18n
161
- I18n.load_path += Dir.glob(File.expand_path('../../../../config/locales/*.yml', __FILE__))
162
- end
163
-
164
- rescue LoadError
165
-
166
- $stderr.puts "[!]You should install `padrino-helpers' gem if you want to use kaminari's pagination helpers with Sinatra."
167
- $stderr.puts "[!]Kaminari::Helpers::SinatraHelper does nothing now..."
168
-
169
- module Kaminari::Helpers
170
- module SinatraHelper
171
- def self.registered(*)
172
- end
173
- end
174
- end
175
-
176
- end
@@ -1,108 +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) || Kaminari.config.param_name
19
- @theme = @options.delete(:theme)
20
- @views_prefix = @options.delete(:views_prefix)
21
- @params = template.params
22
- # @params in Rails 5 no longer inherits from Hash
23
- @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h)
24
- @params = @params.except(:script_name).merge(@options.delete(:params) || {})
25
- end
26
-
27
- def to_s(locals = {}) #:nodoc:
28
- @template.render :partial => partial_path, :locals => @options.merge(locals), :formats => [:html]
29
- end
30
-
31
- def page_url_for(page)
32
- @template.url_for @params.merge(@param_name => (page <= 1 ? nil : page), :only_path => true)
33
- end
34
-
35
- def partial_path
36
- [
37
- @views_prefix,
38
- "kaminari",
39
- @theme,
40
- self.class.name.demodulize.underscore
41
- ].compact.join("/")
42
- end
43
- end
44
-
45
- # Tag that contains a link
46
- module Link
47
- # target page number
48
- def page
49
- raise 'Override page with the actual page value to be a Page.'
50
- end
51
- # the link's href
52
- def url
53
- page_url_for page
54
- end
55
- def to_s(locals = {}) #:nodoc:
56
- super locals.merge(:url => url)
57
- end
58
- end
59
-
60
- # A page
61
- class Page < Tag
62
- include Link
63
- # target page number
64
- def page
65
- @options[:page]
66
- end
67
- def to_s(locals = {}) #:nodoc:
68
- super locals.merge(:page => page)
69
- end
70
- end
71
-
72
- # Link with page number that appears at the leftmost
73
- class FirstPage < Tag
74
- include Link
75
- def page #:nodoc:
76
- 1
77
- end
78
- end
79
-
80
- # Link with page number that appears at the rightmost
81
- class LastPage < Tag
82
- include Link
83
- def page #:nodoc:
84
- @options[:total_pages]
85
- end
86
- end
87
-
88
- # The "previous" page of the current page
89
- class PrevPage < Tag
90
- include Link
91
- def page #:nodoc:
92
- @options[:current_page] - 1
93
- end
94
- end
95
-
96
- # The "next" page of the current page
97
- class NextPage < Tag
98
- include Link
99
- def page #:nodoc:
100
- @options[:current_page] + 1
101
- end
102
- end
103
-
104
- # Non-link tag that stands for skipped pages...
105
- class Gap < Tag
106
- end
107
- end
108
- end
@@ -1,53 +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
- # data_mapper
10
- begin
11
- require 'kaminari/data_mapper'
12
- rescue LoadError
13
- begin; require 'data_mapper'; rescue LoadError; end
14
- if defined? ::DataMapper
15
- require 'dm-aggregates'
16
- require 'kaminari/models/data_mapper_extension'
17
- ::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection
18
- ::DataMapper::Model.append_extensions Kaminari::DataMapperExtension::Model
19
- # ::DataMapper::Model.send :extend, Kaminari::DataMapperExtension::Model
20
- end
21
- end
22
-
23
- ## mongoid
24
- begin
25
- require 'kaminari/mongoid'
26
- rescue LoadError
27
- begin; require 'mongoid'; rescue LoadError; end
28
- if defined? ::Mongoid
29
- ActiveSupport::Deprecation.warn 'Kaminari Mongoid support has been extracted to a separate gem, and will be removed in the next 1.0 release. Please bundle kaminari-mongoid gem.'
30
- require 'kaminari/models/mongoid_extension'
31
- ::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
32
- end
33
- end
34
-
35
- ## mongo_mapper
36
- ActiveSupport.on_load(:mongo_mapper) do
37
- begin
38
- require 'kaminari/mongo_mapper'
39
- rescue LoadError
40
- ActiveSupport::Deprecation.warn 'Kaminari MongoMapper support has been extracted to a separate gem, and will be removed in the next 1.0 release. Please bundle kaminari-mongo_mapper gem.'
41
- require 'kaminari/models/mongo_mapper_extension'
42
- ::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
43
- ::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
44
- end
45
- end
46
- require 'kaminari/models/array_extension'
47
-
48
- ActiveSupport.on_load(:action_view) do
49
- ::ActionView::Base.send :include, Kaminari::ActionViewExtension
50
- end
51
- end
52
- end
53
- 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
-
7
- module ClassMethods
8
- # Future subclasses will pick up the model extension
9
- def inherited(kls) #:nodoc:
10
- super
11
- kls.send(:include, Kaminari::ActiveRecordModelExtension) if kls.superclass == ::ActiveRecord::Base
12
- end
13
- end
14
-
15
- included do
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,22 +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
- eval <<-RUBY
13
- def self.#{Kaminari.config.page_method_name}(num = nil)
14
- limit(default_per_page).offset(default_per_page * ((num = num.to_i - 1) < 0 ? 0 : num)).extending do
15
- include Kaminari::ActiveRecordRelationMethods
16
- include Kaminari::PageScopeMethods
17
- end
18
- end
19
- RUBY
20
- end
21
- end
22
- end
@@ -1,42 +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(column_name = nil, options = {}) #:nodoc:
7
- limit_value && !options[:distinct] ? length : super(column_name, options)
8
- end
9
- end
10
-
11
- def entry_name
12
- model_name.human.downcase
13
- end
14
-
15
- def reset #:nodoc:
16
- @total_count = nil
17
- super
18
- end
19
-
20
- def total_count(column_name = :all, options = {}) #:nodoc:
21
- # #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
22
- @total_count ||= begin
23
- c = except(:offset, :limit, :order)
24
-
25
- # Remove includes only if they are irrelevant
26
- c = c.except(:includes) unless references_eager_loaded_tables?
27
-
28
- # Rails 4.1 removes the `options` argument from AR::Relation#count
29
- args = [column_name]
30
- args << options if ActiveRecord::VERSION::STRING < '4.1.0'
31
-
32
- # .group returns an OrderdHash that responds to #count
33
- c = c.count(*args)
34
- if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash)
35
- c.count
36
- else
37
- c.respond_to?(:count) ? c.count(*args) : c
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,66 +0,0 @@
1
- require 'active_support/core_ext/module'
2
- module Kaminari
3
- # Kind of Array that can paginate
4
- class PaginatableArray < Array
5
- include Kaminari::ConfigurationMethods::ClassMethods
6
-
7
- attr_internal_accessor :limit_value, :offset_value
8
-
9
- # ==== Options
10
- # * <tt>:limit</tt> - limit
11
- # * <tt>:offset</tt> - offset
12
- # * <tt>:total_count</tt> - total_count
13
- def initialize(original_array = [], options = {})
14
- @_original_array, @_limit_value, @_offset_value, @_total_count, @_padding = original_array, (options[:limit] || default_per_page).to_i, options[:offset].to_i, options[:total_count], options[:padding].to_i
15
-
16
- if options[:limit] && options[:offset]
17
- extend Kaminari::PageScopeMethods
18
- end
19
-
20
- if @_total_count.present? && @_total_count <= original_array.count
21
- original_array = original_array.first(@_total_count)[@_offset_value, @_limit_value]
22
- end
23
-
24
- if @_total_count.nil?
25
- original_array = original_array[@_offset_value, @_limit_value]
26
- end
27
-
28
- super(original_array || [])
29
- end
30
-
31
- def entry_name
32
- "entry"
33
- end
34
-
35
- # items at the specified "page"
36
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
37
- def #{Kaminari.config.page_method_name}(num = 1)
38
- offset(limit_value * ((num = num.to_i - 1) < 0 ? 0 : num))
39
- end
40
- RUBY
41
-
42
- # returns another chunk of the original array
43
- def limit(num)
44
- self.class.new @_original_array, :limit => num, :offset => @_offset_value, :total_count => @_total_count, :padding => @_padding
45
- end
46
-
47
- # total item numbers of the original array
48
- def total_count
49
- @_total_count || @_original_array.count
50
- end
51
-
52
- # returns another chunk of the original array
53
- def offset(num)
54
- self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count, :padding => @_padding
55
- end
56
- end
57
-
58
- # Wrap an Array object to make it paginatable
59
- # ==== Options
60
- # * <tt>:limit</tt> - limit
61
- # * <tt>:offset</tt> - offset
62
- # * <tt>:total_count</tt> - total_count
63
- def self.paginate_array(array, options = {})
64
- PaginatableArray.new array, options
65
- end
66
- end