From d80576bc365ffe7ee117e0d00e6c2321e75be9cb Mon Sep 17 00:00:00 2001 From: Daniel Gaytan Date: Fri, 28 Feb 2014 18:46:59 -0600 Subject: [PATCH 1/3] Count embedded relations --- lib/active_admin/mongoid/helpers/collection.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From 18cfcfe734104137f841186375348f28deea58d1 Mon Sep 17 00:00:00 2001 From: Daniel Gaytan Date: Thu, 6 Mar 2014 12:20:03 -0600 Subject: [PATCH 2/3] Sort by alone fields of by embedded fields --- lib/active_admin/mongoid/document.rb | 7 +++++- spec/features/smoke_spec.rb | 32 ++++++++++++++++++++++++++-- test_app/app/admin/posts.rb | 27 +++++++++++++++++++++++ test_app/app/models/author.rb | 7 ++++++ test_app/app/models/post.rb | 3 +++ 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 test_app/app/models/author.rb 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/spec/features/smoke_spec.rb b/spec/features/smoke_spec.rb index dfc52c4..20f0fe7 100644 --- a/spec/features/smoke_spec.rb +++ b/spec/features/smoke_spec.rb @@ -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') @@ -244,7 +274,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..a1c42b2 100644 --- a/test_app/app/admin/posts.rb +++ b/test_app/app/admin/posts.rb @@ -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 diff --git a/test_app/app/models/author.rb b/test_app/app/models/author.rb new file mode 100644 index 0000000..27d1659 --- /dev/null +++ b/test_app/app/models/author.rb @@ -0,0 +1,7 @@ +class Author + include Mongoid::Document + include Mongoid::Timestamps + + embedded_in :post + field :name +end diff --git a/test_app/app/models/post.rb b/test_app/app/models/post.rb index aa9ada7..a7b8125 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.name' end From 74b5edf7aa865960c3d340c09bfd5e26a0eb83ac Mon Sep 17 00:00:00 2001 From: Daniel Gaytan Date: Thu, 6 Mar 2014 12:36:45 -0600 Subject: [PATCH 3/3] Ensure an embedded object within an embedded object can be sorted --- spec/features/smoke_spec.rb | 16 +++++++++++++--- test_app/app/admin/posts.rb | 4 ++++ test_app/app/models/author.rb | 2 ++ test_app/app/models/city.rb | 7 +++++++ test_app/app/models/post.rb | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test_app/app/models/city.rb diff --git a/spec/features/smoke_spec.rb b/spec/features/smoke_spec.rb index 20f0fe7..ea29bc2 100644 --- a/spec/features/smoke_spec.rb +++ b/spec/features/smoke_spec.rb @@ -231,12 +231,13 @@ 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.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 author name' do + 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' @@ -244,6 +245,15 @@ 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 diff --git a/test_app/app/admin/posts.rb b/test_app/app/admin/posts.rb index a1c42b2..a0adec8 100644 --- a/test_app/app/admin/posts.rb +++ b/test_app/app/admin/posts.rb @@ -16,6 +16,10 @@ 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 diff --git a/test_app/app/models/author.rb b/test_app/app/models/author.rb index 27d1659..c42240b 100644 --- a/test_app/app/models/author.rb +++ b/test_app/app/models/author.rb @@ -3,5 +3,7 @@ class Author 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 a7b8125..57b9704 100644 --- a/test_app/app/models/post.rb +++ b/test_app/app/models/post.rb @@ -9,5 +9,5 @@ class Post belongs_to :other_user, class_name: 'AdminUser' embeds_one :author - field :'author.name' + field :'author.city.name' end