will_paginate_ui_kit 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '035966066e16c06ac70604a9d2f80d4d685915e6'
4
+ data.tar.gz: c3e50e10b73bfea1fd5fe045eff829cc4ce00c40
5
+ SHA512:
6
+ metadata.gz: 916d3223f32548b51d8f01072de5a8a15f480dc5ac1ecf5b8a289a40f6081e58a087341af9f7aa9d531548c95712a83c23e881ab7820c0f6743e655a67b10738
7
+ data.tar.gz: 58a8b2eb3e324a00d6dc9826a6e4b77cbe9510ecdfc3487b2196d7fbbbef06f1de845653a5fe3d1ba5cec5fa057879f6272093f29e61f90f8f1e0c1891f01132
@@ -0,0 +1,9 @@
1
+ # You will paginate!
2
+ module WillPaginateUiKit
3
+ end
4
+
5
+ if defined?(Rails::Railtie)
6
+ require 'will_paginate_ui_kit/railtie'
7
+ elsif defined?(Rails::Initializer)
8
+ raise "will_paginate_ui_kit 3.0 is not compatible with Rails 2.3 or older"
9
+ end
@@ -0,0 +1,9 @@
1
+ module WillPaginateUiKit
2
+ class Railtie < Rails::Railtie
3
+ initializer "will_paginate_ui_kit" do |app|
4
+ ActiveSupport.on_load :action_view do
5
+ require 'will_paginate_ui_kit/view_helpers/ui_kit_link_renderer'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,177 @@
1
+ class UiKitLinkRenderer < WillPaginate::ViewHelpers::LinkRendererBase
2
+
3
+ GET_PARAMS_BLACKLIST = [:script_name, :original_script_name]
4
+
5
+ # * +collection+ is a WillPaginate::Collection instance or any other object
6
+ # that conforms to that API
7
+ # * +options+ are forwarded from +will_paginate+ view helper
8
+ # * +template+ is the reference to the template being rendered
9
+ def prepare(collection, options, template)
10
+ super(collection, options)
11
+ @options[:class] = 'uk-pagination'
12
+ @template = template
13
+ @container_attributes = @base_url_params = nil
14
+ end
15
+
16
+ # Process it! This method returns the complete HTML string which contains
17
+ # pagination links. Feel free to subclass LinkRenderer and change this
18
+ # method as you see fit.
19
+ def to_html
20
+ html = pagination.map do |item|
21
+ item.is_a?(Integer) ?
22
+ page_number(item) :
23
+ send(item)
24
+ end.join(@options[:link_separator])
25
+
26
+ @options[:container] ? html_container(html) : html
27
+ end
28
+
29
+ # Returns the subset of +options+ this instance was initialized with that
30
+ # represent HTML attributes for the container element of pagination links.
31
+ def container_attributes
32
+ @container_attributes ||= @options.except(*(WillPaginate::ViewHelpers.pagination_options.keys + [:renderer] - [:class]))
33
+ end
34
+
35
+ def default_url_params
36
+ {}
37
+ end
38
+
39
+ def page_number(page)
40
+ if page == current_page
41
+ tag(:li, nil, class: 'uk-active') do
42
+ tag(:span, page)
43
+ end
44
+ else
45
+ tag(:li) do
46
+ tag(:a, page, href: url(page), rel: rel_value(page))
47
+ end
48
+ end
49
+ end
50
+
51
+ def gap
52
+ text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
53
+ tag(:li) do
54
+ tag(:span, text)
55
+ end
56
+ end
57
+
58
+ def previous_page
59
+ num = @collection.current_page > 1 && @collection.current_page - 1
60
+ previous_or_next_page(num, 'uk-pagination-previous')
61
+ end
62
+
63
+ def next_page
64
+ num = @collection.current_page < total_pages && @collection.current_page + 1
65
+ previous_or_next_page(num, 'uk-pagination-next')
66
+ end
67
+
68
+ def previous_or_next_page(page, attribute)
69
+ if page
70
+ tag(:li, nil) do
71
+ tag(:a, nil, href: url(page), rel: rel_value(page)) do
72
+ tag(:span, '', attribute => '')
73
+ end
74
+ end
75
+ else
76
+ tag(:li, nil, class: 'uk-disabled') do
77
+ tag(:a, nil, href: '#') do
78
+ tag(:span, '', attribute => '')
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ def html_container(html)
85
+ tag(:ul, html, container_attributes)
86
+ end
87
+
88
+ protected
89
+
90
+ def url(page)
91
+ @base_url_params ||= begin
92
+ url_params = merge_get_params(default_url_params)
93
+ url_params[:only_path] = true
94
+ merge_optional_params(url_params)
95
+ end
96
+
97
+ url_params = @base_url_params.dup
98
+ add_current_page_param(url_params, page)
99
+
100
+ @template.url_for(url_params)
101
+ end
102
+
103
+ def merge_get_params(url_params)
104
+ if @template.respond_to? :request and @template.request and @template.request.get?
105
+ symbolized_update(url_params, @template.params, GET_PARAMS_BLACKLIST)
106
+ end
107
+ url_params
108
+ end
109
+
110
+ def merge_optional_params(url_params)
111
+ symbolized_update(url_params, @options[:params]) if @options[:params]
112
+ url_params
113
+ end
114
+
115
+ def add_current_page_param(url_params, page)
116
+ unless param_name.index(/[^\w-]/)
117
+ url_params[param_name.to_sym] = page
118
+ else
119
+ page_param = parse_query_parameters("#{param_name}=#{page}")
120
+ symbolized_update(url_params, page_param)
121
+ end
122
+ end
123
+
124
+ private
125
+
126
+ def parse_query_parameters(params)
127
+ Rack::Utils.parse_nested_query(params)
128
+ end
129
+
130
+ def param_name
131
+ @options[:param_name].to_s
132
+ end
133
+
134
+ def link(text, target, attributes = {})
135
+ if target.is_a?(Integer)
136
+ attributes[:rel] = rel_value(target)
137
+ target = url(target)
138
+ end
139
+ attributes[:href] = target
140
+ tag(:a, text, attributes)
141
+ end
142
+
143
+ def tag(name, value = '', attributes = {}, &block)
144
+ if block_given?
145
+ value = yield
146
+ end
147
+ string_attributes = attributes.inject('') do |attrs, pair|
148
+ unless pair.last.nil?
149
+ attrs << %( #{pair.first}="#{CGI::escapeHTML(pair.last.to_s)}")
150
+ end
151
+ attrs
152
+ end
153
+ "<#{name}#{string_attributes}>#{value}</#{name}>"
154
+ end
155
+
156
+ def rel_value(page)
157
+ case page
158
+ when @collection.current_page - 1; 'prev'
159
+ when @collection.current_page + 1; 'next'
160
+ end
161
+ end
162
+
163
+ def symbolized_update(target, other, blacklist = nil)
164
+ other.each_pair do |key, value|
165
+ key = key.to_sym
166
+ existing = target[key]
167
+ next if blacklist && blacklist.include?(key)
168
+
169
+ if value.respond_to?(:each_pair) and (existing.is_a?(Hash) or existing.nil?)
170
+ symbolized_update(existing || (target[key] = {}), value)
171
+ else
172
+ target[key] = value
173
+ end
174
+ end
175
+ end
176
+
177
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: will_paginate_ui_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Niels van der Zanden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: UIKit-compatible LinkRenderer for will_paginate gem
14
+ email: niels@pharynx.nl
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/will_paginate_ui_kit.rb
20
+ - lib/will_paginate_ui_kit/railtie.rb
21
+ - lib/will_paginate_ui_kit/view_helpers/ui_kit_link_renderer.rb
22
+ homepage: https://github.com/scarhand/will_paginate_ui_kit
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.5.2.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: UIKit-compatible LinkRenderer for will_paginate gem
46
+ test_files: []