activeadmin-mongoid 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codecheck.yml +18 -0
  3. data/.github/workflows/tests.yml +45 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +104 -0
  6. data/CHANGELOG.md +28 -0
  7. data/Gemfile +11 -8
  8. data/README.md +17 -3
  9. data/activeadmin-mongoid.gemspec +9 -9
  10. data/lib/active_admin/mongoid/association/relatable.rb +7 -1
  11. data/lib/active_admin/mongoid/controllers/resource_controller.rb +9 -10
  12. data/lib/active_admin/mongoid/criteria.rb +0 -1
  13. data/lib/active_admin/mongoid/csv_builder.rb +11 -6
  14. data/lib/active_admin/mongoid/document.rb +4 -16
  15. data/lib/active_admin/mongoid/filter_form_builder.rb +4 -4
  16. data/lib/active_admin/mongoid/filters/resource_extension.rb +4 -2
  17. data/lib/active_admin/mongoid/helpers/collection.rb +3 -1
  18. data/lib/active_admin/mongoid/inputs/filters/select_input.rb +15 -6
  19. data/lib/active_admin/mongoid/resource/attributes.rb +2 -3
  20. data/lib/active_admin/mongoid/resource.rb +1 -1
  21. data/lib/active_admin/mongoid/version.rb +1 -1
  22. data/lib/activeadmin-mongoid.rb +26 -0
  23. data/lib/meta_search/searches/mongoid.rb +4 -4
  24. data/spec/features/smoke_spec.rb +20 -6
  25. data/spec/lib/active_admin/mongoid/filter_form_builder_spec.rb +5 -7
  26. data/spec/lib/active_admin/mongoid/filters/resource_extension_spec.rb +19 -0
  27. data/spec/spec_helper.rb +3 -3
  28. data/spec/support/capybara.rb +0 -2
  29. data/spec/support/debug.rb +2 -2
  30. data/spec/support/mongoid.rb +2 -2
  31. data/tasks/js.rake +0 -2
  32. data/tasks/test.rake +2 -6
  33. data/test_app/Rakefile +1 -1
  34. data/test_app/app/admin/admin_users.rb +0 -1
  35. data/test_app/app/admin/dashboard.rb +4 -5
  36. data/test_app/app/admin/posts.rb +1 -1
  37. data/test_app/app/assets/config/manifest.js +1 -0
  38. data/test_app/app/models/admin_user.rb +11 -11
  39. data/test_app/app/models/post.rb +2 -0
  40. data/test_app/bin/rails +4 -0
  41. data/test_app/bin/rake +4 -0
  42. data/test_app/bin/setup +25 -0
  43. data/test_app/config/application.rb +1 -1
  44. data/test_app/config/boot.rb +4 -5
  45. data/test_app/config/environment.rb +3 -3
  46. data/test_app/config/environments/development.rb +2 -1
  47. data/test_app/config/environments/production.rb +0 -1
  48. data/test_app/config/environments/test.rb +3 -2
  49. data/test_app/config/initializers/active_admin.rb +0 -13
  50. data/test_app/config/initializers/inflections.rb +6 -5
  51. data/test_app/config/initializers/wrap_parameters.rb +0 -1
  52. data/test_app/config/mongoid.7.yml +2 -3
  53. data/test_app/config/mongoid.8.yml +13 -0
  54. data/test_app/config.ru +1 -1
  55. data/test_app/db/migrate/20130514223107_create_admin_notes.rb +3 -3
  56. data/test_app/db/migrate/20130514223108_move_admin_notes_to_comments.rb +2 -2
  57. data/test_app/script/rails +2 -2
  58. data/test_app/test/test_helper.rb +1 -1
  59. metadata +41 -24
  60. data/.travis.yml +0 -12
  61. data/Gemfile.lock +0 -284
  62. data/test_app/config/mongoid.2.yml +0 -20
  63. data/test_app/config/mongoid.3.yml +0 -96
  64. data/test_app/config/mongoid.4.yml +0 -87
  65. data/test_app/config/mongoid.5.yml +0 -7
  66. data/test_app/config/mongoid.6.yml +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be7b4cf7a8d6adf31b32f4a10f1719f04b859fd62c062240a2d2519d70cfc739
