sql_search_n_sort 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,18 +25,24 @@ FactoryGirl.define do
25
25
 
26
26
  factory :product do
27
27
  sequence(:name) { |n| "cog_#{n}" }
28
- descr { "random description #{Random.rand(1..20)}" }
29
- price { "#{Random.rand(1..250)}" }
30
- date_produced { Time.now - Random.rand(1..100).days }
31
- manufacturer { "Company #{Random.rand(1..5)}" }
28
+ sequence(:descr) { |n| "random description #{n}" }
29
+ sequence(:price) { |n| "#{n}" }
30
+ sequence(:date_produced) { |n| Time.now - n.days }
31
+ sequence(:manufacturer) { |n| "Company #{n}" }
32
32
  end
33
33
 
34
34
 
35
35
  factory :article do
36
- headline "Headline #{Random.rand(1..100)}"
37
- by_line "Journalist #{Random.rand(1..5)}"
38
- date_pub { Time.now - Random.rand(1..100).days }
39
- body "The main article... #{Random.rand(0..50)}"
36
+ sequence(:headline) { |n| "Headline #{n}" }
37
+ sequence(:by_line) { |n| "Journalist #{n}" }
38
+ sequence(:date_pub) { |n| Time.now - n.days }
39
+ sequence(:body) { |n| "The main article... #{n}" }
40
+ end
41
+
42
+ factory :comment do
43
+ article
44
+ sequence(:ctext) { |n| "These are comments __#{n}__" }
45
+ sequence(:commentator) { |n| "myHandle_#{n}" }
40
46
  end
41
47
 
42
48
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Comment do
4
+
5
+ describe "Associations" do
6
+ it { is_expected.to respond_to(:article) }
7
+ end
8
+ describe "Modules" do
9
+ #just to verify the model is extending the module
10
+ it "extends SqlSearchableSortable module" do
11
+ Comment.singleton_class.included_modules.include?(SqlSearchableSortable)
12
+ end
13
+ end
14
+
15
+ describe "#sql_sort" do
16
+
17
+ before do
18
+ Article.record_timestamps = false
19
+ Comment.record_timestamps = false
20
+ (1..5).each do
21
+ article = FactoryGirl.create(:article, headline: "Some Article Headline #{rand(1..9)}", updated_at: Time.now + (rand(1..9)).minutes, created_at: Time.now - (rand(1..9)).minutes)
22
+ (1..3).each do
23
+ FactoryGirl.create(:comment, article: article, updated_at: Time.now + (rand(1..9)).minutes, created_at: Time.now - (rand(1..9)).minutes)
24
+ end
25
+ end
26
+ expect(Comment.count).to be > 0 #non-trivial for sorting
27
+ end
28
+
29
+
30
+ describe "Allows sorting by joined model column" do
31
+ it "sorts by article.headline as defined in the model" do
32
+ comments = Comment.joins(:article).sql_sort("headline")
33
+ expect(comments.pluck(:headline)).to eq(comments.pluck(:headline).sort)
34
+ end
35
+ end
36
+
37
+ describe "Column Naming Conflicts" do
38
+ context "when joined tables share a column name" do
39
+ let!(:comments_1) { Comment.joins(:article).sql_sort("updated_at") }
40
+ let!(:comments_2) { Comment.joins(:article).sql_sort("created_at") }
41
+ describe "sorts by the column from the table specified within the hash (or the base table)" do
42
+ it "sorts using the comments table for updated_at" do
43
+ expect(comments_1.pluck("comments.updated_at")).to eq(comments_1.pluck("comments.updated_at").sort)
44
+ end
45
+ it "sorts using the articles table for created_at" do
46
+ expect(comments_2.pluck("articles.created_at")).to eq(comments_2.pluck("articles.created_at").sort)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+
54
+
55
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_search_n_sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John O'Malley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-08 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -195,7 +195,9 @@ files:
195
195
  - lib/sql_search_n_sort.rb
196
196
  - lib/sql_search_n_sort/app/helpers/sql_search_n_sort_helper.rb
