Skip to content

Commit

Permalink
Sort by alone fields of by embedded fields
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-g committed Mar 6, 2014
1 parent d80576b commit 18cfcfe
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/active_admin/mongoid/document.rb
Expand Up @@ -115,7 +115,12 @@ def columns_hash

def reorder sorting
return unscoped if sorting.blank?
options = sorting.split(' ')
if sorting.match /\".*\".*/
options = sorting.split(/ |\./)
options.shift if options.count == 3
else
options = sorting.split(' ')
end
field, order = *options
unscoped.order_by(field => order)
end
Expand Down
32 changes: 30 additions & 2 deletions spec/features/smoke_spec.rb
Expand Up @@ -217,6 +217,36 @@
click_on 'Posts'
end

describe 'sorting' do
let!(:post) { Post.create!(title: "First Post", body: 'First Post', view_count: 5, admin_user: admin_user, other_user: other_user) }

it 'sorts by title' do
click_on 'Posts'
page.find('#index_table_posts > thead > tr > th > a', text: 'Title').click
page.first('#index_table_posts > tbody > tr').should have_content 'Quick Brown Fox'

page.find('#index_table_posts > thead > tr > th > a', text: 'Title').click
page.first('#index_table_posts > tbody > tr').should have_content 'First Post'
end

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.save!
end

it 'sorts by author name' 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
end
end

describe "paginator" do
it "must have paginator with 4 pages" do
page.should have_css('.pagination > .page.current')
Expand Down Expand Up @@ -244,7 +274,5 @@
end
end
end # context 'with 100 posts'

end

end
27 changes: 27 additions & 0 deletions test_app/app/admin/posts.rb
Expand Up @@ -8,4 +8,31 @@
filter :admin_user, as: :select
filter :other_user, as: :check_boxes

index do
selectable_column
column :title
column :body
column :view_count
column 'Author Name', :'author.name' do |post|
post.author.name if post.author.present?
end
default_actions
end

show do
attributes_table do
row :title
row :body
row :created_at
row :updated_at
end
end

form do |f|
f.inputs "Post" do
f.input :title
f.input :body
end
f.actions
end
end
7 changes: 7 additions & 0 deletions test_app/app/models/author.rb
@@ -0,0 +1,7 @@
class Author
include Mongoid::Document
include Mongoid::Timestamps

embedded_in :post
field :name
end
3 changes: 3 additions & 0 deletions test_app/app/models/post.rb
Expand Up @@ -7,4 +7,7 @@ class Post
field :view_count, type: ::Integer, default: 0
belongs_to :admin_user
belongs_to :other_user, class_name: 'AdminUser'

embeds_one :author
field :'author.name'
end

0 comments on commit 18cfcfe

Please sign in to comment.