listings 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -249,13 +249,20 @@ Also `render:` option can be used to suppress the rendering of the filter, but a
249
249
  filter :id, render: false
250
250
  ```
251
251
 
252
+ Or `render:` can be user to indicate the partial to be used for rendering that filter. Hence allowing custom UI for certain filter among the default UI. See [Templates](#templates) for information regarding where the partial will be looked.
253
+
254
+ ```ruby
255
+ filter :update_at, render: 'date'
256
+ ```
257
+
258
+
252
259
  Filters are rendered by default by the side of the listing. With `layout` method you can change this and render them on the top.
253
260
 
254
261
  ```ruby
255
262
  layout filters: :top
256
263
  ```
257
264
 
258
- Custom filters allows definition of custom meaning to a key. This filters are not rendered.
265
+ Custom filters allows definition of custom meaning to a key. This filters are not rendered by default. Use `render:` option to indicate the partial to be used for rendering.
259
266
 
260
267
  ```ruby
261
268
  custom_filter :order_lte do |items, value|
@@ -408,10 +415,10 @@ There are a number of templates involved in rendering the listing. These templat
408
415
 
409
416
  For example if a listing named `tracks` is rendered with `twitter-bootstrap-3` theme the templates are looked up in the following locations/order:
410
417
 
411
- * `<app>/views/listings/twitter-bootstrap-3/<partial>`
412
- * `<gem>/views/listings/twitter-bootstrap-3/<partial>`
413
418
  * `<app>/views/listings/tracks/<partial>`
414
419
  * `<gem>/views/listings/tracks/<partial>`
420
+ * `<app>/views/listings/twitter-bootstrap-3/<partial>`
421
+ * `<gem>/views/listings/twitter-bootstrap-3/<partial>`
415
422
  * `<app>/views/listings/<partial>`
416
423
  * `<gem>/views/listings/<partial>`
417
424
 
@@ -5,11 +5,15 @@ module Listings
5
5
  def full
6
6
  @listing = prepare_listing params, view_context
7
7
  render 'listings/full'
8
+ ensure
9
+ Kaminari::Helpers::Tag.paginate_with_listings(nil)
8
10
  end
9
11
 
10
12
  def content
11
13
  @listing = prepare_listing params, view_context
12
14
  render 'listings/content'
15
+ ensure
16
+ Kaminari::Helpers::Tag.paginate_with_listings(nil)
13
17
  end
14
18
 
15
19
  def export
@@ -22,6 +26,8 @@ module Listings
22
26
  render 'listings/export'
23
27
  end
24
28
  end
29
+ ensure
30
+ Kaminari::Helpers::Tag.paginate_with_listings(nil)
25
31
  end
26
32
 
27
33
  end
@@ -0,0 +1,2 @@
1
+ - listing.filters_to_render.reverse.each do |filter_view|
2
+ = listings_partial_render filter_view.partial_name, listing, filter: filter_view
@@ -0,0 +1,2 @@
1
+ .filters
2
+ = listings_partial_render 'filters', listing
@@ -0,0 +1 @@
1
+ = listings_partial_render 'filters', listing
@@ -0,0 +1,8 @@
1
+ %ul.nav.nav-list.well
2
+ %li.nav-header
3
+ = "Filter by #{filter.human_name}"
4
+ - filter.values.each do |value|
5
+ %li.filter
6
+ %a{:href => '#', :'data-key' => filter.key, :'data-value' => value}
7
+ = filter.value_for(value)
8
+ %i.icon-remove
@@ -0,0 +1,10 @@
1
+ %li.dropdown.pull-right
2
+ %a.dropdown-toggle(data-toggle="dropdown" href="#")
3
+ = filter.human_name
4
+ %b.caret
5
+ %ul.dropdown-menu
6
+ - filter.values.each do |value|
7
+ %li.filter
8
+ %a{:href => '#', :'data-key' => filter.key, :'data-value' => value}
9
+ = filter.value_for(value)
10
+ %i.icon-remove
@@ -0,0 +1,7 @@
1
+ %a.list-group-item.disabled(href="#")
2
+ = "Filter by #{filter.human_name}"
3
+ - filter.values.each do |value|
4
+ %a.list-group-item.filter{:href => '#', :'data-key' => filter.key, :'data-value' => value}
5
+ = filter.value_for(value)
6
+ %span.icon-remove
7
+ &times;
@@ -1,9 +1,2 @@
1
1
  .filters.list-group
2
- - listing.filters_to_render.reverse.each do |filter_view|
3
- %a.list-group-item.disabled(href="#")
4
- = "Filter by #{filter_view.human_name}"
5
- - filter_view.values.each do |value|
6
- %a.list-group-item.filter{:href => '#', :'data-key' => filter_view.key, :'data-value' => value}
7
- = filter_view.value_for(value)
8
- %span.icon-remove
9
- &times;
2
+ = listings_partial_render 'filters', listing
@@ -0,0 +1,10 @@
1
+ %li.dropdown
2
+ %a.dropdown-toggle(data-toggle="dropdown" href="#")
3
+ = filter.human_name
4
+ %span.caret
5
+ %ul.dropdown-menu
6
+ - filter.values.each do |value|
7
+ %li.filter
8
+ %a{:href => '#', :'data-key' => filter.key, :'data-value' => value}
9
+ = filter.value_for(value)
10
+ %i.icon-remove
data/lib/listings/base.rb CHANGED
@@ -89,7 +89,7 @@ module Listings
89
89
 
