From 74b5edf7aa865960c3d340c09bfd5e26a0eb83ac Mon Sep 17 00:00:00 2001 From: Daniel Gaytan Date: Thu, 6 Mar 2014 12:36:45 -0600 Subject: [PATCH] Ensure an embedded object within an embedded object can be sorted --- spec/features/smoke_spec.rb | 16 +++++++++++++--- test_app/app/admin/posts.rb | 4 ++++ test_app/app/models/author.rb | 2 ++ test_app/app/models/city.rb | 7 +++++++ test_app/app/models/post.rb | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test_app/app/models/city.rb diff --git a/spec/features/smoke_spec.rb b/spec/features/smoke_spec.rb index 20f0fe7..ea29bc2 100644 --- a/spec/features/smoke_spec.rb +++ b/spec/features/smoke_spec.rb @@ -231,12 +231,13 @@ 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' @@ -244,6 +245,15 @@ 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 diff --git a/test_app/app/admin/posts.rb b/test_app/app/admin/posts.rb index a1c42b2..a0adec8 100644 --- a/test_app/app/admin/posts.rb +++ b/test_app/app/admin/posts.rb @@ -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 diff --git a/test_app/app/models/author.rb b/test_app/app/models/author.rb index 27d1659..c42240b 100644 --- a/test_app/app/models/author.rb +++ b/test_app/app/models/author.rb @@ -3,5 +3,7 @@ class Author include Mongoid::Timestamps embedded_in :post + embeds_one :city + field :name end diff --git a/test_app/app/models/city.rb b/test_app/app/models/city.rb new file mode 100644 index 0000000..eb31732 --- /dev/null +++ b/test_app/app/models/city.rb @@ -0,0 +1,7 @@ +class City + include Mongoid::Document + include Mongoid::Timestamps + + embedded_in :author + field :name +end diff --git a/test_app/app/models/post.rb b/test_app/app/models/post.rb index a7b8125..57b9704 100644 --- a/test_app/app/models/post.rb +++ b/test_app/app/models/post.rb @@ -9,5 +9,5 @@ class Post belongs_to :other_user, class_name: 'AdminUser' embeds_one :author - field :'author.name' + field :'author.city.name' end