197
197
  - lib/sql_search_n_sort/exceptions.rb
198
+ - lib/sql_search_n_sort/model_sort_config.rb
198
199
  - lib/sql_search_n_sort/railtie.rb
200
+ - lib/sql_search_n_sort/sort_column.rb
199
201
  - lib/sql_search_n_sort/sql_searchable_sortable.rb
200
202
  - lib/sql_search_n_sort/sql_sort_setup.rb
201
203
  - lib/sql_search_n_sort/version.rb
@@ -216,6 +218,7 @@ files:
216
218
  - test/dummy/app/helpers/articles_helper.rb
217
219
  - test/dummy/app/models/admin/member.rb
218
220
  - test/dummy/app/models/article.rb
221
+ - test/dummy/app/models/comment.rb
219
222
  - test/dummy/app/models/item.rb
220
223
  - test/dummy/app/models/person.rb
221
224
  - test/dummy/app/models/product.rb
@@ -263,6 +266,9 @@ files:
263
266
  - test/dummy/db/migrate/20140725161555_add_dob_and_grade_to_people.rb
264
267
  - test/dummy/db/migrate/20141003015903_create_unsearchables.rb
265
268
  - test/dummy/db/migrate/20150306040113_create_members.rb
269
+ - test/dummy/db/migrate/20160121020023_create_comments.rb
270
+ - test/dummy/db/migrate/20160121020232_add_timestamps_to_comments.rb
271
+ - test/dummy/db/migrate/20160121020730_add_article_id_to_comments.rb
266
272
  - test/dummy/db/migrate/20160128023040_create_items.rb
267
273
  - test/dummy/db/schema.rb
268
274
  - test/dummy/db/test.sqlite3
@@ -275,6 +281,7 @@ files:
275
281
  - test/dummy/public/favicon.ico
276
282
  - test/dummy/spec/factories/factory.rb
277
283
  - test/dummy/spec/helpers/sql_search_n_sort_helper_spec.rb
284
+ - test/dummy/spec/models/comment_spec.rb
278
285
  - test/dummy/spec/models/member_spec.rb
279
286
  - test/dummy/spec/models/person_spec.rb
280
287
  - test/dummy/spec/models/product_spec.rb
@@ -363,6 +370,7 @@ test_files:
363
370
  - test/dummy/app/helpers/articles_helper.rb
364
371
  - test/dummy/app/models/admin/member.rb
365
372
  - test/dummy/app/models/article.rb
373
+ - test/dummy/app/models/comment.rb
366
374
  - test/dummy/app/models/item.rb
367
375
  - test/dummy/app/models/person.rb
368
376
  - test/dummy/app/models/product.rb
@@ -410,6 +418,9 @@ test_files:
410
418
  - test/dummy/db/migrate/20140725161555_add_dob_and_grade_to_people.rb
411
419
  - test/dummy/db/migrate/20141003015903_create_unsearchables.rb
412
420
  - test/dummy/db/migrate/20150306040113_create_members.rb
421
+ - test/dummy/db/migrate/20160121020023_create_comments.rb
422
+ - test/dummy/db/migrate/20160121020232_add_timestamps_to_comments.rb
423
+ - test/dummy/db/migrate/20160121020730_add_article_id_to_comments.rb
413
424
  - test/dummy/db/migrate/20160128023040_create_items.rb
414
425
  - test/dummy/db/schema.rb
415
426
  - test/dummy/db/test.sqlite3
@@ -424,6 +435,7 @@ test_files:
424
435
  - test/dummy/README.rdoc
425
436
  - test/dummy/spec/factories/factory.rb
426
437
  - test/dummy/spec/helpers/sql_search_n_sort_helper_spec.rb
438
+ - test/dummy/spec/models/comment_spec.rb
427
439
  - test/dummy/spec/models/member_spec.rb
428
440
  - test/dummy/spec/models/person_spec.rb
429
441
  - test/dummy/spec/models/product_spec.rb