Skip to content

Commit

Permalink
Merge pull request #72 from daniel-g/master
Browse files Browse the repository at this point in the history
Fix sorting embedded objects and single fields.
  • Loading branch information
elia committed Mar 6, 2014
2 parents 7b3073b + 74b5edf commit f33e41e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/active_admin/mongoid/document.rb
Original file line number Diff line number Diff line change
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
6 changes: 5 additions & 1 deletion lib/active_admin/mongoid/helpers/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module Collection
original_collection_size = instance_method(:collection_size)
def collection_size(collection=collection)
if(not collection.empty? and collection.first.class.included_modules.include?(Mongoid::Document))
collection.count(true)
if collection.first.class.embedded?
collection.count
else
collection.count(true)
end
else
original_collection_size(collection)
end
Expand Down
42 changes: 40 additions & 2 deletions spec/features/smoke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,46 @@
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', 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 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'

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

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

end

end
31 changes: 31 additions & 0 deletions test_app/app/admin/posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,35 @@
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
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

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
9 changes: 9 additions & 0 deletions test_app/app/models/author.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Author
include Mongoid::Document
include Mongoid::Timestamps

embedded_in :post
embeds_one :city

field :name
end
7 changes: 7 additions & 0 deletions test_app/app/models/city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class City
include Mongoid::Document
include Mongoid::Timestamps

embedded_in :author
field :name
end
3 changes: 3 additions & 0 deletions test_app/app/models/post.rb
Original file line number Diff line number Diff line change
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.city.name'
end

0 comments on commit f33e41e

Please sign in to comment.