90
90
  if filterable?
91
91
  filters_to_render.each do |filter_view|
92
- filter_view.values # prepare values
92
+ filter_view.prepare_values
93
93
  end
94
94
 
95
95
  self.search_filters.each do |key, filter_value|
@@ -164,8 +164,8 @@ module Listings
164
164
  filters << FilterDescriptor.new(path, props, proc)
165
165
  end
166
166
 
167
- def custom_filter(key, &proc)
168
- filters << CustomFilterDescriptor.new(key, proc)
167
+ def custom_filter(key, props = {}, &proc)
168
+ filters << CustomFilterDescriptor.new(key, props, proc)
169
169
  end
170
170
 
171
171
  def layout(props = {})
@@ -1,10 +1,12 @@
1
1
  module Listings
2
2
  class CustomFilterDescriptor
3
3
  attr_reader :key
4
+ attr_reader :props
4
5
  attr_reader :proc
5
6
 
6
- def initialize(key, proc)
7
+ def initialize(key, props, proc)
7
8
  @key = key
9
+ @props = props
8
10
  @proc = proc
9
11
  end
10
12
 
@@ -8,12 +8,12 @@ module Listings
8
8
  @descriptor = descriptor
9
9
  end
10
10
 
11
- def key
12
- @descriptor.key
11
+ def prepare_values
12
+ # custom filters do not perform lookup
13
13
  end
14
14
 
15
- def render?
16
- false
15
+ def key
16
+ @descriptor.key
17
17
  end
18
18
 
19
19
  def apply_filter(value)
@@ -21,5 +21,25 @@ module Listings
21
21
  listing.instance_exec items, value, &@descriptor.proc
22
22
  end
23
23
  end
24
+
25
+ def render?
26
+ if render_option.is_a?(String)
27
+ true
28
+ else
29
+ render_option
30
+ end
31
+ end
32
+
33
+ def partial_name
34
+ if render_option.is_a?(String)
35
+ render_option
36
+ else
37
+ raise "custom filters can only be rendered when a partial is specified"
38
+ end
39
+ end
40
+
41
+ def render_option
42
+ @descriptor.props.fetch(:render, false)
43
+ end
24
44
  end
25
45
  end
@@ -4,6 +4,10 @@ module Listings
4
4
  super
5
5
  end
6
6
 
7
+ def prepare_values
8
+ values
9
+ end
10
+
7
11
  def values
8
12
  @values ||= listing.data_source.values_for_filter(field)
9
13
  end
@@ -16,12 +20,28 @@ module Listings
16
20
  end
17
21
  end
18
22
 
23
+ def apply_filter(value)
24
+ listing.data_source.filter(field, value)
25
+ end
26
+
19
27
  def render?
20
- @field_description.props.fetch(:render, true)
28
+ if render_option.is_a?(String)
29
+ true
30
+ else
31
+ render_option
32
+ end
21
33
  end
22
34
 
23
- def apply_filter(value)
24
- listing.data_source.filter(field, value)
35
+ def partial_name
36
+ if render_option.is_a?(String)
37
+ render_option
38
+ else
39
+ "#{listing.layout_options[:filters]}_filter"
40
+ end
41
+ end
42
+
43
+ def render_option
44
+ @field_description.props.fetch(:render, true)
25
45
  end
26
46
  end
27
47
  end
@@ -1,3 +1,3 @@
1
1
  module Listings
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -22,3 +22,18 @@
22
22
  // console.log(el);
23
23
  // });
24
24
  // });
25
+
26
+ $(function(){
27
+
28
+ $('.listing').on('change', '#date-filter', function(){
29
+ var listings = $(this).closest('.listing')
30
+ var filter = $(this);
31
+
32
+ if (filter.val() == '') {
33
+ listings.trigger("listings:filter:key:clear", filter.data('filter-key'))
34
+ } else {
35
+ listings.trigger("listings:filter:key:set", [filter.data('filter-key'), filter.val()])
36
+ }
37
+ });
38
+ });
39
+
@@ -13,11 +13,22 @@ class FilteredPostsListing < Listings::Base
13
13
  filter :author
14
14
  filter :category
15
15
 
16
+ custom_filter :updated, render: 'date' do |items, value|
17
+ begin
18
+ d = Date.parse(value)
19
+ items.where(updated_at: d.beginning_of_day..d.end_of_day)
20
+ rescue
21
+ items
22
+ end
23
+ end
24
+
16
25
 
17
26
  column :id
