Skip to content

Commit 18cfcfe

Browse files
committed
Sort by alone fields of by embedded fields
1 parent d80576b commit 18cfcfe

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
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

spec/features/smoke_spec.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,36 @@
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'})
235+
post.author = Author.new name: 'Adam'
236+
post.save!
237+
end
238+
239+
it 'sorts by author name' do
240+
click_on 'Posts'
241+
visit '/admin/posts?order=author.name_desc'
242+
page.first('#index_table_posts > tbody > tr').should have_content 'Bob'
243+
244+
visit '/admin/posts?order=author.name_asc'
245+
page.first('#index_table_posts > tbody > tr').should have_content 'Adam'
246+
end
247+
end
248+
end
249+
220250
describe "paginator" do
221251
it "must have paginator with 4 pages" do
222252
page.should have_css('.pagination > .page.current')
@@ -244,7 +274,5 @@
244274
end
245275
end
246276
end # context 'with 100 posts'
247-
248277
end
249-
250278
end

test_app/app/admin/posts.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,31 @@
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+
default_actions
20+
end
21+
22+
show do
23+
attributes_table do
24+
row :title
25+
row :body
26+
row :created_at
27+
row :updated_at
28+
end
29+
end
30+
31+
form do |f|
32+
f.inputs "Post" do
33+
f.input :title
34+
f.input :body
35+
end
36+
f.actions
37+
end
1138
end

test_app/app/models/author.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Author
2+
include Mongoid::Document
3+
include Mongoid::Timestamps
4+
5+
embedded_in :post
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.name'
1013
end

0 commit comments

Comments
 (0)