listings 0.1.12 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a22fc4c13d5cb505a29f55c60ce5e6e25f47da2b6bed2a3af544a98ceb1585a
4
- data.tar.gz: fae15cccdcd790883b853edc1e53c001fc752c6e7d356dea2c7dc8da05e81428
3
+ metadata.gz: a9b35711b73e6f70de549f6508b4d2626a3dc5c899d809bb08f1b6e6ff0adf49
4
+ data.tar.gz: 942eadc3eb9a0adfce42a544c61f263aeab9f3bf4a8825656a3a28bac3b36301
5
5
  SHA512:
6
- metadata.gz: 885752d12e05bf3e3e87fbaaa432d33bfbe72e187da074014fb54441d4e8527804ca803e7f43899ae03e36984aa31a12620e03cad96d07ee91b3daf1ea46d1b8
7
- data.tar.gz: 812390604aaa4ed022db339abce0e8ffddd291689cde6f1d03a0995eb5e2dbe997918b6fdb6fa9957946e60fb9b88897e9186826093491eb3705ad7f223fb021
6
+ metadata.gz: 9668f06ac36fcf570e47c0c325719c51f2c34118943acd673a82f910ccef7f7dbf9850c8d443a519c39ebf36fe4ad0ce9d087a2161fc8e400bd082953c8b5ff2
7
+ data.tar.gz: 53b6c6433a53d146d9ac62eaae1e68d7e7299c4f4a2945f10c1d276d7c98a43ca68e49197abfc0b5f4660c22dd7d3b2ef5b55a6b3217dc6b40a17f6829078046
data/README.md CHANGED
@@ -207,6 +207,14 @@ The block is evaluated in a view_context so any helper you will usually use in a
207
207
  column album: :name, title: 'Album'
