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 +4 -4
- data/README.md +8 -0
- data/lib/listings/base_field_descriptor.rb +1 -1
- data/lib/listings/sources/active_record_data_source.rb +5 -4
- data/lib/listings/sources/data_source.rb +1 -1
- data/lib/listings/sources/object_data_source.rb +1 -1
- data/lib/listings/version.rb +1 -1
- data/spec/dummy/app/assets/javascripts/application.js +14 -2
- data/spec/dummy/app/controllers/welcome_controller.rb +3 -0
- data/spec/dummy/app/listings/authors_listing.rb +26 -0
- data/spec/dummy/app/listings/posts_listing.rb +1 -1
- data/spec/dummy/app/models/author.rb +5 -0
- data/spec/dummy/app/models/post.rb +2 -0
- data/spec/dummy/app/views/listings/_posts_count.html.haml +5 -0
- data/spec/dummy/app/views/welcome/authors.html.haml +3 -0
- data/spec/dummy/app/views/welcome/index.html.haml +6 -0
- data/spec/dummy/config/routes.rb +1 -0
- data/spec/dummy/db/schema.rb +9 -1
- data/spec/dummy/db/seeds.rb +7 -1
- data/spec/factories/authors.rb +5 -0
- data/spec/factories/{post.rb → posts.rb} +1 -0
- metadata +14 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9b35711b73e6f70de549f6508b4d2626a3dc5c899d809bb08f1b6e6ff0adf49
|
4
|
+
data.tar.gz: 942eadc3eb9a0adfce42a544c61f263aeab9f3bf4a8825656a3a28bac3b36301
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -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
|
data/lib/listings/version.rb
CHANGED
@@ -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
|
|
@@ -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
|
@@ -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) }
|
@@ -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
|
data/spec/dummy/config/routes.rb
CHANGED
data/spec/dummy/db/schema.rb
CHANGED
@@ -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.
|
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
|
data/spec/dummy/db/seeds.rb
CHANGED
@@ -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:
|
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|
|
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.
|
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-
|
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/
|
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
|