File tree Expand file tree Collapse file tree 5 files changed +27
-4
lines changed Expand file tree Collapse file tree 5 files changed +27
-4
lines changed Original file line number Diff line number Diff line change 231
231
232
232
context 'with an embedded document' do
233
233
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' }
236
236
post . save!
237
+ Post . all . each { |p | p . author . city }
237
238
end
238
239
239
- it 'sorts by author name ' do
240
+ it 'sorts by the embedded document field ' do
240
241
click_on 'Posts'
241
242
visit '/admin/posts?order=author.name_desc'
242
243
page . first ( '#index_table_posts > tbody > tr' ) . should have_content 'Bob'
243
244
244
245
visit '/admin/posts?order=author.name_asc'
245
246
page . first ( '#index_table_posts > tbody > tr' ) . should have_content 'Adam'
246
247
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
247
257
end
248
258
end
249
259
Original file line number Diff line number Diff line change 16
16
column 'Author Name' , :'author.name' do |post |
17
17
post . author . name if post . author . present?
18
18
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
19
23
default_actions
20
24
end
21
25
Original file line number Diff line number Diff line change @@ -3,5 +3,7 @@ class Author
3
3
include Mongoid ::Timestamps
4
4
5
5
embedded_in :post
6
+ embeds_one :city
7
+
6
8
field :name
7
9
end
Original file line number Diff line number Diff line change
1
+ class City
2
+ include Mongoid ::Document
3
+ include Mongoid ::Timestamps
4
+
5
+ embedded_in :author
6
+ field :name
7
+ end
Original file line number Diff line number Diff line change @@ -9,5 +9,5 @@ class Post
9
9
belongs_to :other_user , class_name : 'AdminUser'
10
10
11
11
embeds_one :author
12
- field :'author.name'
12
+ field :'author.city. name'
13
13
end
You can’t perform that action at this time.
0 commit comments