kaminari 0.16.3 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +19 -0
  3. data/.gitignore +1 -1
  4. data/.travis.yml +82 -47
  5. data/CHANGELOG.md +636 -0
  6. data/CONTRIBUTING.md +22 -0
  7. data/Gemfile +2 -0
  8. data/README.md +573 -0
  9. data/Rakefile +33 -14
  10. data/gemfiles/active_record_41.gemfile +10 -6
  11. data/gemfiles/active_record_42.gemfile +10 -6
  12. data/gemfiles/active_record_50.gemfile +24 -0
  13. data/gemfiles/active_record_51.gemfile +24 -0
  14. data/gemfiles/active_record_52.gemfile +24 -0
  15. data/gemfiles/active_record_60.gemfile +26 -0
  16. data/gemfiles/active_record_edge.gemfile +15 -12
  17. data/kaminari.gemspec +23 -27
  18. data/lib/kaminari/version.rb +3 -1
  19. data/lib/kaminari.rb +4 -38
  20. data/misc/bug_report_template.rb +57 -0
  21. metadata +48 -186
  22. data/.gemtest +0 -0
  23. data/.rspec +0 -2
  24. data/CHANGELOG.rdoc +0 -453
  25. data/README.rdoc +0 -336
  26. data/app/views/kaminari/_first_page.html.erb +0 -11
  27. data/app/views/kaminari/_first_page.html.haml +0 -9
  28. data/app/views/kaminari/_first_page.html.slim +0 -10
  29. data/app/views/kaminari/_gap.html.erb +0 -8
  30. data/app/views/kaminari/_gap.html.haml +0 -8
  31. data/app/views/kaminari/_gap.html.slim +0 -9
  32. data/app/views/kaminari/_last_page.html.erb +0 -11
  33. data/app/views/kaminari/_last_page.html.haml +0 -9
  34. data/app/views/kaminari/_last_page.html.slim +0 -10
  35. data/app/views/kaminari/_next_page.html.erb +0 -11
  36. data/app/views/kaminari/_next_page.html.haml +0 -9
  37. data/app/views/kaminari/_next_page.html.slim +0 -10
  38. data/app/views/kaminari/_page.html.erb +0 -12
  39. data/app/views/kaminari/_page.html.haml +0 -10
  40. data/app/views/kaminari/_page.html.slim +0 -11
  41. data/app/views/kaminari/_paginator.html.erb +0 -23
  42. data/app/views/kaminari/_paginator.html.haml +0 -18
  43. data/app/views/kaminari/_paginator.html.slim +0 -19
  44. data/app/views/kaminari/_prev_page.html.erb +0 -11
  45. data/app/views/kaminari/_prev_page.html.haml +0 -9
  46. data/app/views/kaminari/_prev_page.html.slim +0 -10
  47. data/config/locales/kaminari.yml +0 -19
  48. data/gemfiles/active_record_30.gemfile +0 -30
  49. data/gemfiles/active_record_31.gemfile +0 -33
  50. data/gemfiles/active_record_32.gemfile +0 -33
  51. data/gemfiles/active_record_40.gemfile +0 -25
  52. data/gemfiles/data_mapper_12.gemfile +0 -39
  53. data/gemfiles/mongo_mapper.gemfile +0 -31
  54. data/gemfiles/mongoid_24.gemfile +0 -25
  55. data/gemfiles/mongoid_30.gemfile +0 -21
  56. data/gemfiles/mongoid_31.gemfile +0 -20
  57. data/gemfiles/mongoid_40.gemfile +0 -18
  58. data/gemfiles/sinatra_13.gemfile +0 -43
  59. data/gemfiles/sinatra_14.gemfile +0 -43
  60. data/lib/generators/kaminari/config_generator.rb +0 -16
  61. data/lib/generators/kaminari/templates/kaminari_config.rb +0 -10
  62. data/lib/generators/kaminari/views_generator.rb +0 -119
  63. data/lib/kaminari/config.rb +0 -51
  64. data/lib/kaminari/engine.rb +0 -4
  65. data/lib/kaminari/grape.rb +0 -4
  66. data/lib/kaminari/helpers/action_view_extension.rb +0 -132
  67. data/lib/kaminari/helpers/paginator.rb +0 -195
  68. data/lib/kaminari/helpers/sinatra_helpers.rb +0 -176
  69. data/lib/kaminari/helpers/tags.rb +0 -105
  70. data/lib/kaminari/hooks.rb +0 -36
  71. data/lib/kaminari/models/active_record_extension.rb +0 -22
  72. data/lib/kaminari/models/active_record_model_extension.rb +0 -22
  73. data/lib/kaminari/models/active_record_relation_methods.rb +0 -42
  74. data/lib/kaminari/models/array_extension.rb +0 -66
  75. data/lib/kaminari/models/configuration_methods.rb +0 -48
  76. data/lib/kaminari/models/data_mapper_collection_methods.rb +0 -19
  77. data/lib/kaminari/models/data_mapper_extension.rb +0 -51
  78. data/lib/kaminari/models/mongo_mapper_extension.rb +0 -18
  79. data/lib/kaminari/models/mongoid_criteria_methods.rb +0 -40
  80. data/lib/kaminari/models/mongoid_extension.rb +0 -26
  81. data/lib/kaminari/models/page_scope_methods.rb +0 -70
  82. data/lib/kaminari/models/plucky_criteria_methods.rb +0 -23
  83. data/lib/kaminari/railtie.rb +0 -7
  84. data/lib/kaminari/sinatra.rb +0 -5
  85. data/spec/config/config_spec.rb +0 -91
  86. data/spec/fake_app/active_record/config.rb +0 -3
  87. data/spec/fake_app/active_record/models.rb +0 -65
  88. data/spec/fake_app/data_mapper/config.rb +0 -1
  89. data/spec/fake_app/data_mapper/models.rb +0 -27
  90. data/spec/fake_app/mongo_mapper/config.rb +0 -2
  91. data/spec/fake_app/mongo_mapper/models.rb +0 -9
  92. data/spec/fake_app/mongoid/config.rb +0 -18
  93. data/spec/fake_app/mongoid/models.rb +0 -38
  94. data/spec/fake_app/rails_app.rb +0 -56
  95. data/spec/fake_app/sinatra_app.rb +0 -22
  96. data/spec/fake_app/views/alternative/kaminari/_first_page.html.erb +0 -1
  97. data/spec/fake_app/views/alternative/kaminari/_paginator.html.erb +0 -3
  98. data/spec/fake_app/views/kaminari/bootstrap/_page.html.erb +0 -1
  99. data/spec/fake_app/views/kaminari/bootstrap/_paginator.html.erb +0 -7
  100. data/spec/fake_gem.rb +0 -20
  101. data/spec/generators/views_generator_spec.rb +0 -18
  102. data/spec/helpers/action_view_extension_spec.rb +0 -306
  103. data/spec/helpers/helpers_spec.rb +0 -144
  104. data/spec/helpers/sinatra_helpers_spec.rb +0 -223
  105. data/spec/helpers/tags_spec.rb +0 -140
  106. data/spec/models/active_record/active_record_relation_methods_spec.rb +0 -77
  107. data/spec/models/active_record/inherited_spec.rb +0 -9
  108. data/spec/models/active_record/scopes_spec.rb +0 -265
  109. data/spec/models/array_spec.rb +0 -172
  110. data/spec/models/configuration_methods_spec.rb +0 -125
  111. data/spec/models/data_mapper/data_mapper_spec.rb +0 -205
  112. data/spec/models/mongo_mapper/mongo_mapper_spec.rb +0 -84
  113. data/spec/models/mongoid/mongoid_spec.rb +0 -228
  114. data/spec/requests/users_spec.rb +0 -53
  115. data/spec/spec_helper.rb +0 -34
  116. data/spec/spec_helper_for_sinatra.rb +0 -34
  117. data/spec/support/database_cleaner.rb +0 -19
  118. data/spec/support/matchers.rb +0 -52