4
- data.tar.gz: '09d014d82d99ebadda2c05ed1f06327ed99931ee371fb2433ddf0806460a1017'
3
+ metadata.gz: 6e88a5c57c831a91f2d9359ad41814f85685f33d9312ac4d98c9b8240b310555
4
+ data.tar.gz: 89803e894a8fef6d88f48bef7778b2199e14d51c714e75c0b6997cc46b128ac8
5
5
  SHA512:
6
- metadata.gz: d3cc3ccc47e25617aae19eddc3db1af073df07a22f39338297616bfe1f41eb29b734b18399a7f8409712a7837eb694de4344145af7d884b64210670acadca9af
7
- data.tar.gz: 4b9b1da89366e1617206aa7a76aba19a54ed91240e0834588caff0f89b9a38f66ea9a89b42dcac0650f457cb90f3bd4c1c82cf47e11ef4e911e33f62b2613890
6
+ metadata.gz: aba3dc5070f97ef7f015a546b4be222be5561bef3a1a86b011390ba634453f95c1fc973d9c37a76eecc3b9c76d97680851c90f162f761d9ced6eb299df8d0ff7
7
+ data.tar.gz: b34d932081070a55f7c6a344e4bc0683665685304cf7f7616e2bedec7bad7b2427495b24d741d3aaa5b762067c5690c9c6c70c88fb515b767a1153f0cd792ebd
@@ -0,0 +1,18 @@
1
+ name: Codecheck
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ pull_request:
7
+ branches:
8
+ - master
9
+ jobs:
10
+ codecheck:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: 3.3
17
+ bundler-cache: true
18
+ - run: bundle exec rubocop
@@ -0,0 +1,45 @@
1
+ name: Tests
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ pull_request:
7
+ branches:
8
+ - master
9
+ jobs:
10
+ test:
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ ruby: ['3.1', '3.2', '3.3', '3.4', '4.0']
15
+ rails: ['6.1', '7.0']
16
+ mongoid: ['7.5', '8.1']
17
+ exclude:
18
+ # Rails 6.1 doesn't support Ruby 3.4 or later.
19
+ - ruby: '3.4'
20
+ rails: '6.1'
21
+ - ruby: '4.0'
22
+ rails: '6.1'
23
+ # ubuntu-latest (26.04) can't compile native extensions against Ruby 3.2's headers.
24
+ runs-on: ubuntu-24.04
25
+ name: Ruby ${{ matrix.ruby }}, Rails ${{ matrix.rails }}, Mongoid ${{ matrix.mongoid }}
26
+ env:
27
+ RAILS_VERSION: ${{ matrix.rails }}
28
+ MONGOID_VERSION: ${{ matrix.mongoid }}
29
+
30
+ services:
31
+ mongodb:
32
+ image: mongo:7
33
+ ports:
34
+ - 27017:27017
35
+
36
+ steps:
37
+ - uses: actions/checkout@v4
38
+ - uses: ruby/setup-ruby@v1
39
+ with:
40
+ ruby-version: ${{ matrix.ruby }}
41
+ # Not setup-ruby's bundler-cache: it locks the Gemfile without this job's RAILS_VERSION and
42
+ # MONGOID_VERSION. setup-ruby still writes that default lockfile, and moving it to this job's
43
+ # versions can fail to resolve, so resolve from scratch.
44
+ - run: rm -f Gemfile.lock && bundle install
45
+ - run: bundle exec rake
data/.gitignore CHANGED
@@ -21,3 +21,5 @@ spec/reports
21
21
  /test_app/.sass-cache
