Skip to content

Commit f33e41e

Browse files
committed
Merge pull request #72 from daniel-g/master
Fix sorting embedded objects and single fields.
2 parents 7b3073b + 74b5edf commit f33e41e

File tree

7 files changed

+101
-4
lines changed

7 files changed

+101
-4
lines changed

lib/active_admin/mongoid/document.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ def columns_hash
115115

116116
def reorder sorting
117117
return unscoped if sorting.blank?
118-
options = sorting.split(' ')
118+
if sorting.match /\".*\".*/
119+
options = sorting.split(/ |\./)
120+
options.shift if options.count == 3
121+
else
122+
options = sorting.split(' ')
123+
end
119124
field, order = *options
120125
unscoped.order_by(field => order)
121126
end

lib/active_admin/mongoid/helpers/collection.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ module Collection
66
original_collection_size = instance_method(:collection_size)
77
def collection_size(collection=collection)
88
if(not collection.empty? and collection.first.class.included_modules.include?(Mongoid::Document))
9-
collection.count(true)
9+
if collection.first.class.embedded?
10+
collection.count
11+
else
12+
collection.count(true)
13+
end
1014
else
1115
original_collection_size(collection)
1216
end

spec/features/smoke_spec.rb

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,46 @@
217217
click_on 'Posts'
218218
end
219219

220+
describe 'sorting' do
221+
let!(:post) { Post.create!(title: "First Post", body: 'First Post', view_count: 5, admin_user: admin_user, other_user: other_user) }
222+
223+
it 'sorts by title' do
224+
click_on 'Posts'
225+
page.find('#index_table_posts > thead > tr > th > a', text: 'Title').click
226+
page.first('#index_table_posts > tbody > tr').should have_content 'Quick Brown Fox'
227+
228+
page.find('#index_table_posts > thead > tr > th > a', text: 'Title').click
229+
page.first('#index_table_posts > tbody > tr').should have_content 'First Post'
230+
end
231+
232+
context 'with an embedded document' do
233+
before do
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+
post.save!
237+
Post.all.each{|p| p.author.city }
238+
end
239+
240+
it 'sorts by the embedded document field' do
241+
click_on 'Posts'
242+
visit '/admin/posts?order=author.name_desc'
243+
page.first('#index_table_posts > tbody > tr').should have_content 'Bob'
244+
245+
visit '/admin/posts?order=author.name_asc'
246+
page.first('#index_table_posts > tbody > tr').should have_content 'Adam'
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
257+
end
258+
end
259+
220260
describe "paginator" do
221261
it "must have paginator with 4 pages" do
222262
page.should have_css('.pagination > .page.current')
@@ -244,7 +284,5 @@
244284
end
245285
end
246286
end # context 'with 100 posts'
247-
248287
end
249-
250288
end

test_app/app/admin/posts.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,35 @@
88
filter :admin_user, as: :select
99
filter :other_user, as: :check_boxes
1010

11+
index do
12+
selectable_column
13+
column :title
14+
column :body
15+
column :view_count
16+
column 'Author Name', :'author.name' do |post|
17+
post.author.name if post.author.present?
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
23+
default_actions
24+
end
25+
26+
show do
27+
attributes_table do
28+
row :title
29+
row :body
30+
row :created_at
31+
row :updated_at
32+
end
33+
end
34+
35+
form do |f|
36+
f.inputs "Post" do
37+
f.input :title
38+
f.input :body
39+
end
40+
f.actions
41+
end
1142
end

test_app/app/models/author.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Author
2+
include Mongoid::Document
3+
include Mongoid::Timestamps
4+
5+
embedded_in :post
6+
embeds_one :city
7+
8+
field :name
9+
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ class Post
77
field :view_count, type: ::Integer, default: 0
88
belongs_to :admin_user
99
belongs_to :other_user, class_name: 'AdminUser'
10+
11+
embeds_one :author
12+
field :'author.city.name'
1013
end

0 commit comments

Comments
 (0)