kaminari-core 1.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README.md +31 -0
  4. data/app/views/kaminari/_first_page.html.erb +11 -0
  5. data/app/views/kaminari/_first_page.html.haml +9 -0
  6. data/app/views/kaminari/_first_page.html.slim +10 -0
  7. data/app/views/kaminari/_gap.html.erb +8 -0
  8. data/app/views/kaminari/_gap.html.haml +8 -0
  9. data/app/views/kaminari/_gap.html.slim +9 -0
  10. data/app/views/kaminari/_last_page.html.erb +11 -0
  11. data/app/views/kaminari/_last_page.html.haml +9 -0
  12. data/app/views/kaminari/_last_page.html.slim +10 -0
  13. data/app/views/kaminari/_next_page.html.erb +11 -0
  14. data/app/views/kaminari/_next_page.html.haml +9 -0
  15. data/app/views/kaminari/_next_page.html.slim +10 -0
  16. data/app/views/kaminari/_page.html.erb +12 -0
  17. data/app/views/kaminari/_page.html.haml +10 -0
  18. data/app/views/kaminari/_page.html.slim +11 -0
  19. data/app/views/kaminari/_paginator.html.erb +25 -0
  20. data/app/views/kaminari/_paginator.html.haml +18 -0
  21. data/app/views/kaminari/_paginator.html.slim +19 -0
  22. data/app/views/kaminari/_prev_page.html.erb +11 -0
  23. data/app/views/kaminari/_prev_page.html.haml +9 -0
  24. data/app/views/kaminari/_prev_page.html.slim +10 -0
  25. data/config/locales/kaminari.yml +23 -0
  26. data/kaminari-core.gemspec +23 -0
  27. data/lib/generators/kaminari/config_generator.rb +18 -0
  28. data/lib/generators/kaminari/templates/kaminari_config.rb +12 -0
  29. data/lib/generators/kaminari/views_generator.rb +134 -0
  30. data/lib/kaminari/config.rb +28 -0
  31. data/lib/kaminari/core.rb +23 -0
  32. data/lib/kaminari/core/version.rb +6 -0
  33. data/lib/kaminari/engine.rb +5 -0
  34. data/lib/kaminari/exceptions.rb +4 -0
  35. data/lib/kaminari/helpers/helper_methods.rb +169 -0
  36. data/lib/kaminari/helpers/paginator.rb +189 -0
  37. data/lib/kaminari/helpers/tags.rb +137 -0
  38. data/lib/kaminari/models/array_extension.rb +72 -0
  39. data/lib/kaminari/models/configuration_methods.rb +57 -0
  40. data/lib/kaminari/models/page_scope_methods.rb +80 -0
  41. data/lib/kaminari/railtie.rb +8 -0
  42. metadata +113 -0
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/core_ext/module'
3
+ module Kaminari
4
+ # Kind of Array that can paginate
5
+ class PaginatableArray < Array
6
+ include Kaminari::ConfigurationMethods::ClassMethods
7
+
8
+ ENTRY = 'entry'.freeze
9
+
10
+ attr_internal_accessor :limit_value, :offset_value
11
+
12
+ # ==== Options
13
+ # * <tt>:limit</tt> - limit
14
+ # * <tt>:offset</tt> - offset
15
+ # * <tt>:total_count</tt> - total_count
16
+ # * <tt>:padding</tt> - padding
17
+ def initialize(original_array = [], limit: nil, offset: nil, total_count: nil, padding: nil)
18
+ @_original_array, @_limit_value, @_offset_value, @_total_count, @_padding = original_array, (limit || default_per_page).to_i, offset.to_i, total_count, padding.to_i
19
+
20
+ if limit && offset
21
+ extend Kaminari::PageScopeMethods
22
+ end
23
+
24
+ if @_total_count && (@_total_count <= original_array.count)
25
+ original_array = original_array.first(@_total_count)[@_offset_value, @_limit_value]
26
+ end
27
+
28
+ unless @_total_count
29
+ original_array = original_array[@_offset_value, @_limit_value]
30
+ end
31
+
32
+ super(original_array || [])
33
+ end
34
+
35
+ # Used for page_entry_info
36
+ def entry_name(options = {})
37
+ I18n.t('helpers.page_entries_info.entry', options.reverse_merge(default: ENTRY.pluralize(options[:count])))
38
+ end
39
+
40
+ # items at the specified "page"
41
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
42
+ def #{Kaminari.config.page_method_name}(num = 1)
43
+ offset(limit_value * ((num = num.to_i - 1) < 0 ? 0 : num))
44
+ end
45
+ RUBY
46
+
47
+ # returns another chunk of the original array
48
+ def limit(num)
49
+ self.class.new @_original_array, limit: num, offset: @_offset_value, total_count: @_total_count, padding: @_padding
50
+ end
51
+
52
+ # total item numbers of the original array
53
+ def total_count
54
+ @_total_count || @_original_array.length
55
+ end
56
+
57
+ # returns another chunk of the original array
58
+ def offset(num)
59
+ self.class.new @_original_array, limit: @_limit_value, offset: num, total_count: @_total_count, padding: @_padding
60
+ end
61
+ end
62
+
63
+ # Wrap an Array object to make it paginatable
64
+ # ==== Options
65
+ # * <tt>:limit</tt> - limit
66
+ # * <tt>:offset</tt> - offset
67
+ # * <tt>:total_count</tt> - total_count
68
+ # * <tt>:padding</tt> - padding
69
+ def self.paginate_array(array, limit: nil, offset: nil, total_count: nil, padding: nil)
70
+ PaginatableArray.new array, limit: limit, offset: offset, total_count: total_count, padding: padding
71
+ end
72
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ module Kaminari
3
+ module ConfigurationMethods #:nodoc:
4
+ extend ActiveSupport::Concern
5
+ module ClassMethods #:nodoc:
6
+ # Overrides the default +per_page+ value per model
7
+ # class Article < ActiveRecord::Base
8
+ # paginates_per 10
9
+ # end
10
+ def paginates_per(val)
11
+ @_default_per_page = val
12
+ end
13
+
14
+ # This model's default +per_page+ value
15
+ # returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt>
16
+ def default_per_page
17
+ (defined?(@_default_per_page) && @_default_per_page) || Kaminari.config.default_per_page
18
+ end
19
+
20
+ # Overrides the max +per_page+ value per model
21
+ # class Article < ActiveRecord::Base
22
+ # max_paginates_per 100
23
+ # end
24
+ def max_paginates_per(val)
25
+ @_max_per_page = val
26
+ end
27
+
28
+ # This model's max +per_page+ value
29
+ # returns +max_per_page+ value unless explicitly overridden via <tt>max_paginates_per</tt>
30
+ def max_per_page
31
+ (defined?(@_max_per_page) && @_max_per_page) || Kaminari.config.max_per_page
32
+ end
33
+
34
+ # Overrides the max_pages value per model when a value is given
35
+ # class Article < ActiveRecord::Base
36
+ # max_pages 100
37
+ # end
38
+ #
39
+ # Also returns this model's max_pages value (globally configured
40
+ # +max_pages+ value unless explicitly overridden) when no value is given
41
+ def max_pages(val = :none)
42
+ if val == :none
43
+ # getter
44
+ (defined?(@_max_pages) && @_max_pages) || Kaminari.config.max_pages
45
+ else
46
+ # setter
47
+ @_max_pages = val
48
+ end
49
+ end
50
+
51
+ def max_pages_per(val)
52
+ ActiveSupport::Deprecation.warn 'max_pages_per is deprecated. Use max_pages instead.', caller_locations(2)
53
+ max_pages val
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+ module Kaminari
3
+ module PageScopeMethods
4
+ # Specify the <tt>per_page</tt> value for the preceding <tt>page</tt> scope
5
+ # Model.page(3).per(10)
6
+ def per(num, max_per_page: nil)
7
+ max_per_page ||= ((defined?(@_max_per_page) && @_max_per_page) || self.max_per_page)
8
+ @_per = num
9
+ if num.nil? && max_per_page
10
+ limit(max_per_page).offset(offset_value / limit_value * max_per_page)
11
+ elsif (n = num.to_i) < 0 || !(/^\d/ =~ num.to_s)
12
+ self
13
+ elsif n.zero?
14
+ limit(n)
15
+ elsif max_per_page && max_per_page < n
16
+ limit(max_per_page).offset(offset_value / limit_value * max_per_page)
17
+ else
18
+ limit(n).offset(offset_value / limit_value * n)
19
+ end
20
+ end
21
+
22
+ def max_paginates_per(new_max_per_page)
23
+ @_max_per_page = new_max_per_page
24
+ per ((defined?(@_per) && @_per) || default_per_page), max_per_page: new_max_per_page
25
+ end
26
+
27
+ def padding(num)
28
+ @_padding = num
29
+ offset(offset_value + num.to_i)
30
+ end
31
+
32
+ # Total number of pages
33
+ def total_pages
34
+ count_without_padding = total_count
35
+ count_without_padding -= @_padding if defined?(@_padding) && @_padding
36
+ count_without_padding = 0 if count_without_padding < 0
37
+
38
+ total_pages_count = (count_without_padding.to_f / limit_value).ceil
39
+ max_pages && (max_pages < total_pages_count) ? max_pages : total_pages_count
40
+ rescue FloatDomainError
41
+ raise ZeroPerPageOperation, "The number of total pages was incalculable. Perhaps you called .per(0)?"
42
+ end
43
+
44
+ # Current page number
45
+ def current_page
46
+ offset_without_padding = offset_value
47
+ offset_without_padding -= @_padding if defined?(@_padding) && @_padding
48
+ offset_without_padding = 0 if offset_without_padding < 0
49
+
50
+ (offset_without_padding / limit_value) + 1
51
+ rescue ZeroDivisionError
52
+ raise ZeroPerPageOperation, "Current page was incalculable. Perhaps you called .per(0)?"
53
+ end
54
+
55
+ # Next page number in the collection
56
+ def next_page
57
+ current_page + 1 unless last_page? || out_of_range?
58
+ end
59
+
60
+ # Previous page number in the collection
61
+ def prev_page
62
+ current_page - 1 unless first_page? || out_of_range?
63
+ end
64
+
65
+ # First page of the collection?
66
+ def first_page?
67
+ current_page == 1
68
+ end
69
+
70
+ # Last page of the collection?
71
+ def last_page?
72
+ current_page == total_pages
73
+ end
74
+
75
+ # Out of range of the collection?
76
+ def out_of_range?
77
+ current_page > total_pages
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ module Kaminari
3
+ class Railtie < ::Rails::Railtie #:nodoc:
4
+ # Doesn't actually do anything. Just keeping this hook point, mainly for compatibility
5
+ initializer 'kaminari' do
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kaminari-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta2
5
+ platform: ruby
6
+ authors:
7
+ - Akira Matsuda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: kaminari-core includes pagination logic independent from ORMs and view
42
+ libraries
43
+ email:
44
+ - ronnie@dio.jp
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - app/views/kaminari/_first_page.html.erb
52
+ - app/views/kaminari/_first_page.html.haml
53
+ - app/views/kaminari/_first_page.html.slim
54
+ - app/views/kaminari/_gap.html.erb
55
+ - app/views/kaminari/_gap.html.haml
56
+ - app/views/kaminari/_gap.html.slim
57
+ - app/views/kaminari/_last_page.html.erb
58
+ - app/views/kaminari/_last_page.html.haml
59
+ - app/views/kaminari/_last_page.html.slim
60
+ - app/views/kaminari/_next_page.html.erb
61
+ - app/views/kaminari/_next_page.html.haml
62
+ - app/views/kaminari/_next_page.html.slim
63
+ - app/views/kaminari/_page.html.erb
64
+ - app/views/kaminari/_page.html.haml
65
+ - app/views/kaminari/_page.html.slim
66
+ - app/views/kaminari/_paginator.html.erb
67
+ - app/views/kaminari/_paginator.html.haml
68
+ - app/views/kaminari/_paginator.html.slim
69
+ - app/views/kaminari/_prev_page.html.erb
70
+ - app/views/kaminari/_prev_page.html.haml
71
+ - app/views/kaminari/_prev_page.html.slim
72
+ - config/locales/kaminari.yml
73
+ - kaminari-core.gemspec
74
+ - lib/generators/kaminari/config_generator.rb
75
+ - lib/generators/kaminari/templates/kaminari_config.rb
76
+ - lib/generators/kaminari/views_generator.rb
77
+ - lib/kaminari/config.rb
78
+ - lib/kaminari/core.rb
79
+ - lib/kaminari/core/version.rb
80
+ - lib/kaminari/engine.rb
81
+ - lib/kaminari/exceptions.rb
82
+ - lib/kaminari/helpers/helper_methods.rb
83
+ - lib/kaminari/helpers/paginator.rb
84
+ - lib/kaminari/helpers/tags.rb
85
+ - lib/kaminari/models/array_extension.rb
86
+ - lib/kaminari/models/configuration_methods.rb
87
+ - lib/kaminari/models/page_scope_methods.rb
88
+ - lib/kaminari/railtie.rb
89
+ homepage: https://github.com/amatsuda/kaminari
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">"
105
+ - !ruby/object:Gem::Version
106
+ version: 1.3.1
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.8
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Kaminari's core pagination library
113
+ test_files: []