22
22
  /test_app/config/mongoid.yml
23
23
  /spec/rails
24
+ .ruby-version
25
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,104 @@
1
+ AllCops:
2
+ DisabledByDefault: true
3
+ TargetRubyVersion: 2.4
4
+
5
+ DisplayCopNames: true
6
+
7
+ StyleGuideCopsOnly: false
8
+
9
+ Layout/EndAlignment:
10
+ Enabled: true
11
+
12
+ Lint/ParenthesesAsGroupedExpression:
13
+ Enabled: true
14
+
15
+ Layout/AccessModifierIndentation:
16
+ Enabled: true
17
+
18
+ Layout/CaseIndentation:
19
+ Enabled: true
20
+
21
+ Layout/CommentIndentation:
22
+ Enabled: true
23
+
24
+ Layout/ElseAlignment:
25
+ Enabled: true
26
+
27
+ Layout/EmptyLines:
28
+ Enabled: true
29
+
30
+ Layout/EmptyLinesAroundBlockBody:
31
+ Enabled: true
32
+
33
+ Layout/EndOfLine:
34
+ Enabled: true
35
+
36
+ Layout/ExtraSpacing:
37
+ Enabled: true
38
+
39
+ Style/Dir:
40
+ Enabled: true
41
+
42
+ Style/Encoding:
43
+ Enabled: true
44
+
45
+ Style/ExpandPathArguments:
46
+ Enabled: true
47
+
48
+ Style/FrozenStringLiteralComment:
49
+ Enabled: true
50
+ EnforcedStyle: never
51
+
52
+ Style/HashSyntax:
53
+ Enabled: true
54
+
55
+ Style/ParallelAssignment:
56
+ Enabled: true
57
+
58
+ Layout/IndentationConsistency:
59
+ Enabled: true
60
+
61
+ Layout/IndentationWidth:
62
+ Enabled: true
63
+
64
+ Naming/PredicateName:
65
+ Enabled: true
66
+
67
+ ForbiddenPrefixes:
68
+ - is_
69
+ - have_
70
+
71
+ AllowedMethods:
72
+ - has_many
73
+ - has_many_actions
74
+ - is_association?
75
+
76
+ Style/TrailingCommaInArguments:
77
+ Enabled: true
78
+
79
+ Layout/TrailingEmptyLines:
80
+ Enabled: true
81
+
82
+ Layout/TrailingWhitespace:
83
+ Enabled: true
84
+
85
+ Layout/SpaceAfterComma:
86
+ Enabled: true
87
+
88
+ Layout/SpaceAroundEqualsInParameterDefault:
89
+ Enabled: true
90
+
91
+ Layout/SpaceAroundOperators:
92
+ Enabled: true
93
+
94
+ Layout/SpaceBeforeBlockBraces:
95
+ Enabled: true
96
+
97
+ Layout/SpaceInsideBlockBraces:
98
+ Enabled: true
99
+
100
+ Layout/SpaceInsideHashLiteralBraces:
101
+ Enabled: true
102
+
103
+ Layout/SpaceInsideParens:
104
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ ## 2.0.0
4
+
5
+ Supports Ruby 3.1+, Rails 6.1 and 7.0, Mongoid 7 and 8, and ActiveAdmin 2.x. Use 1.0.0 for older
6
+ versions.
7
+
8
+ ### Breaking changes
9
+
10
+ * Requires Ruby 3.1 or later, Mongoid 7.0 or later, and ActiveAdmin 2.x.
11
+ * `build_new_resource` no longer passes `resource_params` to the build method. ActiveAdmin assigns
12
+ them right after building, so they were being assigned twice.
13
+
14
+ ### Fixes
15
+
16
+ * Rails 7.0: the resource controller patch is applied in `to_prepare`, since Rails 7 doesn't autoload
17
+ during initialization.
18
+ * ActiveAdmin 2.14: CSV export no longer goes through `in_paginated_batches`, which needs
19
+ ActiveRecord. Like it, the export skips the query cache.
20
+ * Ruby 3: CSV options are passed as keyword arguments.
21
+ * Select filters search the key the document stores, so filters on `belongs_to` and
22
+ `has_and_belongs_to_many` associations work.
23
+ * `has_many` and `has_one` associations are no longer default filters, since their keys are in the
24
+ related documents. Declaring one explicitly raises an error that says so.
25
+ * The index page's "Displaying X - Y" counts the current page in the database instead of loading
26
+ documents (and no longer counts the whole collection).
27
+ * `primary_key` works for subclasses of a document.
28
+ * Declares the `csv` dependency, which Ruby 3.4 no longer installs by default.
data/Gemfile CHANGED
@@ -2,21 +2,24 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'ransack-mongoid', git: 'https://github.com/activerecord-hackery/ransack-mongoid'
5
+ # ransack-mongoid 0.x was never released to RubyGems. Pinned to the last commit that supports
6
+ # ransack 2.x, which ActiveAdmin 2 requires.
7
+ gem 'ransack-mongoid', git: 'https://github.com/riverock/ransack-mongoid', ref: 'f83ae55ea67fc0a0bca30158f3e04c49362f4464'
6
8
 