208
208
  ```
209
209
 
210
+ > **computed columns**
211
+ >
212
+ > Listings will try to sort and filter by default using the `<table_name>.<column_name>` field of the query, on computed queries this will not work, you will need to use `column :my_computed_field, query_column: :my_computed_field` to allow sorting and filtering.
213
+ >
214
+ > Using computed columns might cause some issues depending on the Kaminari version used. If this is the case and you need to filter based on a computed filed, you will need to define a `custom_filter` to apply the filtering of the computed field.
215
+ >
216
+ > See example at [spec/dummy/app/listings/authors_listing.rb](spec/dummy/app/listings/authors_listing.rb)
217
+
210
218
  ### scope
211
219
 
212
220
  Declaring a `scope` with a symbol with allow user to show only records matching the scope in the ActiveRecord class
@@ -11,7 +11,7 @@ module Listings
11
11
  end
12
12
 
13
13
  def build_field(listing)
14
- listing.data_source.build_field(path)
14
+ listing.data_source.build_field(path, props)
15
15
  end
16
16
 
17
17
  def is_field?
@@ -68,12 +68,12 @@ module Listings::Sources
68
68
  @items = @items.eager_load(relation)
69
69
  end
70
70
 
71
- def build_field(path)
71
+ def build_field(path, props)
72
72
  path = self.class.sanitaize_path(path)
73
73
  if path.is_a?(Array)
74
74
  ActiveRecordAssociationField.new(path, self)
75
75
  else
76
- ActiveRecordField.new(path, self)
76
+ ActiveRecordField.new(path, self, props[:query_column])
77
77
  end
78
78
  end
79
79
  end
@@ -113,9 +113,10 @@ module Listings::Sources
113
113
  end
114
114
 
115
115
  class ActiveRecordField < BaseActiveRecordField
116
- def initialize(attribute_name, data_source)
116
+ def initialize(attribute_name, data_source, query_column)
117
117
  super(data_source)
118
118
  @attribute_name = attribute_name
119
+ @query_column = query_column
119
120
  end
120
121
 
121
122
  def value_for(item)
@@ -123,7 +124,7 @@ module Listings::Sources
123
124
  end
124
125
 
125
126
  def query_column
126
- "#{quote_table_name(data_source.items.table_name)}.#{quote_column_name(@attribute_name)}"
127
+ @query_column || "#{quote_table_name(data_source.items.table_name)}.#{quote_column_name(@attribute_name)}"
127
128
  end
128
129
 
129
130
  def key
@@ -45,7 +45,7 @@ module Listings::Sources
45
45
  end
46
46
 
47
47
  # returns a +Field+ for the specified options
48
- def build_field(path)
48
+ def build_field(path, props)
49
49
  end
50
50
 
51
51
  def self.for(model)
@@ -47,7 +47,7 @@ module Listings::Sources
47
47
  end
48
48
  end
49
49
 
50
- def build_field(path)
50
+ def build_field(path, props)
51
51
  path = self.class.sanitaize_path(path)
52
52
  unless path.is_a?(Array)
53
53
  path = [path]
@@ -1,3 +1,3 @@
1
1
  module Listings
2
- VERSION = "0.1.12"
2
+ VERSION = "0.1.13"
3
3
  end
@@ -23,9 +23,9 @@
23
23
  // });
24
24
  // });
25
25
 
26
- $(function(){
26
+ $(function () {
27
27
 
28
- $('.listing').on('change', '#date-filter', function(){
28
+ $('.listing').on('change', '#date-filter', function () {
29
29
  var listings = $(this).closest('.listing')
30
30
  var filter = $(this);
31
31
 
@@ -35,5 +35,17 @@ $(function(){
35
35
  listings.trigger("listings:filter:key:set", [filter.data('filter-key'), filter.val()])
36
36
  }
37
37
  });
38
+
39
+ $('.listing').on('click', '#posts-count-filter', function () {
40
+ var listings = $(this).closest('.listing')
41
+ var filter = $(this);
42
+ var value = filter.data('filter-value');
43
+
44
+ if (value == '') {
45
+ listings.trigger("listings:filter:key:clear", filter.data('filter-key'))
46
+ } else {
47
+ listings.trigger("listings:filter:key:set", [filter.data('filter-key'), value])
48
+ }
49
+ });
38
50
  });
39
51
 
@@ -10,4 +10,7 @@ class WelcomeController < ApplicationController
10
10
 
11
11
  def tracks
12
12
  end
13
+
14
+ def authors
15
+ end
13
16
  end
@@ -0,0 +1,26 @@
1
+ class AuthorsListing < Listings::Base
2
+ model do
3
+ Author
4
+ .select('authors.*, (select count(*) from posts where posts.author_id = authors.id) as posts_count')
5
+ end
6
+
7
+ paginates_per 7
8
+
9
+ filter :category, values: :categories
10
+
11
+ custom_filter :posts_count_eq, render: 'posts_count' do |items, value|
12
+ items.where("(select count(*) from posts where posts.author_id = authors.id) = ?", value.to_i)
13
+ end
14
+
15
+ def categories
16
+ Author.select('distinct category').pluck(:category).reject(&:nil?)
17
+ end
18
+
19
+ def query_counts
20
+ (1..30).to_a
21
+ end
22
+
23
+ column :name, seachable: true
24
+ column :posts_count, query_column: :posts_count
25
+ column :category
26
+ end
@@ -22,7 +22,7 @@ class PostsListing < Listings::Base
22
22
 
23
23
  column :id
24
24
  column :title, searchable: true
25
- column :author, searchable: true
25
+ column author: :name, searchable: true
26
26
  column :category
27
27
  column do |post|
28
28
  link_to 'Editar', edit_post_path(post)
@@ -0,0 +1,5 @@
1
+ class Author < ActiveRecord::Base
2
+ attr_accessible :name, :category if Rails::VERSION::MAJOR == 3
3
+
4
+ has_many :posts
5
+ end
@@ -1,6 +1,8 @@
1
1
  class Post < ActiveRecord::Base
2
2
  attr_accessible :title, :author, :category if Rails::VERSION::MAJOR == 3
3
3
 
4
+ belongs_to :author
5
+
4
6
  scope :even, -> { where('id % 2 = 0') }
5
7
  scope :odd, -> { where('id % 2 = 1') }
6
8
  scope :greater_than, lambda { |gt_id| where('id > ?', gt_id) }
@@ -0,0 +1,5 @@
1
+ %ul.nav.nav-list.well
2
+ %li.nav-header
3
+ = "Filter by #{filter.key}"
4
+ - (1..20).each do |value|
5
+ %a#posts-count-filter{:'data-filter-key' => filter.key, :'data-filter-value' => value, :href => '#'}= value
@@ -0,0 +1,3 @@
1
+ %h1 Welcome#authors
2
+
3
+ = render_listing :authors
@@ -12,6 +12,12 @@
12
12
  %dd
13
13
  ActiveRecord based listing with scopes, deferred_scopes, styles and selectable
14
14
 
15
+ %dl
16
+ %dt
17
+ = link_to 'Authors', authors_path
18
+ %dd
19
+ ActiveRecord based listing with computed columns
20
+
15
21
  %dl
16
22
  %dt
17
23
  = link_to 'Filtered posts', filtered_posts_path
@@ -5,6 +5,7 @@ Rails.application.routes.draw do
5
5
  get 'filtered'
6
6
  end
7
7
  end
8
+ get 'authors', to: 'welcome#authors'
8
9
 
9
10
  get 'array', to: 'welcome#array'
10
11
  get 'hash', to: 'welcome#hash'
@@ -19,11 +19,18 @@ ActiveRecord::Schema.define(:version => 20150611185922) do
19
19
  t.datetime "updated_at", :null => false
20
20
  end
21
21
 
22
+ create_table "authors", :force => true do |t|
23
+ t.string "name"
24
+ t.string "category"
25
+ t.datetime "created_at", :null => false
26
+ t.datetime "updated_at", :null => false
27
+ end
28
+
22
29
  create_table "posts", :force => true do |t|
23
30
  t.string "title"
24
31
  t.datetime "created_at", :null => false
25
32
  t.datetime "updated_at", :null => false
26
- t.text "author"
33
+ t.integer "author_id"
27
34
  t.string "category"
28
35
  end
29
36
 
@@ -37,5 +44,6 @@ ActiveRecord::Schema.define(:version => 20150611185922) do
37
44
  end
38
45
 
39
46
  add_index "tracks", ["album_id"], :name => "index_tracks_on_album_id"
47
+ add_index "posts", ["author_id"], :name => "index_posts_on_author_id"
40
48
 
41
49
  end
@@ -1,8 +1,14 @@
1
1
  require 'factory_girl'
2
2
  # Dir[Rails.root.join("spec/factories/*.rb")].each {|f| require f}
3
3
 
4
+ AUTHORS = []
5
+
6
+ (1..20).each do |sn|
7
+ AUTHORS << Author.create!(name: "john-#{(sn % 20) + 1}", category: "category-#{(sn % 3) + 1}")
8
+ end
9
+
4
10
  (1..100).each do |sn|
5
- Post.create! title: "post n-#{sn}", author: "john-#{(sn % 4) + 1}", category: "category-#{(sn % 3) + 1}"
11
+ Post.create! title: "post n-#{sn}", author: AUTHORS[sn % (sn / 10 + 1)], category: "category-#{(sn % 3) + 1}"
6
12
  end
7
13
 
8
14
  (1..10).each do |sn|
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :author do
3
+ name
4
+ end
5
+ end
@@ -1,5 +1,6 @@
1
1
  FactoryGirl.define do
2
2
  factory :post do
3
3
  title
4
+ author
4
5
  end
5
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: listings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian J. Cardiff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-10 00:00:00.000000000 Z
11
+ date: 2020-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -198,22 +198,26 @@ files:
198
198
  - spec/dummy/app/helpers/application_helper.rb
199
199
  - spec/dummy/app/helpers/posts_helper.rb
200
200
  - spec/dummy/app/listings/array_listing.rb
201
+ - spec/dummy/app/listings/authors_listing.rb
201
202
  - spec/dummy/app/listings/filtered_posts_listing.rb
202
203
  - spec/dummy/app/listings/hash_listing.rb
203
204
  - spec/dummy/app/listings/posts_listing.rb
204
205
  - spec/dummy/app/listings/tracks_fixed_order_listing.rb
205
206
  - spec/dummy/app/listings/tracks_listing.rb
206
207
  - spec/dummy/app/models/album.rb
208
+ - spec/dummy/app/models/author.rb
207
209
  - spec/dummy/app/models/object_album.rb
208
210
  - spec/dummy/app/models/object_track.rb
209
211
  - spec/dummy/app/models/post.rb
210
212
  - spec/dummy/app/models/track.rb
211
213
  - spec/dummy/app/views/layouts/application.html.erb
212
214
  - spec/dummy/app/views/listings/_date.html.haml
215
+ - spec/dummy/app/views/listings/_posts_count.html.haml
213
216
  - spec/dummy/app/views/posts/filtered.html.haml
214
217
  - spec/dummy/app/views/posts/index.html.haml
215
218
  - spec/dummy/app/views/shared/_post_partial.html.haml
216
219
  - spec/dummy/app/views/welcome/array.html.haml
220
+ - spec/dummy/app/views/welcome/authors.html.haml
217
221
  - spec/dummy/app/views/welcome/hash.html.haml
218
222
  - spec/dummy/app/views/welcome/index.html.haml
219
223
  - spec/dummy/app/views/welcome/tracks.html.haml
@@ -248,7 +252,8 @@ files:
248
252
  - spec/dummy/script/rails
249
253
  - spec/dummy/spec
250
254
  - spec/factories/albums.rb
251
- - spec/factories/post.rb
255
+ - spec/factories/authors.rb
256
+ - spec/factories/posts.rb
252
257
  - spec/factories/tracks.rb
253
258
  - spec/factories/traits.rb
254
259
  - spec/lib/filter_parser_spec.rb
@@ -289,11 +294,13 @@ test_files:
289
294
  - spec/dummy/app/listings/filtered_posts_listing.rb
290
295
  - spec/dummy/app/listings/posts_listing.rb
291
296
  - spec/dummy/app/listings/array_listing.rb
297
+ - spec/dummy/app/listings/authors_listing.rb
292
298
  - spec/dummy/app/listings/hash_listing.rb
293
299
  - spec/dummy/app/listings/tracks_listing.rb
294
300
  - spec/dummy/app/listings/tracks_fixed_order_listing.rb
295
301
  - spec/dummy/app/models/object_track.rb
296
302
  - spec/dummy/app/models/album.rb
303
+ - spec/dummy/app/models/author.rb
297
304
  - spec/dummy/app/models/object_album.rb
298
305
  - spec/dummy/app/models/track.rb
299
306
  - spec/dummy/app/models/post.rb
@@ -303,11 +310,13 @@ test_files:
303
310
  - spec/dummy/app/views/posts/filtered.html.haml
304
311
  - spec/dummy/app/views/posts/index.html.haml
305
312
  - spec/dummy/app/views/listings/_date.html.haml
313
+ - spec/dummy/app/views/listings/_posts_count.html.haml
306
314
  - spec/dummy/app/views/shared/_post_partial.html.haml
307
315
  - spec/dummy/app/views/welcome/array.html.haml
308
316
  - spec/dummy/app/views/welcome/index.html.haml
309
317
  - spec/dummy/app/views/welcome/hash.html.haml
310
318
  - spec/dummy/app/views/welcome/tracks.html.haml
319
+ - spec/dummy/app/views/welcome/authors.html.haml
311
320
  - spec/dummy/app/views/layouts/application.html.erb
312
321
  - spec/dummy/app/assets/config/manifest.js
313
322
  - spec/dummy/app/assets/javascripts/application.js
@@ -355,8 +364,9 @@ test_files:
355
364
  - spec/support/query_counter.rb
356
365
  - spec/factories/albums.rb
357
366
  - spec/factories/traits.rb
367
+ - spec/factories/authors.rb
368
+ - spec/factories/posts.rb
358
369
  - spec/factories/tracks.rb
359
- - spec/factories/post.rb
360
370
  - spec/lib/filter_parser_spec.rb
361
371
  - spec/lib/sources/active_record_data_source_spec.rb
362
372
  - spec/lib/sources/object_data_source_spec.rb