|
4 | 4 |
|
5 | 5 | describe 'Grouping search results by attributes', :live => true do
|
6 | 6 | it "groups by the provided attribute" do
|
7 |
| - snuff = Book.create! :title => 'Snuff', :year => 2011 |
8 |
| - earth = Book.create! :title => 'The Long Earth', :year => 2012 |
9 |
| - dodger = Book.create! :title => 'Dodger', :year => 2012 |
| 7 | + snuff = Book.create! :title => 'Snuff', :publishing_year => 2011 |
| 8 | + earth = Book.create! :title => 'The Long Earth', :publishing_year => 2012 |
| 9 | + dodger = Book.create! :title => 'Dodger', :publishing_year => 2012 |
10 | 10 |
|
11 | 11 | index
|
12 | 12 |
|
13 |
| - expect(Book.search(:group_by => :year).to_a).to eq([snuff, earth]) |
| 13 | + expect(Book.search(:group_by => :publishing_year).to_a).to eq([snuff, earth]) |
14 | 14 | end
|
15 | 15 |
|
16 | 16 | it "allows sorting within the group" do
|
17 |
| - snuff = Book.create! :title => 'Snuff', :year => 2011 |
18 |
| - earth = Book.create! :title => 'The Long Earth', :year => 2012 |
19 |
| - dodger = Book.create! :title => 'Dodger', :year => 2012 |
| 17 | + snuff = Book.create! :title => 'Snuff', :publishing_year => 2011 |
| 18 | + earth = Book.create! :title => 'The Long Earth', :publishing_year => 2012 |
| 19 | + dodger = Book.create! :title => 'Dodger', :publishing_year => 2012 |
20 | 20 |
|
21 | 21 | index
|
22 | 22 |
|
23 |
| - expect(Book.search(:group_by => :year, :order_group_by => 'title ASC').to_a). |
| 23 | + expect(Book.search(:group_by => :publishing_year, :order_group_by => 'title ASC').to_a). |
24 | 24 | to eq([snuff, dodger])
|
25 | 25 | end
|
26 | 26 |
|
27 | 27 | it "allows enumerating by count" do
|
28 |
| - snuff = Book.create! :title => 'Snuff', :year => 2011 |
29 |
| - earth = Book.create! :title => 'The Long Earth', :year => 2012 |
30 |
| - dodger = Book.create! :title => 'Dodger', :year => 2012 |
| 28 | + snuff = Book.create! :title => 'Snuff', :publishing_year => 2011 |
| 29 | + earth = Book.create! :title => 'The Long Earth', :publishing_year => 2012 |
| 30 | + dodger = Book.create! :title => 'Dodger', :publishing_year => 2012 |
31 | 31 |
|
32 | 32 | index
|
33 | 33 |
|
34 | 34 | expectations = [[snuff, 1], [earth, 2]]
|
35 | 35 |
|
36 |
| - Book.search(:group_by => :year).each_with_count do |book, count| |
| 36 | + Book.search(:group_by => :publishing_year).each_with_count do |book, count| |
37 | 37 | expectation = expectations.shift
|
38 | 38 |
|
39 | 39 | expect(book).to eq(expectation.first)
|
|
42 | 42 | end
|
43 | 43 |
|
44 | 44 | it "allows enumerating by group" do
|
45 |
| - snuff = Book.create! :title => 'Snuff', :year => 2011 |
46 |
| - earth = Book.create! :title => 'The Long Earth', :year => 2012 |
47 |
| - dodger = Book.create! :title => 'Dodger', :year => 2012 |
| 45 | + snuff = Book.create! :title => 'Snuff', :publishing_year => 2011 |
| 46 | + earth = Book.create! :title => 'The Long Earth', :publishing_year => 2012 |
| 47 | + dodger = Book.create! :title => 'Dodger', :publishing_year => 2012 |
48 | 48 |
|
49 | 49 | index
|
50 | 50 |
|
51 | 51 | expectations = [[snuff, 2011], [earth, 2012]]
|
52 | 52 |
|
53 |
| - Book.search(:group_by => :year).each_with_group do |book, group| |
| 53 | + Book.search(:group_by => :publishing_year).each_with_group do |book, group| |
54 | 54 | expectation = expectations.shift
|
55 | 55 |
|
56 | 56 | expect(book).to eq(expectation.first)
|
|
59 | 59 | end
|
60 | 60 |
|
61 | 61 | it "allows enumerating by group and count" do
|
62 |
| - snuff = Book.create! :title => 'Snuff', :year => 2011 |
63 |
| - earth = Book.create! :title => 'The Long Earth', :year => 2012 |
64 |
| - dodger = Book.create! :title => 'Dodger', :year => 2012 |
| 62 | + snuff = Book.create! :title => 'Snuff', :publishing_year => 2011 |
| 63 | + earth = Book.create! :title => 'The Long Earth', :publishing_year => 2012 |
| 64 | + dodger = Book.create! :title => 'Dodger', :publishing_year => 2012 |
65 | 65 |
|
66 | 66 | index
|
67 | 67 |
|
68 | 68 | expectations = [[snuff, 2011, 1], [earth, 2012, 2]]
|
69 |
| - search = Book.search(:group_by => :year) |
| 69 | + search = Book.search(:group_by => :publishing_year) |
70 | 70 |
|
71 | 71 | search.each_with_group_and_count do |book, group, count|
|
72 | 72 | expectation = expectations.shift
|
|
0 commit comments