@@ -1,132 +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
- options[:total_pages] ||= options[:num_pages] || scope.total_pages
19
-
20
- paginator = Kaminari::Helpers::Paginator.new(self, options.reverse_merge(:current_page => scope.current_page, :per_page => scope.limit_value, :remote => false))
21
- paginator.to_s
22
- end
23
-
24
- # A simple "Twitter like" pagination link that creates a link to the previous page.
25
- #
26
- # ==== Examples
27
- # Basic usage:
28
- #
29
- # <%= link_to_previous_page @items, 'Previous Page' %>
30
- #
31
- # Ajax:
32
- #
33
- # <%= link_to_previous_page @items, 'Previous Page', :remote => true %>
34
- #
35
- # By default, it renders nothing if there are no more results on the previous page.
36
- # You can customize this output by passing a block.
37
- #
38
- # <%= link_to_previous_page @users, 'Previous Page' do %>
39
- # <span>At the Beginning</span>
40
- # <% end %>
41
- def link_to_previous_page(scope, name, options = {}, &block)
42
- params = options.delete(:params) || {}
43
- param_name = options.delete(:param_name) || Kaminari.config.param_name
44
- link_to_unless scope.first_page?, name, params.merge(param_name => scope.prev_page), options.reverse_merge(:rel => 'previous') do
45
- block.call if block
46
- end
47
- end
48
-
49
- # A simple "Twitter like" pagination link that creates a link to the next page.
50
- #
51
- # ==== Examples
52
- # Basic usage:
53
- #
54
- # <%= link_to_next_page @items, 'Next Page' %>
55
- #
56
- # Ajax:
57
- #
58
- # <%= link_to_next_page @items, 'Next Page', :remote => true %>
59
- #
60
- # By default, it renders nothing if there are no more results on the next page.
61
- # You can customize this output by passing a block.
62
- #
63
- # <%= link_to_next_page @users, 'Next Page' do %>
64
- # <span>No More Pages</span>
65
- # <% end %>
66
- def link_to_next_page(scope, name, options = {}, &block)
67
- params = options.delete(:params) || {}
68
- param_name = options.delete(:param_name) || Kaminari.config.param_name
69
- link_to_unless scope.last_page?, name, params.merge(param_name => scope.next_page), options.reverse_merge(:rel => 'next') do
70
- block.call if block
71
- end
72
- end
73
-
74
- # Renders a helpful message with numbers of displayed vs. total entries.
75
- # Ported from mislav/will_paginate
76
- #
77
- # ==== Examples
78
- # Basic usage:
79
- #
80
- # <%= page_entries_info @posts %>
81
- # #-> Displaying posts 6 - 10 of 26 in total
82
- #
83
- # By default, the message will use the humanized class name of objects
84
- # in collection: for instance, "project types" for ProjectType models.
85
- # The namespace will be cutted out and only the last name will be used.
86
- # Override this with the <tt>:entry_name</tt> parameter:
87
- #
88
- # <%= page_entries_info @posts, :entry_name => 'item' %>
89
- # #-> Displaying items 6 - 10 of 26 in total
90
- def page_entries_info(collection, options = {})
91
- entry_name = options[:entry_name] || collection.entry_name
92
- entry_name = entry_name.pluralize unless collection.total_count == 1
93
-
94
- if collection.total_pages < 2
95
- t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)
96
- else
97
- first = collection.offset_value + 1
98
- last = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value
99
- t('helpers.page_entries_info.more_pages.display_entries', :entry_name => entry_name, :first => first, :last => last, :total => collection.total_count)
100
- end.html_safe
101
- end
102
-
103
- # Renders rel="next" and rel="prev" links to be used in the head.
104
- #
105
- # ==== Examples
106
- # Basic usage:
107
- #
108
- # In head:
109
- # <head>
110
- # <title>My Website</title>
111
- # <%= yield :head %>
112
- # </head>
113
- #
114
- # Somewhere in body:
115
- # <% content_for :head do %>
116
- # <%= rel_next_prev_link_tags @items %>
117
- # <% end %>
118
- #
119
- # #-> <link rel="next" href="/items/page/3" /><link rel="prev" href="/items/page/1" />
120
- #
121
- def rel_next_prev_link_tags(scope, options = {})
122
- params = options.delete(:params) || {}
123
- param_name = options.delete(:param_name) || Kaminari.config.param_name
124
-
125
- output = ""
126
- output << '<link rel="next" href="' + url_for(params.merge(param_name => scope.next_page, :only_path => true)) + '"/>' if scope.next_page
127
- output << '<link rel="prev" href="' + url_for(params.merge(param_name => scope.prev_page, :only_path => true)) + '"/>' if scope.prev_page
128
-
129
- output.html_safe
130
- end
131
- end
132
- end
@@ -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
- #FIXME for compatibility. remove num_pages at some time in the future
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,105 +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 = @options[:params] ? template.params.merge(@options.delete :params) : template.params
22
- end
23
-
24
- def to_s(locals = {}) #:nodoc:
25
- @template.render :partial => partial_path, :locals => @options.merge(locals), :formats => [:html]
26
- end
27
-
28
- def page_url_for(page)
29
- @template.url_for @params.merge(@param_name => (page <= 1 ? nil : page), :only_path => true)
30
- end
31
-
32
- def partial_path
33
- [
34
- @views_prefix,
35
- "kaminari",
36
- @theme,
37
- self.class.name.demodulize.underscore
38
- ].compact.join("/")
39
- end
40
- end
41
-
42
- # Tag that contains a link
43
- module Link
44
- # target page number
45
- def page
46
- raise 'Override page with the actual page value to be a Page.'
47
- end
48
- # the link's href
49
- def url
50
- page_url_for page
51
- end
52
- def to_s(locals = {}) #:nodoc:
53
- super locals.merge(:url => url)
54
- end
55
- end
56
-
57
- # A page
58
- class Page < Tag
59
- include Link
60
- # target page number
61
- def page
62
- @options[:page]
63
- end
64
- def to_s(locals = {}) #:nodoc:
65
- super locals.merge(:page => page)
66
- end
67
- end
68
-
69
- # Link with page number that appears at the leftmost
70
- class FirstPage < Tag
71
- include Link
72
- def page #:nodoc:
73
- 1
74
- end
75
- end
76
-
77
- # Link with page number that appears at the rightmost
78
- class LastPage < Tag
79
- include Link
80
- def page #:nodoc:
81
- @options[:total_pages]
82
- end
83
- end
84
-
85
- # The "previous" page of the current page
86
- class PrevPage < Tag
87
- include Link
88
- def page #:nodoc:
89
- @options[:current_page] - 1
90
- end
91
- end
92
-
93
- # The "next" page of the current page
94
- class NextPage < Tag
95
- include Link
96
- def page #:nodoc:
97
- @options[:current_page] + 1
98
- end
99
- end
100
-
101
- # Non-link tag that stands for skipped pages...
102
- class Gap < Tag
103
- end
104
- end
105
- end
@@ -1,36 +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::Document.send :include, Kaminari::MongoidExtension::Document
22
- end
23
-
24
- ActiveSupport.on_load(:mongo_mapper) do
25
- require 'kaminari/models/mongo_mapper_extension'
26
- ::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
27
- ::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
28
- end
29
- require 'kaminari/models/array_extension'
30
-
31
- ActiveSupport.on_load(:action_view) do
32
- ::ActionView::Base.send :include, Kaminari::ActionViewExtension
33
- end
34
- end
35
- end
36
- 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