Skip to content

Commit 74b5edf

Browse files
committed
Ensure an embedded object within an embedded object can be sorted
1 parent 18cfcfe commit 74b5edf

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

spec/features/smoke_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,19 +231,29 @@
231231

232232
context 'with an embedded document' do
233233
before do
234-
Post.where(body: 'The quick brown fox jumps over the lazy dog.').update_all(author: {name: 'Bob'})
235-
post.author = Author.new name: 'Adam'
234+
Post.where(body: 'The quick brown fox jumps over the lazy dog.').update_all(author: { name: 'Bob', city: { name: 'Washington' } })
235+
post.author = Author.new name: 'Adam', city: { name: 'California' }
236236
post.save!
237+
Post.all.each{|p| p.author.city }
237238
end
238239

239-
it 'sorts by author name' do
240+
it 'sorts by the embedded document field' do
240241
click_on 'Posts'
241242
visit '/admin/posts?order=author.name_desc'
242243
page.first('#index_table_posts > tbody > tr').should have_content 'Bob'
243244

244245
visit '/admin/posts?order=author.name_asc'
245246
page.first('#index_table_posts > tbody > tr').should have_content 'Adam'
246247
end
248+
249+
it 'sorts by embedded document fields of the the embedded document' do
250+
click_on 'Posts'
251+
visit '/admin/posts?order=author.city.name_desc'
252+
page.first('#index_table_posts > tbody > tr').should have_content 'Washington'
253+
254+
visit '/admin/posts?order=author.city.name_asc'
255+
page.first('#index_table_posts > tbody > tr').should have_content 'California'
256+
end
247257
end
248258
end
249259

test_app/app/admin/posts.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
column 'Author Name', :'author.name' do |post|
1717
post.author.name if post.author.present?
1818
end
19+
column 'Author City Name', :'author.city.name' do |post|
20+
author = post.author
21+
author.city.name if author.present? and author.city.present?
22+
end
1923
default_actions
2024
end
2125

test_app/app/models/author.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ class Author
33
include Mongoid::Timestamps
44

55
embedded_in :post
6+
embeds_one :city
7+
68
field :name
79
end

test_app/app/models/city.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class City
2+
include Mongoid::Document
3+
include Mongoid::Timestamps
4+
5+
embedded_in :author
6+
field :name
7+
end

test_app/app/models/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class Post
99
belongs_to :other_user, class_name: 'AdminUser'
1010

1111
embeds_one :author
12-
field :'author.name'
12+
field :'author.city.name'
1313
end

0 commit comments

Comments
 (0)