diff --git a/lib/active_admin/mongoid/document.rb b/lib/active_admin/mongoid/document.rb index 98cac8f..88a01fd 100644 --- a/lib/active_admin/mongoid/document.rb +++ b/lib/active_admin/mongoid/document.rb @@ -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 diff --git a/lib/active_admin/mongoid/helpers/collection.rb b/lib/active_admin/mongoid/helpers/collection.rb index ef405d4..b9631c0 100644 --- a/lib/active_admin/mongoid/helpers/collection.rb +++ b/lib/active_admin/mongoid/helpers/collection.rb @@ -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 diff --git a/spec/features/smoke_spec.rb b/spec/features/smoke_spec.rb index dfc52c4..ea29bc2 100644 --- a/spec/features/smoke_spec.rb +++ b/spec/features/smoke_spec.rb @@ -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') @@ -244,7 +284,5 @@ end end end # context 'with 100 posts' - end - end diff --git a/test_app/app/admin/posts.rb b/test_app/app/admin/posts.rb index 4d3e08b..a0adec8 100644 --- a/test_app/app/admin/posts.rb +++ b/test_app/app/admin/posts.rb @@ -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 diff --git a/test_app/app/models/author.rb b/test_app/app/models/author.rb new file mode 100644 index 0000000..c42240b --- /dev/null +++ b/test_app/app/models/author.rb @@ -0,0 +1,9 @@ +class Author + include Mongoid::Document + include Mongoid::Timestamps + + embedded_in :post + embeds_one :city + + field :name +end diff --git a/test_app/app/models/city.rb b/test_app/app/models/city.rb new file mode 100644 index 0000000..eb31732 --- /dev/null +++ b/test_app/app/models/city.rb @@ -0,0 +1,7 @@ +class City + include Mongoid::Document + include Mongoid::Timestamps + + embedded_in :author + field :name +end diff --git a/test_app/app/models/post.rb b/test_app/app/models/post.rb index aa9ada7..57b9704 100644 --- a/test_app/app/models/post.rb +++ b/test_app/app/models/post.rb @@ -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