diff --git a/Gemfile b/Gemfile index ab7397d..d44cf4e 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,8 @@ source 'https://rubygems.org' # development dependencies will be added by default to the :development group. gemspec -gem 'ransack', github: 'activerecord-hackery/ransack', ref: 'c869fc210500' +gem 'ransack', github: 'activerecord-hackery/ransack' +gem 'ransack-mongoid', github: 'activerecord-hackery/ransack-mongoid' # Test app stuff diff --git a/README.md b/README.md index 6f5e946..fd334e8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ActiveAdmin is holding off on pulling Mongoid support into the core ActiveAdmin * Ruby 2.2.2 or greater. (Note, ruby-2.4.0 fails specs, see [this issue](https://github.com/DatabaseCleaner/database_cleaner/issues/466)) * Requires Rails 5.0.x (also tested working on Rails 5.1.x) * Mongoid 6.x (**WARNING**:, using a Mongoid version >= 6.1.x has resulted in a fair amount of errors seen in the wild. Test your upgrade very carefully with any apps in which you're using this gem with Mongoid >= 6.1.x!) -* ActiveAdmin 1.1 (Not working above 1.1.0 at this time) +* ActiveAdmin 1.3 ## Previous versions * Rails 4.x with Mongoid 5.x use branch rails4-mongoid5 @@ -45,8 +45,6 @@ You can safely remove the following lines, since are already activeadmin-mongoid ```ruby gem 'activeadmin' -gem 'meta_search', '>= 1.1.0.pre' -gem 'sass-rails', ['~> 3.1', '>= 3.1.4'] ``` ### Remove Application Dependencies diff --git a/activeadmin-mongoid.gemspec b/activeadmin-mongoid.gemspec index 64c99ae..fd276f4 100644 --- a/activeadmin-mongoid.gemspec +++ b/activeadmin-mongoid.gemspec @@ -19,10 +19,9 @@ Gem::Specification.new do |gem| gem.license = 'MIT' gem.add_runtime_dependency 'mongoid', ['~> 6.0'] - gem.add_runtime_dependency 'activeadmin', ['<= 1.1.0'] + gem.add_runtime_dependency 'activeadmin', '~> 1.3.0' gem.add_runtime_dependency 'jquery-rails' - gem.add_runtime_dependency 'sass-rails', ['>= 3.1.4', '<= 5.0.6'] + gem.add_runtime_dependency 'sass-rails', ['>= 3.1.4'] gem.add_development_dependency 'rspec-rails', '~> 3.6' - gem.add_development_dependency 'simplecov' end diff --git a/lib/active_admin-mongoid.rb b/lib/active_admin-mongoid.rb deleted file mode 100644 index d9f077b..0000000 --- a/lib/active_admin-mongoid.rb +++ /dev/null @@ -1 +0,0 @@ -require 'active_admin/mongoid' diff --git a/lib/active_admin/mongoid.rb b/lib/active_admin/mongoid.rb index 3280f52..bdf0384 100644 --- a/lib/active_admin/mongoid.rb +++ b/lib/active_admin/mongoid.rb @@ -14,9 +14,16 @@ require 'active_admin/mongoid/document' require 'active_admin/mongoid/helpers/collection' require 'active_admin/mongoid/criteria' - require 'active_admin/mongoid/order_clause' +require 'active_admin/mongoid/inputs/filters/check_boxes_input' +require 'active_admin/mongoid/inputs/filters/select_input' +require 'active_admin/mongoid/filters/active_filter' +require 'active_admin/mongoid/filters/resource_extension' +require 'active_admin/mongoid/controllers/resource_controller' +require 'active_admin/mongoid/resource/attributes' +require 'active_admin/mongoid/csv_builder' + module ActiveAdmin module Mongoid class Railtie < ::Rails::Railtie diff --git a/lib/active_admin/mongoid/controllers/resource_controller.rb b/lib/active_admin/mongoid/controllers/resource_controller.rb new file mode 100644 index 0000000..9d9cac4 --- /dev/null +++ b/lib/active_admin/mongoid/controllers/resource_controller.rb @@ -0,0 +1,15 @@ +require 'active_admin/engine' + +ActiveAdmin::Engine.module_eval do + + initializer 'active_admin.mongoid.resource_controller' do + class ActiveAdmin::ResourceController + def build_new_resource + scoped_collection.send( + method_for_build, + *resource_params + ) + end + end + end +end diff --git a/lib/active_admin/mongoid/csv_builder.rb b/lib/active_admin/mongoid/csv_builder.rb new file mode 100644 index 0000000..f391723 --- /dev/null +++ b/lib/active_admin/mongoid/csv_builder.rb @@ -0,0 +1,27 @@ +module ActiveAdmin + class CSVBuilder + def build(controller, csv) + @collection = controller.send :find_collection, except: :pagination + columns = exec_columns controller.view_context + options = ActiveAdmin.application.csv_options.merge self.options + bom = options.delete :byte_order_mark + column_names = options.delete(:column_names) { true } + csv_options = options.except :encoding_options, :humanize_name + + csv << bom if bom + + if column_names + csv << CSV.generate_line(columns.map{ |c| encode c.name, options }, csv_options) + end + + (1..paginated_collection.total_pages).each do |page| + paginated_collection(page).each do |resource| + resource = controller.send :apply_decorator, resource + csv << CSV.generate_line(build_row(resource, columns, options), csv_options) + end + end + + csv + end + end +end diff --git a/lib/active_admin/mongoid/filters/active_filter.rb b/lib/active_admin/mongoid/filters/active_filter.rb new file mode 100644 index 0000000..bcda9f5 --- /dev/null +++ b/lib/active_admin/mongoid/filters/active_filter.rb @@ -0,0 +1,15 @@ +require 'active_admin/filters/active_filter' +module ActiveAdmin + module Filters + + class ActiveFilter + def related_primary_key + if predicate_association + predicate_association.key + elsif related_class + related_class.key + end + end + end + end +end diff --git a/lib/active_admin/mongoid/filters/resource_extension.rb b/lib/active_admin/mongoid/filters/resource_extension.rb new file mode 100644 index 0000000..259a586 --- /dev/null +++ b/lib/active_admin/mongoid/filters/resource_extension.rb @@ -0,0 +1,14 @@ +require 'active_admin/filters/resource_extension' + +module ActiveAdmin::Filters::ResourceExtension + def default_association_filters + if resource_class.respond_to?(:reflect_on_all_associations) + poly, not_poly = resource_class.reflect_on_all_associations.partition{ |r| r.macro == :belongs_to && r.options[:polymorphic] } + + filters = poly.map(&:foreign_type) + not_poly.map(&:name) + filters.map &:to_sym + else + [] + end + end +end diff --git a/lib/active_admin/mongoid/inputs/filters/check_boxes_input.rb b/lib/active_admin/mongoid/inputs/filters/check_boxes_input.rb new file mode 100644 index 0000000..df5b866 --- /dev/null +++ b/lib/active_admin/mongoid/inputs/filters/check_boxes_input.rb @@ -0,0 +1,11 @@ +require 'active_admin/inputs/filters/check_boxes_input' + +class ActiveAdmin::Inputs::Filters::CheckBoxesInput + def searchable_method_name + if searchable_has_many_through? + "#{reflection.through_reflection.name}_#{reflection.foreign_key}" + else + reflection.key || method + end + end +end diff --git a/lib/active_admin/mongoid/inputs/filters/select_input.rb b/lib/active_admin/mongoid/inputs/filters/select_input.rb new file mode 100644 index 0000000..713fa8e --- /dev/null +++ b/lib/active_admin/mongoid/inputs/filters/select_input.rb @@ -0,0 +1,12 @@ +require 'active_admin/inputs/filters/select_input' + +class ActiveAdmin::Inputs::Filters::SelectInput + def searchable_method_name + name = if searchable_has_many_through? + "#{reflection.through_reflection.name}_#{reflection.foreign_key}" + elsif reflection_searchable? + reflection.key + end + (name == '_id') ? 'id' : name + end +end diff --git a/lib/active_admin/mongoid/order_clause.rb b/lib/active_admin/mongoid/order_clause.rb index a5defca..d650364 100644 --- a/lib/active_admin/mongoid/order_clause.rb +++ b/lib/active_admin/mongoid/order_clause.rb @@ -8,5 +8,9 @@ def to_sql def to_mongo_options { @column => @order.downcase.to_sym } end + + def apply(chain) + chain.reorder(sql) + end end end diff --git a/lib/active_admin/mongoid/resource/attributes.rb b/lib/active_admin/mongoid/resource/attributes.rb new file mode 100644 index 0000000..1001b0a --- /dev/null +++ b/lib/active_admin/mongoid/resource/attributes.rb @@ -0,0 +1,23 @@ +require 'active_admin/resource/attributes' + +module ActiveAdmin + + class Resource + module Attributes + def foreign_methods + @foreign_methods ||= resource_class.reflect_on_all_associations. + select{ |r| r.macro == :belongs_to }. + index_by{ |r| r.foreign_key.to_sym } + end + + def primary_col?(c) + c.name == '_id' + end + + def sti_col?(c) + false + end + end + end +end + diff --git a/spec/features/smoke_spec.rb b/spec/features/smoke_spec.rb index 1508baf..bdc2722 100644 --- a/spec/features/smoke_spec.rb +++ b/spec/features/smoke_spec.rb @@ -313,6 +313,11 @@ end end + it "builds csv" do + visit '/admin/admin_users.csv' + expect(page.status_code).to eq(200) or eq(304) + end + end end diff --git a/test_app/Gemfile b/test_app/Gemfile index b09a207..02d4483 100644 --- a/test_app/Gemfile +++ b/test_app/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' gem 'activeadmin-mongoid', path: '../' -gem 'activeadmin', '<= 1.1.0' +gem 'activeadmin', '~> 1.3.0' # Test app stuff @@ -14,7 +14,7 @@ gem 'devise' gem 'mongoid', '~> 6.0.3', require: true gem 'kaminari', '~> 1.0' gem 'kaminari-mongoid' -gem 'ransack' +gem 'ransack', '< 1.8.7' # Gems used only for assets and not required # in production environments by default. diff --git a/test_app/Gemfile.lock b/test_app/Gemfile.lock index 6ded5b9..ecdcef5 100644 --- a/test_app/Gemfile.lock +++ b/test_app/Gemfile.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - activeadmin-mongoid (0.4.0) - activeadmin (<= 1.1.0) + activeadmin-mongoid (0.5.0) + activeadmin (~> 1.2.0) jquery-rails mongoid (~> 6.0.3) sass-rails (>= 3.1.4, <= 5.0.6) @@ -33,13 +33,13 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activeadmin (1.1.0) + activeadmin (1.2.1) arbre (>= 1.1.1) coffee-rails formtastic (~> 3.1) formtastic_i18n inherited_resources (~> 1.7) - jquery-rails + jquery-rails (>= 4.2.0) kaminari (>= 0.15, < 2.0) railties (>= 4.2, < 5.2) ransack (~> 1.3) @@ -65,7 +65,7 @@ GEM activesupport (>= 3.0.0) arel (8.0.0) bcrypt (3.1.11) - bson (4.2.2) + bson (4.3.0) builder (3.2.3) capybara (2.15.1) addressable @@ -94,23 +94,23 @@ GEM docile (1.1.5) erubi (1.7.1) execjs (2.7.0) - ffi (1.9.18) + ffi (1.9.23) formtastic (3.1.5) actionpack (>= 3.2.13) formtastic_i18n (0.6.0) globalid (0.4.1) activesupport (>= 4.2.0) - has_scope (0.7.1) - actionpack (>= 4.1, < 5.2) - activesupport (>= 4.1, < 5.2) - i18n (1.0.0) + has_scope (0.7.2) + actionpack (>= 4.1) + activesupport (>= 4.1) + i18n (1.0.1) concurrent-ruby (~> 1.0) - inherited_resources (1.7.2) - actionpack (>= 3.2, < 5.2.x) + inherited_resources (1.8.0) + actionpack (>= 4.2, <= 5.2) has_scope (~> 0.6) - railties (>= 3.2, < 5.2.x) + railties (>= 4.2, <= 5.2) responders - jquery-rails (4.3.1) + jquery-rails (4.3.3) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) @@ -119,18 +119,18 @@ GEM jslint (1.2.0) json json (2.1.0) - kaminari (1.0.1) + kaminari (1.1.1) activesupport (>= 4.1.0) - kaminari-actionview (= 1.0.1) - kaminari-activerecord (= 1.0.1) - kaminari-core (= 1.0.1) - kaminari-actionview (1.0.1) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) actionview - kaminari-core (= 1.0.1) - kaminari-activerecord (1.0.1) + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) activerecord - kaminari-core (= 1.0.1) - kaminari-core (1.0.1) + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) kaminari-mongoid (1.0.1) kaminari-core (~> 1.0) mongoid @@ -145,8 +145,8 @@ GEM mini_mime (0.1.4) mini_portile2 (2.3.0) minitest (5.11.3) - mongo (2.4.3) - bson (>= 4.2.1, < 5.0.0) + mongo (2.5.1) + bson (>= 4.3.0, < 5.0.0) mongoid (6.0.3) activemodel (~> 5.0) mongo (~> 2.3) @@ -158,7 +158,7 @@ GEM capybara (~> 2.1) cliver (~> 0.3.1) websocket-driver (>= 0.2.0) - polyamorous (1.3.1) + polyamorous (1.3.3) activerecord (>= 3.0) pry (0.10.4) coderay (~> 1.1.0) @@ -192,19 +192,19 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (12.3.1) - ransack (1.8.3) + ransack (1.8.6) actionpack (>= 3.0) activerecord (>= 3.0) activesupport (>= 3.0) i18n - polyamorous (~> 1.3) - rb-fsevent (0.10.2) + polyamorous (~> 1.3.2) + rb-fsevent (0.10.3) rb-inotify (0.9.10) ffi (>= 0.5.0, < 2) responders (2.4.0) actionpack (>= 4.2.0, < 5.3) railties (>= 4.2.0, < 5.3) - sass (3.5.1) + sass (3.5.6) sass-listen (~> 4.0.0) sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) @@ -247,7 +247,7 @@ PLATFORMS ruby DEPENDENCIES - activeadmin (<= 1.1.0) + activeadmin (~> 1.2.0) activeadmin-mongoid! capybara coffee-rails @@ -262,7 +262,7 @@ DEPENDENCIES poltergeist pry rails (~> 5.1.6) - ransack + ransack (< 1.8.7) sass-rails simplecov uglifier diff --git a/test_app/config/initializers/active_admin.rb b/test_app/config/initializers/active_admin.rb index aa8c64c..95eb12e 100644 --- a/test_app/config/initializers/active_admin.rb +++ b/test_app/config/initializers/active_admin.rb @@ -211,3 +211,4 @@ end +