18
27
  column :title, searchable: true
19
28
  column :author
20
29
  column :category
30
+ column :updated_at
31
+
21
32
  column do |post|
22
33
  link_to 'Editar', edit_post_path(post)
23
34
  end
@@ -0,0 +1,4 @@
1
+ %ul.nav.nav-list.well
2
+ %li.nav-header
3
+ = "Filter by #{filter.key}"
4
+ %input#date-filter{:'data-filter-key' => filter.key, style: 'width: 100%'}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: listings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-01 00:00:00.000000000 Z
12
+ date: 2016-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -187,19 +187,23 @@ files:
187
187
  - app/controllers/listings/listings_controller.rb
188
188
  - app/helpers/listings/application_helper.rb
189
189
  - app/views/layouts/listings/application.html.erb
190
+ - app/views/listings/_filters.html.haml
190
191
  - app/views/listings/_listing.html.haml
192
+ - app/views/listings/_side_filters.html.haml
191
193
  - app/views/listings/_table_content.html.haml
194
+ - app/views/listings/_top_filters.html.haml
192
195
  - app/views/listings/content.js.erb
193
196
  - app/views/listings/export.xls.erb
194
197
  - app/views/listings/full.js.erb
195
198
  - app/views/listings/twitter-bootstrap-2/_header.html.haml
196
- - app/views/listings/twitter-bootstrap-2/_side_filters.html.haml
199
+ - app/views/listings/twitter-bootstrap-2/_side_filter.html.haml
197
200
  - app/views/listings/twitter-bootstrap-2/_table_full.html.haml
198
- - app/views/listings/twitter-bootstrap-2/_top_filters.html.haml
201
+ - app/views/listings/twitter-bootstrap-2/_top_filter.html.haml
199
202
  - app/views/listings/twitter-bootstrap-3/_header.html.haml
203
+ - app/views/listings/twitter-bootstrap-3/_side_filter.html.haml
200
204
  - app/views/listings/twitter-bootstrap-3/_side_filters.html.haml
201
205
  - app/views/listings/twitter-bootstrap-3/_table_full.html.haml
202
- - app/views/listings/twitter-bootstrap-3/_top_filters.html.haml
206
+ - app/views/listings/twitter-bootstrap-3/_top_filter.html.haml
203
207
  - config/locales/listings.yml
204
208
  - config/routes.rb
205
209
  - lib/listings/action_view_extensions.rb
@@ -251,6 +255,7 @@ files:
251
255
  - spec/dummy/app/models/post.rb
252
256
  - spec/dummy/app/models/track.rb
253
257
  - spec/dummy/app/views/layouts/application.html.erb
258
+ - spec/dummy/app/views/listings/_date.html.haml
254
259
  - spec/dummy/app/views/posts/filtered.html.haml
255
260
  - spec/dummy/app/views/posts/index.html.haml
256
261
  - spec/dummy/app/views/shared/_post_partial.html.haml
@@ -348,6 +353,7 @@ test_files:
348
353
  - spec/dummy/app/models/post.rb
349
354
  - spec/dummy/app/models/track.rb
350
355
  - spec/dummy/app/views/layouts/application.html.erb
356
+ - spec/dummy/app/views/listings/_date.html.haml
351
357
  - spec/dummy/app/views/posts/filtered.html.haml
352
358
  - spec/dummy/app/views/posts/index.html.haml
353
359
  - spec/dummy/app/views/shared/_post_partial.html.haml
@@ -1,11 +0,0 @@
1
- .filters
2
- - listing.filters_to_render.reverse.each do |filter_view|
3
- %ul.nav.nav-list.well
4
- %li.nav-header
5
- = "Filter by #{filter_view.human_name}"
6
- - filter_view.values.each do |value|
7
- %li.filter
8
- %a{:href => '#', :'data-key' => filter_view.key, :'data-value' => value}
9
- = filter_view.value_for(value)
10
- %i.icon-remove
11
-
@@ -1,11 +0,0 @@
1
- - listing.filters_to_render.each do |filter_view|
2
- %li.dropdown.pull-right
3
- %a.dropdown-toggle(data-toggle="dropdown" href="#")
4
- = filter_view.human_name
5
- %b.caret
6
- %ul.dropdown-menu
7
- - filter_view.values.each do |value|
8
- %li.filter
9
- %a{:href => '#', :'data-key' => filter_view.key, :'data-value' => value}
10
- = filter_view.value_for(value)
11
- %i.icon-remove
@@ -1,11 +0,0 @@
1
- - listing.filters_to_render.each do |filter_view|
2
- %li.dropdown
3
- %a.dropdown-toggle(data-toggle="dropdown" href="#")
4
- = filter_view.human_name
5
- %span.caret
6
- %ul.dropdown-menu
7
- - filter_view.values.each do |value|
8
- %li.filter
9
- %a{:href => '#', :'data-key' => filter_view.key, :'data-value' => value}
10
- = filter_view.value_for(value)
11
- %i.icon-remove