7
- gem 'rails', '>= 5.2', '< 6.1'
8
-
9
- gem 'devise'
10
-
11
- gem 'pry-rails'
9
+ # CI picks the versions; see .github/workflows/tests.yml.
10
+ gem 'rails', "~> #{ENV.fetch('RAILS_VERSION', '7.0')}.0"
11
+ gem 'mongoid', "~> #{ENV.fetch('MONGOID_VERSION', '8.1')}.0"
12
12
 
13
+ gem 'devise', '~> 4.0' # ActiveAdmin 2 requires < 5
13
14
  gem 'jquery-rails'
14
15
  gem 'jquery-ui-rails'
15
- gem 'jslint'
16
16
 
17
17
  group :test do
18
18
  gem 'capybara'
19
- gem 'poltergeist'
20
19
  gem 'launchy'
21
20
  gem 'simplecov', require: false
22
21
  end
22
+
23
+ group :lint do
24
+ gem 'rubocop', '0.80.0'
25
+ end
data/README.md CHANGED
@@ -1,11 +1,25 @@
1
1
  # ActiveAdmin-Mongoid
2
2
  [![Version ][rubygems_badge]][rubygems]
3
- [![Build Status](https://travis-ci.org/activeadmin/activeadmin-mongoid.svg?branch=master)](https://travis-ci.org/activeadmin/activeadmin-mongoid)
3
+ ![Tests](https://github.com/activeadmin/activeadmin-mongoid/actions/workflows/tests.yml/badge.svg)
4
+ ![Codecheck](https://github.com/activeadmin/activeadmin-mongoid/actions/workflows/codecheck.yml/badge.svg)
4
5
 
5
6
  ## Updates
6
7
 
7
8
  ActiveAdmin is holding off on pulling Mongoid support into the core ActiveAdmin application. This repo was pulled into the ActiveAdmin org from previous work done by Elia Schito, and will be maintained by Grzegorz Jakubiak, Nic Boie, JD Guzman, Elia Schito and other ActiveAdmin and community members.
8
9
 
10
+ ### Requirements for version 2.0.0
11
+ * Ruby 3.1 or greater (Ruby 3.4 and later need Rails 7.0).
12
+ * Rails 6.1 or 7.0
13
+ * Mongoid 7.x or 8.x
14
+ * ActiveAdmin 2.x (tested on 2.14)
15
+ * `ransack-mongoid` - declare it in your Gemfile from GitHub; it has no RubyGems release that supports ransack 2.x or 3.x, which ActiveAdmin 2 requires:
16
+
17
+ ```ruby
18
+ gem 'ransack-mongoid', github: 'riverock/ransack-mongoid', ref: 'f83ae55ea67fc0a0bca30158f3e04c49362f4464'
19
+ ```
20
+
21
+ See [CHANGELOG.md](CHANGELOG.md) for the changes from 1.0.0.
22
+
9
23
  ### Requirements for version 1.0.0
10
24
  * Ruby 2.4.0 or greater.
11
25
  * Tested working on Rails 5.2.3
@@ -38,8 +52,8 @@ For more on Mongoid support in ActiveAdmin see [this issue](https://github.com/g
38
52
  Add the following gems to your application's Gemfile, and lock the version:
39
53
 
40
54
  ```ruby
41
- gem 'activeadmin-mongoid', '1.0.0'
42
- gem 'ransack-mongoid', github: 'activerecord-hackery/ransack-mongoid'
55
+ gem 'activeadmin-mongoid', '~> 2.0'
56
+ gem 'ransack-mongoid', github: 'riverock/ransack-mongoid', ref: 'f83ae55ea67fc0a0bca30158f3e04c49362f4464'
43
57
  ```
44
58
 
45
59
  You can safely remove the following lines, since are already activeadmin-mongoid dependencies:
@@ -1,5 +1,4 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'active_admin/mongoid/version'
5
4
 
@@ -8,9 +7,9 @@ Gem::Specification.new do |gem|
8
7
  gem.email = ['elia@schito.me']
9
8
  gem.description = %q{ActiveAdmin hacks to support Mongoid (some ActiveAdmin features are disabled)}
10
9
  gem.summary = %q{ActiveAdmin hacks to support Mongoid}
11
- gem.homepage = ''
10
+ gem.homepage = 'https://github.com/activeadmin/activeadmin-mongoid'
12
11
 
13
- gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
14
13
  gem.files = `git ls-files`.split("\n")
15
14
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
15
  gem.name = 'activeadmin-mongoid'
@@ -18,13 +17,14 @@ Gem::Specification.new do |gem|
18
17
  gem.version = ActiveAdmin::Mongoid::VERSION
19
18
  gem.license = 'MIT'
20
19
 
21
- gem.required_ruby_version = '>= 2.4'
20
+ gem.required_ruby_version = '>= 3.1'
22
21
 
23
- gem.add_runtime_dependency 'mongoid', '>= 6.0', '< 8.a'
24
- gem.add_runtime_dependency 'activeadmin', '>= 1.4.3'
22
+ gem.add_runtime_dependency 'mongoid', '>= 7.0', '< 9'
23
+ gem.add_runtime_dependency 'activeadmin', '~> 2.0'
24
+ gem.add_runtime_dependency 'csv' # the CSV builder; no longer a default gem from Ruby 3.4
25
25
  gem.add_runtime_dependency 'kaminari-mongoid'
26
26
  gem.add_runtime_dependency 'jquery-rails'
27
- gem.add_runtime_dependency 'sass-rails', ['>= 3.1.4']
27
+ gem.add_runtime_dependency 'sass-rails', ['>= 3.1.4']
28
28
 
29
- gem.add_development_dependency 'rspec-rails', '~> 3.6'
29
+ gem.add_development_dependency 'rspec-rails', '~> 6.1'
30
30
  end
@@ -7,8 +7,14 @@ Mongoid::Association::Relatable.module_eval do
7
7
  [:embeds_one, :embeds_many].include?(macro)
8
8
  end
9
9
 
10
+ # Whether the association's key is stored in this document, so it can be filtered on.
11
+ # has_many and has_one keep theirs in the related document.
12
+ def key_stored_on_document?
13
+ [:belongs_to, :has_and_belongs_to_many].include?(macro)
14
+ end
15
+
10
16
  def foreign_key
11
17
  return if embeds?
12
18
  foreign_key.to_sym rescue nil
13
19
  end
14
- end
20
+ end
@@ -1,15 +1,14 @@
1
1
  require 'active_admin/engine'
2
2
 
3
- ActiveAdmin::Engine.module_eval do
4
-
5
- initializer 'active_admin.mongoid.resource_controller' do
6
- class ActiveAdmin::ResourceController
7
- def build_new_resource
8
- scoped_collection.send(
9
- method_for_build,
10
- *resource_params
11
- )
12
- end
3
+ # to_prepare, not an initializer: ResourceController inherits from InheritedResources::Base,
4
+ # which lives in that gem's app/controllers and can't be autoloaded during initialization
5
+ # on Rails 7.
6
+ ActiveAdmin::Engine.config.to_prepare do
7
+ ActiveAdmin::ResourceController.class_eval do
8
+ # No params: ActiveAdmin assigns resource_params right after building, and passing them here
9
+ # too assigned relation attributes twice.
10
+ def build_new_resource
11
+ scoped_collection.send(method_for_build)
13
12
  end
14
13
  end
15
14
  end
@@ -19,4 +19,3 @@ Mongoid::Criteria.class_eval do
19
19
  end
20
20
  end
21
21
  end
22
-
@@ -1,7 +1,6 @@
1
1
  module ActiveAdmin
2
2
  class CSVBuilder
3
3
  def build(controller, csv)
4
- @collection = controller.send :find_collection, except: :pagination
5
4
  columns = exec_columns controller.view_context
6
5
  options = ActiveAdmin.application.csv_options.merge self.options
7
6
  bom = options.delete :byte_order_mark
@@ -11,13 +10,19 @@ module ActiveAdmin
11
10
  csv << bom if bom
12
11
 
13
12
  if column_names
14
- csv << CSV.generate_line(columns.map{ |c| encode c.name, options }, csv_options)
13
+ csv << CSV.generate_line(columns.map { |c| encode c.name, options }, **csv_options)
15
14
  end
16
15
 
17
- (1..paginated_collection.total_pages).each do |page|
18
- paginated_collection(page).each do |resource|
19
- resource = controller.send :apply_decorator, resource
20
- csv << CSV.generate_line(build_row(resource, columns, options), csv_options)
16
+ # Not ActiveAdmin's in_paginated_batches: it wraps the loop in ActiveRecord::Base.uncached,
17
+ # and ActiveRecord isn't loaded in a Mongoid app. Skip Mongo's query cache the same way, so a
18
+ # large export doesn't keep every page cached until the request ends.
19
+ query_cache = defined?(::Mongo::QueryCache) ? ::Mongo::QueryCache : ::Mongoid::QueryCache
20
+ query_cache.uncached do
21
+ (1..controller.send(:paginated_collection).total_pages).each do |page|
22
+ controller.send(:paginated_collection, page).each do |resource|
23
+ resource = controller.send :apply_decorator, resource
24
+ csv << CSV.generate_line(build_row(resource, columns, options), **csv_options)
25
+ end
21
26
  end
22
27
  end
23
28
 
@@ -3,9 +3,6 @@ require 'delegate'
3
3
  module ActiveAdmin::Mongoid::Document
4
4
  extend ActiveSupport::Concern
5
5
 
6
-
7
-
8
-
9
6
  # PROXY CLASSES
10
7
 
11
8
  class ColumnWrapper < SimpleDelegator
@@ -30,24 +27,16 @@ module ActiveAdmin::Mongoid::Document
30
27
  end
31
28
  end
32
29
 
33
-
34
-
35
-
36
- # CLASS METHODS
37
-
38
30
  included do
39
- unless respond_to? :primary_key
40
- class << self
41
- attr_accessor :primary_key
42
- end
31
+ unless respond_to?(:primary_key) && respond_to?(:primary_key=)
32
+ class_attribute :primary_key, instance_writer: false
43
33
  end
44
34
 
45
- self.primary_key ||= :id
35
+ self.primary_key = :id if primary_key.nil?
46
36
 
47
37
  def column_for_attribute(name)
48
38
  self.class.fields[name.to_sym]
49
39
  end
50
-
51
40
  end
52
41
 
53
42
  module ClassMethods
@@ -63,7 +52,7 @@ module ActiveAdmin::Mongoid::Document
63
52
  end
64
53
 
65
54
  def find_by_id id
66
- find_by(:_id => id)
55
+ find_by(_id: id)
67
56
  end
68
57
 
69
58
  def quoted_table_name
@@ -74,7 +63,6 @@ module ActiveAdmin::Mongoid::Document
74
63
  @associations ||= new.associations
75
64
  end
76
65
 
77
-
78
66
  def reflections *a
79
67
  relations *a
80
68
  end
@@ -9,11 +9,11 @@ class ActiveAdmin::Filters::FormBuilder
9
9
  def default_input_type(method, options = {})
10
10
  if column = column_for(method)
11
11
  case column.type.name.downcase.to_sym
12
- when :date, :datetime, :time; :date_range
13
- when :string, :text, :object; :string
14
- when :float, :decimal; :numeric
12
+ when :date, :datetime, :time; :date_range
13
+ when :string, :text, :object; :string
14
+ when :float, :decimal; :numeric
15
15
  when :integer
16
- return :select if reflection_for(method.to_s.gsub('_id','').to_sym)
16
+ return :select if reflection_for(method.to_s.gsub('_id', '').to_sym)
17
17
  return :numeric
18
18
  end
19
19
  elsif is_association?(method)
@@ -3,8 +3,10 @@ require 'active_admin/filters/resource_extension'
3
3
  module ActiveAdmin::Filters::ResourceExtension
4
4
  def default_association_filters
5
5
  if resource_class.respond_to?(:reflect_on_all_associations)
6
- without_embedded = resource_class.reflect_on_all_associations.reject { |e| e.embeds? }
7
- poly, not_poly = without_embedded.partition{ |r| r.macro == :belongs_to && r.options[:polymorphic] }
6
+ # Only associations whose key this document stores can be filtered (not embedded ones,
7
+ # has_many or has_one).
8
+ filterable = resource_class.reflect_on_all_associations.select(&:key_stored_on_document?)
9
+ poly, not_poly = filterable.partition { |r| r.macro == :belongs_to && r.options[:polymorphic] }
8
10
 
9
11
  filters = poly.map(&:foreign_key) + not_poly.map(&:name)
10
12
  filters.map &:to_sym
@@ -8,7 +8,9 @@ module ActiveAdmin
8
8
  def collection_size(collection = nil)
9
9
  collection ||= self.collection
10
10
  if collection.is_a?(::Mongoid::Criteria)
11
- collection.entries.count
11
+ # Count in the database instead of loading the documents. Mongoid's count ignores the
12
+ # criteria's skip and limit, so pass them to get the size of the current page.
13
+ collection.count(collection.options.slice(:skip, :limit))
12
14
  else
13
15
  original_collection_size(collection)
14
16
  end
@@ -1,12 +1,21 @@
1
1
  require 'active_admin/inputs/filters/select_input'
2
2
 
3
3
  class ActiveAdmin::Inputs::Filters::SelectInput
4
+ # ActiveAdmin builds this from reflection.association_primary_key, which Mongoid associations
5
+ # don't have. Filter on the key the document stores instead (belongs_to and
6
+ # has_and_belongs_to_many). has_many and has_one keep their keys in the related documents, so
7
+ # a filter on them can't match anything.
4
8
  def searchable_method_name
5
- name = if searchable_has_many_through?
6
- "#{reflection.through_reflection.name}_#{reflection.foreign_key}"
7
- elsif reflection_searchable?
8
- reflection.key
9
- end
10
- (name == '_id') ? 'id' : name
9
+ return method unless reflection
10
+ return reflection.key if reflection.key_stored_on_document?
11
+
12
+ raise ArgumentError, "Can't filter #{@object.klass} by #{method}: Mongoid #{reflection.macro} " \
13
+ "associations aren't stored in the document. Remove `filter :#{method}`."
14
+ end
15
+
16
+ def input_name
17
+ return method if seems_searchable?
18
+
19
+ searchable_method_name&.to_s + (multiple? ? '_in' : '_eq')
11
20
  end
12
21
  end
@@ -6,8 +6,8 @@ module ActiveAdmin
6
6
  module Attributes
7
7
  def foreign_methods
8
8
  @foreign_methods ||= resource_class.reflect_on_all_associations.
9
- select{ |r| r.macro == :belongs_to }.
10
- index_by{ |r| r.foreign_key.to_sym }
9
+ select { |r| r.macro == :belongs_to }.
10
+ index_by { |r| r.foreign_key.to_sym }
11
11
  end
12
12
 
13
13
  def primary_col?(c)
@@ -20,4 +20,3 @@ module ActiveAdmin
20
20
  end
21
21
  end
22
22
  end
23
-
@@ -6,7 +6,7 @@ module ActiveAdmin
6
6
  # another. I'm not going to investigate any deeper, let's just remove the
7
7
  # underscore prefixed fields as was the prior behavior
8
8
  def default_filters
9
- super.reject { |filter| filter == :_id }
9
+ super.reject { |filter| filter == :_id }
10
10
  end
11
11
 
12
12
  module Attributes
@@ -1,5 +1,5 @@
1
1
  module ActiveAdmin
2
2
  module Mongoid
3
- VERSION = '1.0.0'
3
+ VERSION = '2.0.0'
4
4
  end
5
5
  end
@@ -1 +1,27 @@
1
1
  require 'active_admin/mongoid'
2
+ require "rails/generators/actions"
3
+ require "rails/generators/named_base"
4
+
5
+ # Considering the Rails::Generators::NamedBase is one of the nearest ancestor to
6
+ # ActiveAdmin::Generators::InstallGenerator, we can open the class and an empty create_migration
7
+ # to the class(which will overridden by other subclasses). We can specifically focus on the
8
+ # ActiveAdmin::Generators::InstallGenerator class and apply remove_method during the method_added call
9
+ # and thereby pushing ActiveAdmin::Generators::InstallGenerator to use our empty create_migrations method.
10
+
11
+ Rails::Generators::NamedBase.class_eval do
12
+ def create_migrations
13
+ end
14
+
15
+ def self.inherited(klass)
16
+ super
17
+ if klass.name == "ActiveAdmin::Generators::InstallGenerator"
18
+
19
+ klass.class_eval do
20
+ def self.method_added(method_name)
21
+ super
22
+ remove_method method_name if method_name == :create_migrations
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -10,7 +10,8 @@ module MetaSearch
10
10
  def initialize relation, params, options
11
11
  super(relation)
12
12
  @relation = relation
13
- @params, @options = params, options
13
+ @params = params
14
+ @options = options
14
15
  end
15
16
 
16
17
  def build
@@ -78,7 +79,6 @@ module MetaSearch
78
79
  relation.send(name, *attrs, &block)
79
80
  end
80
81
 
81
-
82
82
  def respond_to? name, include_private = false
83
83
  name.to_s =~ metasearch_regexp or super
84
84
  end
@@ -92,8 +92,8 @@ module MetaSearch
92
92
  end
93
93
 
94
94
  def metasearch_regexp
95
- field_names = klass.fields.map{ |field| field.second.name }
96
- conditions = MetaSearch::DEFAULT_WHERES.map {|condition| condition[0...-1]} # pop tail options
95
+ field_names = klass.fields.map { |field| field.second.name }
96
+ conditions = MetaSearch::DEFAULT_WHERES.map { |condition| condition[0...-1] } # pop tail options
97
97
 
98
98
  /\A(#{field_names.join('|')})_(#{conditions.join('|')})\z/
99
99
  end