Skip to content

Commit

Permalink
Ensure an embedded object within an embedded object can be sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-g committed Mar 6, 2014
1 parent 18cfcfe commit 74b5edf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
16 changes: 13 additions & 3 deletions spec/features/smoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,29 @@

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

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

visit '/admin/posts?order=author.name_asc'
page.first('#index_table_posts > tbody > tr').should have_content 'Adam'
end

it 'sorts by embedded document fields of the the embedded document' do
click_on 'Posts'
visit '/admin/posts?order=author.city.name_desc'
page.first('#index_table_posts > tbody > tr').should have_content 'Washington'

visit '/admin/posts?order=author.city.name_asc'
page.first('#index_table_posts > tbody > tr').should have_content 'California'
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions test_app/app/admin/posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
column 'Author Name', :'author.name' do |post|
post.author.name if post.author.present?
end
column 'Author City Name', :'author.city.name' do |post|
author = post.author
author.city.name if author.present? and author.city.present?
end
default_actions
end

Expand Down
2 changes: 2 additions & 0 deletions test_app/app/models/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ class Author
include Mongoid::Timestamps

embedded_in :post
embeds_one :city

field :name
end
7 changes: 7 additions & 0 deletions test_app/app/models/city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class City
include Mongoid::Document
include Mongoid::Timestamps

embedded_in :author
field :name
end
2 changes: 1 addition & 1 deletion test_app/app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class Post
belongs_to :other_user, class_name: 'AdminUser'

embeds_one :author
field :'author.name'
field :'author.city.name'
end

0 comments on commit 74b5edf

Please sign in to comment.