ransack-mongoid 1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4897a0458c8643349d13e6418daba40ec300b7568d315e1404fdbfebe532f112
4
+ data.tar.gz: 382c0b285ca824e90749cd36085fb84b5f73cd2a24d4156142e35de600c32c76
5
+ SHA512:
6
+ metadata.gz: cb71aaae7df4a523e23172ea005c61baffce0725d337d2fcb9b31a4f1ea65a2adc77f3b05c09b2946d720cb2c696227589ab85c23a15d8183d6167e7aca557a7
7
+ data.tar.gz: da7af02465a952f8761fcd55c107d0d5cbcc27f0902874c068aafe23ff8c920a771cc79ad92b92389f3a6d4bb6fe89880c7f8d8681d6844cd0e0f66b9e6c0a79
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ First release on RubyGems. Supports Ruby 3.1+, Ransack 4.x and Mongoid 7.x/8.x. For Ransack 1.x–3.x,
6
+ see "Older configurations" in the README.
7
+
8
+ * Ransack 4 support. Ransack 4 removed the adapter layer that loaded this gem, so the gem now loads
9
+ its Mongoid adapter when it's required, and re-registers its regex versions of the derived
10
+ predicates (`cont`, `start`, `end` and their negations) over Ransack's SQL `LIKE` versions.
11
+ * Searches with `%` in the value no longer raise `NameError` (uninitialized constant `Arel`) on
12
+ Ransack 4.1 and later.
13
+ * Conditions on several attributes joined with `and` (for example `name_and_email_cont`) no longer
14
+ raise `NameError` (uninitialized constant `Arel`).
15
+ * Requires an application that uses Mongoid only (see the README).
16
+ * Declares its `ransack` (>= 4.0, < 5) and `mongoid` (>= 7.0, < 9) dependencies.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ernie Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Ransack::Mongoid
2
+
3
+ Mongoid support for [Ransack](https://github.com/activerecord-hackery/ransack).
4
+
5
+ Ransack 4 only supports ActiveRecord, so this gem loads its Mongoid adapter itself when it's
6
+ required.
7
+
8
+ This gem continues the archived
9
+ [activerecord-hackery/ransack-mongoid](https://github.com/activerecord-hackery/ransack-mongoid), and
10
+ is maintained by JD Guzman and Nic Boie at
11
+ [riverock/ransack-mongoid](https://github.com/riverock/ransack-mongoid). It is also what
12
+ [activeadmin-mongoid](https://github.com/activeadmin/activeadmin-mongoid) uses for filtering.
13
+
14
+ ## Requirements
15
+
16
+ * Ruby 3.1 or greater
17
+ * Ransack 4.x
18
+ * Mongoid 7.x or 8.x
19
+ * An application that uses Mongoid only. Requiring this gem replaces parts of Ransack that its
20
+ ActiveRecord support also uses (the search context, conditions, the visitor and the `cont`,
21
+ `start` and `end` predicates), so Ransack searches on ActiveRecord models in the same
22
+ application stop working.
23
+
24
+ ### Older configurations
25
+
26
+ Version 1.0.0 supports Ransack 4 only. If you need Ransack 1.x–3.x or older versions of Mongoid,
27
+ use the archived [activerecord-hackery/ransack-mongoid](https://github.com/activerecord-hackery/ransack-mongoid)
28
+ from GitHub:
29
+
30
+ ```ruby
31
+ gem 'ransack-mongoid', github: 'activerecord-hackery/ransack-mongoid'
32
+ ```
33
+
34
+ The last commit of this repository before 1.0.0 also supports Ransack 2.x and 3.x, with a few
35
+ fixes the archived repository doesn't have. activeadmin-mongoid 2.x uses it:
36
+
37
+ ```ruby
38
+ gem 'ransack-mongoid', github: 'riverock/ransack-mongoid', ref: 'f83ae55ea67fc0a0bca30158f3e04c49362f4464'
39
+ ```
40
+
41
+ ## Installation
42
+
43
+ Add this line to your application's Gemfile:
44
+
45
+ ```ruby
46
+ gem 'ransack-mongoid', '~> 1.0'
47
+ ```
48
+
49
+ And then execute:
50
+
51
+ $ bundle
52
+
53
+ Or install it yourself as:
54
+
55
+ $ gem install ransack-mongoid
56
+
57
+ ## Usage
58
+
59
+ Search your Mongoid documents the way
60
+ [Ransack's documentation](https://github.com/activerecord-hackery/ransack) describes. Searching
61
+ through associations is limited, since Mongoid has no joins.
62
+
63
+ ## Development
64
+
65
+ The specs need a MongoDB server. `MONGODB_HOST` sets its address (default `localhost:27017`), and
66
+ `MONGOID_VERSION` picks the Mongoid version (default `8.1`):
67
+
68
+ $ MONGOID_VERSION=7.5 bundle install
69
+ $ MONGOID_VERSION=7.5 bundle exec rake
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,37 @@
1
+ require 'ransack/adapters/mongoid/attributes/predications'
2
+ require 'ransack/adapters/mongoid/attributes/order_predications'
3
+
4
+ module Ransack
5
+ module Adapters
6
+ module Mongoid
7
+ module Attributes
8
+ class Attribute < Struct.new :relation, :name
9
+ # include Arel::Expressions
10
+ # include Arel::Predications
11
+ # include Arel::AliasPredication
12
+ # include Arel::OrderPredications
13
+ # include Arel::Math
14
+
15
+ include ::Ransack::Adapters::Mongoid::Attributes::Predications
16
+ include ::Ransack::Adapters::Mongoid::Attributes::OrderPredications
17
+
18
+ ###
19
+ # Create a node for lowering this attribute
20
+ def lower
21
+ relation.lower self
22
+ end
23
+ end
24
+
25
+ class String < Attribute; end
26
+ class Time < Attribute; end
27
+ class Boolean < Attribute; end
28
+ class Decimal < Attribute; end
29
+ class Float < Attribute; end
30
+ class Integer < Attribute; end
31
+ class Undefined < Attribute; end
32
+ end
33
+
34
+ Attribute = Attributes::Attribute
35
+ end # Attributes
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module Ransack
2
+ module Adapters
3
+ module Mongoid
4
+ module Attributes
5
+ module OrderPredications
6
+ def asc
7
+ { name => :asc }
8
+ end
9
+
10
+ def desc
11
+ { name => :desc }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,141 @@
1
+ module Ransack
2
+ module Adapters
3
+ module Mongoid
4
+ module Attributes
5
+ module Predications
6
+ def not_eq(other)
7
+ { name => { '$ne' => other } }.to_inquiry
8
+ end
9
+
10
+ def not_eq_any(others)
11
+ grouping_any :not_eq, others
12
+ end
13
+
14
+ def not_eq_all(others)
15
+ grouping_all :not_eq, others
16
+ end
17
+
18
+ def eq(other)
19
+ { name => other }.to_inquiry
20
+ end
21
+
22
+ def eq_any(others)
23
+ grouping_any :eq, others
24
+ end
25
+
26
+ def eq_all(others)
27
+ grouping_all :eq, others
28
+ end
29
+
30
+ def in(other)
31
+ { name => { "$in" => other } }.to_inquiry
32
+ end
33
+
34
+ def in_any(others)
35
+ grouping_any :in, others
36
+ end
37
+
38
+ def in_all(others)
39
+ grouping_all :in, others
40
+ end
41
+
42
+ def not_in(other)
43
+ { "$not" => { name => { "$in" => other } } }.to_inquiry
44
+ end
45
+
46
+ def not_in_any(others)
47
+ grouping_any :not_in, others
48
+ end
49
+
50
+ def not_in_all(others)
51
+ grouping_all :not_in, others
52
+ end
53
+
54
+ def matches(other)
55
+ { name => /#{other}/i }.to_inquiry
56
+ end
57
+
58
+ def matches_any(others)
59
+ grouping_any :matches, others
60
+ end
61
+
62
+ def matches_all(others)
63
+ grouping_all :matches, others
64
+ end
65
+
66
+ def does_not_match(other)
67
+ { "$not" => { name => /#{other}/i } }.to_inquiry
68
+ end
69
+
70
+ def does_not_match_any(others)
71
+ grouping_any :does_not_match, others
72
+ end
73
+
74
+ def does_not_match_all(others)
75
+ grouping_all :does_not_match, others
76
+ end
77
+
78
+ def gteq(right)
79
+ { name => { '$gte' => right } }.to_inquiry
80
+ end
81
+
82
+ def gteq_any(others)
83
+ grouping_any :gteq, others
84
+ end
85
+
86
+ def gteq_all(others)
87
+ grouping_all :gteq, others
88
+ end
89
+
90
+ def gt(right)
91
+ { name => { '$gt' => right } }.to_inquiry
92
+ end
93
+
94
+ def gt_any(others)
95
+ grouping_any :gt, others
96
+ end
97
+
98
+ def gt_all(others)
99
+ grouping_all :gt, others
100
+ end
101
+
102
+ def lt(right)
103
+ { name => { '$lt' => right } }.to_inquiry
104
+ end
105
+
106
+ def lt_any(others)
107
+ grouping_any :lt, others
108
+ end
109
+
110
+ def lt_all(others)
111
+ grouping_all :lt, others
112
+ end
113
+
114
+ def lteq(right)
115
+ { name => { '$lte' => right } }.to_inquiry
116
+ end
117
+
118
+ def lteq_any(others)
119
+ grouping_any :lteq, others
120
+ end
121
+
122
+ def lteq_all(others)
123
+ grouping_all :lteq, others
124
+ end
125
+
126
+ private
127
+
128
+ def grouping_any(method_id, others)
129
+ nodes = others.map { |e| send(method_id, e) }
130
+ { "$or" => nodes }.to_inquiry
131
+ end
132
+
133
+ def grouping_all(method_id, others)
134
+ nodes = others.map { |e| send(method_id, e) }
135
+ { "$and" => nodes }.to_inquiry
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,142 @@
1
+ require 'delegate'
2
+
3
+ module Ransack
4
+ module Adapters
5
+ module Mongoid
6
+ module Base
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ class_attribute :_ransackers
12
+ class_attribute :_ransack_aliases
13
+ self._ransackers ||= {}
14
+ self._ransack_aliases ||= {}
15
+ end
16
+
17
+ class ColumnWrapper < SimpleDelegator
18
+ def type
19
+ _super = super
20
+ case _super
21
+ when BSON::ObjectId, Object
22
+ :string
23
+ else
24
+ _super.name.underscore.to_sym
25
+ end
26
+ end
27
+ end
28
+
29
+ class Connection
30
+ def initialize model
31
+ @model = model
32
+ end
33
+
34
+ def quote_column_name name
35
+ name
36
+ end
37
+ end
38
+
39
+ module ClassMethods
40
+ def ransack(params = {}, options = {})
41
+ params = params.presence || {}
42
+ Search.new(self, params ? params.delete_if {
43
+ |k, v| v.blank? && v != false } : params, options)
44
+ end
45
+
46
+ alias_method :search, :ransack
47
+
48
+ def ransack_alias(new_name, old_name)
49
+ self._ransack_aliases = _ransack_aliases.merge new_name.to_s =>
50
+ old_name.to_s
51
+ end
52
+
53
+ def ransacker(name, opts = {}, &block)
54
+ self._ransackers = _ransackers.merge name.to_s => Ransacker
55
+ .new(self, name, opts, &block)
56
+ end
57
+
58
+ def all_ransackable_attributes
59
+ ['id'] + column_names.select { |c| c != '_id' } + _ransackers.keys +
60
+ _ransack_aliases.keys
61
+ end
62
+
63
+ def ransackable_attributes(auth_object = nil)
64
+ all_ransackable_attributes
65
+ end
66
+
67
+ def ransortable_attributes(auth_object = nil)
68
+ # Here so users can overwrite the attributes
69
+ # that show up in the sort_select
70
+ ransackable_attributes(auth_object)
71
+ end
72
+
73
+ def ransackable_associations(auth_object = nil)
74
+ reflect_on_all_associations_all.map { |a| a.name.to_s }
75
+ end
76
+
77
+ def reflect_on_all_associations_all
78
+ reflect_on_all_associations(
79
+ :belongs_to, :has_one, :has_many, :embeds_many, :embedded_in
80
+ )
81
+ end
82
+
83
+ # For overriding with a whitelist of symbols
84
+ def ransackable_scopes(auth_object = nil)
85
+ []
86
+ end
87
+
88
+ # ransack_scope_skip_sanitize_args, by default, returns an empty array.
89
+ # i.e. use the sanitize_scope_args setting to determine if args should be converted.
90
+ # For overriding with a list of scopes which should be passed the args as-is.
91
+ #
92
+ def ransackable_scopes_skip_sanitize_args
93
+ []
94
+ end
95
+
96
+ # imitating active record
97
+
98
+ def joins_values *args
99
+ []
100
+ end
101
+
102
+ def custom_join_ast *args
103
+ []
104
+ end
105
+
106
+ def first(*args)
107
+ if args.size == 0
108
+ super
109
+ else
110
+ self.criteria.limit(args.first)
111
+ end
112
+ end
113
+
114
+ # def group_by *args, &block
115
+ # criteria
116
+ # end
117
+
118
+ def columns
119
+ @columns ||= fields.map(&:second).map{ |c| ColumnWrapper.new(c) }
120
+ end
121
+
122
+ def column_names
123
+ @column_names ||= fields.map(&:first)
124
+ end
125
+
126
+ def columns_hash
127
+ columns.index_by(&:name)
128
+ end
129
+
130
+ def table
131
+ name = ::Ransack::Adapters::Mongoid::Attributes::Attribute.new(
132
+ self.criteria, :name
133
+ )
134
+ { :name => name }
135
+ end
136
+
137
+ end
138
+
139
+ end # Base
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,212 @@
1
+ require 'ransack/context'
2
+ require 'polyamorous/polyamorous'
3
+
4
+ module Ransack
5
+ module Adapters
6
+ module Mongoid
7
+ class Context < ::Ransack::Context
8
+
9
+ def initialize(object, options = {})
10
+ super
11
+ # @arel_visitor = @engine.connection.visitor
12
+ end
13
+
14
+ def relation_for(object)
15
+ object.all
16
+ end
17
+
18
+ def type_for(attr)
19
+ return nil unless attr && attr.valid?
20
+ name = attr.arel_attribute.name.to_s.split('.').last
21
+ # table = attr.arel_attribute.relation.table_name
22
+
23
+ # schema_cache = @engine.connection.schema_cache
24
+ # raise "No table named #{table} exists" unless schema_cache.table_exists?(table)
25
+ # schema_cache.columns_hash(table)[name].type
26
+
27
+ # when :date
28
+ # when :datetime, :timestamp, :time
29
+ # when :boolean
30
+ # when :integer
31
+ # when :float
32
+ # when :decimal
33
+ # else # :string
34
+
35
+ name = '_id' if name == 'id'
36
+
37
+ t = object.klass.fields[name].try(:type) || bind_pair_for(attr.name).first.fields[name].type
38
+
39
+ t.to_s.demodulize.underscore.to_sym
40
+ end
41
+
42
+ def evaluate(search, opts = {})
43
+ viz = Visitor.new
44
+ relation = @object.where(viz.accept(search.base) || {})
45
+ if search.sorts.any?
46
+ ary_sorting = viz.accept(search.sorts)
47
+ sorting = {}
48
+ ary_sorting.each do |s|
49
+ sorting.merge! Hash[s.map { |k, d| [k.to_s == 'id' ? '_id' : k, d] }]
50
+ end
51
+ relation = relation.order_by(sorting)
52
+ # relation = relation.except(:order)
53
+ # .reorder(viz.accept(search.sorts))
54
+ end
55
+ # -- mongoid has different distinct method
56
+ # opts[:distinct] ? relation.distinct : relation
57
+ relation
58
+ end
59
+
60
+ def attribute_method?(str, klass = @klass)
61
+ exists = false
62
+ if ransackable_attribute?(str, klass)
63
+ exists = true
64
+ elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
65
+ remainder = []
66
+ found_assoc = nil
67
+ while !found_assoc && remainder.unshift(
68
+ segments.pop) && segments.size > 0 do
69
+ assoc, poly_class = unpolymorphize_association(
70
+ segments.join('_')
71
+ )
72
+ if found_assoc = get_association(assoc, klass)
73
+ exists = attribute_method?(remainder.join('_'),
74
+ poly_class || found_assoc.klass
75
+ )
76
+ end
77
+ end
78
+ end
79
+ exists
80
+ end
81
+
82
+ def table_for(parent)
83
+ # parent.table
84
+ Ransack::Adapters::Mongoid::Table.new(parent)
85
+ end
86
+
87
+ def klassify(obj)
88
+ if Class === obj && obj.ancestors.include?(::Mongoid::Document)
89
+ obj
90
+ elsif obj.respond_to? :klass
91
+ obj.klass
92
+ elsif obj.respond_to? :base_klass
93
+ obj.base_klass
94
+ else
95
+ raise ArgumentError, "Don't know how to klassify #{obj}"
96
+ end
97
+ end
98
+
99
+ def lock_association(association)
100
+ warn "lock_association is not implemented for Ransack mongoid adapter" if $DEBUG
101
+ end
102
+
103
+ def remove_association(association)
104
+ warn "remove_association is not implemented for Ransack mongoid adapter" if $DEBUG
105
+ end
106
+
107
+ private
108
+
109
+ def get_parent_and_attribute_name(str, parent = @base)
110
+ attr_name = nil
111
+
112
+ if ransackable_attribute?(str, klassify(parent))
113
+ attr_name = str
114
+ elsif (segments = str.split(Constants::UNDERSCORE)).size > 1
115
+ remainder = []
116
+ found_assoc = nil
117
+ while remainder.unshift(
118
+ segments.pop) && segments.size > 0 && !found_assoc do
119
+ assoc, klass = unpolymorphize_association(segments.join('_'))
120
+ if found_assoc = get_association(assoc, parent)
121
+ parent, attr_name = get_parent_and_attribute_name(
122
+ remainder.join('_'), found_assoc.klass
123
+ )
124
+ attr_name = "#{segments.join('_')}.#{attr_name}"
125
+ end
126
+ end
127
+ end
128
+
129
+ [parent, attr_name]
130
+ end
131
+
132
+ def get_association(str, parent = @base)
133
+ klass = klassify parent
134
+ ransackable_association?(str, klass) &&
135
+ klass.reflect_on_all_associations_all.detect { |a| a.name.to_s == str }
136
+ end
137
+
138
+ def join_dependency(relation)
139
+ if relation.respond_to?(:join_dependency) # Polyamorous enables this
140
+ relation.join_dependency
141
+ else
142
+ build_join_dependency(relation)
143
+ end
144
+ end
145
+
146
+ # Checkout active_record/relation/query_methods.rb +build_joins+ for
147
+ # reference. Lots of duplicated code maybe we can avoid it
148
+ def build_join_dependency(relation)
149
+ buckets = relation.joins_values.group_by do |join|
150
+ case join
151
+ when String
152
+ Constants::STRING_JOIN
153
+ when Hash, Symbol, Array
154
+ Constants::ASSOCIATION_JOIN
155
+ when JoinDependency, JoinDependency::JoinAssociation
156
+ Constants::STASHED_JOIN
157
+ when Arel::Nodes::Join
158
+ Constants::JOIN_NODE
159
+ else
160
+ raise 'unknown class: %s' % join.class.name
161
+ end
162
+ end
163
+
164
+ association_joins = buckets[Constants::ASSOCIATION_JOIN] || []
165
+
166
+ stashed_association_joins = buckets[Constants::STASHED_JOIN] || []
167
+
168
+ join_nodes = buckets[Constants::JOIN_NODE] || []
169
+
170
+ string_joins = (buckets[Constants::STRING_JOIN] || [])
171
+ .map { |x| x.strip }
172
+ .uniq
173
+
174
+ join_list = relation.send :custom_join_ast,
175
+ relation.table.from(relation.table), string_joins
176
+
177
+ join_dependency = JoinDependency.new(
178
+ relation.klass, association_joins, join_list
179
+ )
180
+
181
+ join_nodes.each do |join|
182
+ join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
183
+ end
184
+
185
+ join_dependency # ActiveRecord::Associations::JoinDependency
186
+ end
187
+
188
+ # ActiveRecord method
189
+ def build_or_find_association(name, parent = @base, klass = nil)
190
+ found_association = @join_dependency.join_associations
191
+ .detect do |assoc|
192
+ assoc.reflection.name == name &&
193
+ assoc.parent == parent &&
194
+ (!klass || assoc.reflection.klass == klass)
195
+ end
196
+ unless found_association
197
+ @join_dependency.send(
198
+ :build,
199
+ Polyamorous::Join.new(name, @join_type, klass),
200
+ parent
201
+ )
202
+ found_association = @join_dependency.join_associations.last
203
+ # Leverage the stashed association functionality in AR
204
+ @object = @object.joins(found_association)
205
+ end
206
+ found_association
207
+ end
208
+
209
+ end
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,23 @@
1
+ module Ransack
2
+ module Adapters
3
+ module Mongoid
4
+ class InquiryHash < Hash
5
+
6
+ def or(other)
7
+ { '$or' => [ self, other] }.to_inquiry
8
+ end
9
+
10
+ def and(other)
11
+ { '$and' => [ self, other] }.to_inquiry
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ class Hash
20
+ def to_inquiry
21
+ ::Ransack::Adapters::Mongoid::InquiryHash[self]
22
+ end
23
+ end
@@ -0,0 +1,94 @@
1
+ module Ransack
2
+ module Constants
3
+ # Ransack 4 registers its own SQL LIKE versions of these when it loads; the
4
+ # Ransack.configure block below replaces them with the Mongoid regex versions.
5
+ MONGOID_DERIVED_PREDICATES = [
6
+ [CONT, {
7
+ :arel_predicate => 'matches',
8
+ :formatter => proc { |v| "#{escape_regex(v)}" }
9
+ }
10
+ ],
11
+ ['not_cont', {
12
+ :arel_predicate => 'does_not_match',
13
+ :formatter => proc { |v| "#{escape_regex(v)}" }
14
+ }
15
+ ],
16
+ ['start', {
17
+ :arel_predicate => 'matches',
18
+ :formatter => proc { |v| "\\A#{escape_regex(v)}" }
19
+ }
20
+ ],
21
+ ['not_start', {
22
+ :arel_predicate => 'does_not_match',
23
+ :formatter => proc { |v| "\\A#{escape_regex(v)}" }
24
+ }
25
+ ],
26
+ ['end', {
27
+ :arel_predicate => 'matches',
28
+ :formatter => proc { |v| "#{escape_regex(v)}\\Z" }
29
+ }
30
+ ],
31
+ ['not_end', {
32
+ :arel_predicate => 'does_not_match',
33
+ :formatter => proc { |v| "#{escape_regex(v)}\\Z" }
34
+ }
35
+ ],
36
+ ['true', {
37
+ :arel_predicate => 'eq',
38
+ :compounds => false,
39
+ :type => :boolean,
40
+ :validator => proc { |v| TRUE_VALUES.include?(v) }
41
+ }
42
+ ],
43
+ ['false', {
44
+ :arel_predicate => 'eq',
45
+ :compounds => false,
46
+ :type => :boolean,
47
+ :validator => proc { |v| TRUE_VALUES.include?(v) },
48
+ :formatter => proc { |v| !v }
49
+ }
50
+ ],
51
+ ['present', {
52
+ :arel_predicate => proc { |v| v ? 'not_eq_all' : 'eq_any' },
53
+ :compounds => false,
54
+ :type => :boolean,
55
+ :validator => proc { |v| BOOLEAN_VALUES.include?(v) },
56
+ :formatter => proc { |v| [nil, ''] }
57
+ }
58
+ ],
59
+ ['blank', {
60
+ :arel_predicate => proc { |v| v ? 'eq_any' : 'not_eq_all' },
61
+ :compounds => false,
62
+ :type => :boolean,
63
+ :validator => proc { |v| BOOLEAN_VALUES.include?(v) },
64
+ :formatter => proc { |v| [nil, ''] }
65
+ }
66
+ ],
67
+ ['null', {
68
+ :arel_predicate => proc { |v| v ? 'eq' : 'not_eq' },
69
+ :compounds => false,
70
+ :type => :boolean,
71
+ :validator => proc { |v| BOOLEAN_VALUES.include?(v)},
72
+ :formatter => proc { |v| nil }
73
+ }
74
+ ],
75
+ ['not_null', {
76
+ :arel_predicate => proc { |v| v ? 'not_eq' : 'eq' },
77
+ :compounds => false,
78
+ :type => :boolean,
79
+ :validator => proc { |v| BOOLEAN_VALUES.include?(v) },
80
+ :formatter => proc { |v| nil } }
81
+ ]
82
+ ]
83
+
84
+ module_function
85
+ # does nothing
86
+ def escape_regex(unescaped)
87
+ Regexp.escape(unescaped)
88
+ end
89
+ end
90
+ end
91
+
92
+ Ransack.configure do |config|
93
+ Ransack::Constants::MONGOID_DERIVED_PREDICATES.each { |args| config.add_predicate(*args) }
94
+ end
@@ -0,0 +1,59 @@
1
+ require 'ransack/visitor'
2
+
3
+ module Ransack
4
+ class Context
5
+ # attr_reader :arel_visitor
6
+
7
+ class << self
8
+
9
+ def for_class(klass, options = {})
10
+ if klass.ancestors.include?(::Mongoid::Document)
11
+ Adapters::Mongoid::Context.new(klass, options)
12
+ end
13
+ end
14
+
15
+ def for_object(object, options = {})
16
+ case object
17
+ when ActiveRecord::Relation
18
+ Adapters::ActiveRecord::Context.new(object.klass, options)
19
+ end
20
+ end
21
+
22
+ end # << self
23
+
24
+ def initialize(object, options = {})
25
+ @object = relation_for(object)
26
+ @klass = @object.klass
27
+ # @join_dependency = join_dependency(@object)
28
+ # @join_type = options[:join_type] || Arel::OuterJoin
29
+ @search_key = options[:search_key] || Ransack.options[:search_key]
30
+
31
+ @base = @object.klass
32
+ # @engine = @base.arel_engine
33
+ end
34
+
35
+ def bind_pair_for(key)
36
+ @bind_pairs ||= {}
37
+
38
+ @bind_pairs[key] ||= begin
39
+ parent, attr_name = get_parent_and_attribute_name(key.to_s)
40
+ [parent, attr_name] if parent && attr_name
41
+ end
42
+ end
43
+
44
+ def klassify(obj)
45
+ if Class === obj && ::ActiveRecord::Base > obj
46
+ obj
47
+ elsif obj.respond_to? :klass
48
+ obj.klass
49
+ elsif obj.respond_to? :active_record # Rails 3
50
+ obj.active_record
51
+ elsif obj.respond_to? :base_klass # Rails 4
52
+ obj.base_klass
53
+ else
54
+ raise ArgumentError, "Don't know how to klassify #{obj.inspect}"
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,34 @@
1
+ module Ransack
2
+ module Nodes
3
+ class Condition
4
+
5
+ def arel_predicate
6
+ predicates = attributes.map do |attr|
7
+ attr.attr.send(
8
+ arel_predicate_for_attribute(attr),
9
+ formatted_values_for_attribute(attr)
10
+ )
11
+ end
12
+
13
+ # The predicates are InquiryHashes, which combine with $and / $or. (Arel isn't loaded
14
+ # without ActiveRecord.)
15
+ if predicates.size > 1 && combinator == 'and'
16
+ predicates.inject(&:and)
17
+ else
18
+ predicates.inject(&:or)
19
+ end
20
+ end
21
+
22
+ # Ransack 4.1+ wraps values containing % in Arel::Nodes::Quoted, which is a SQL LIKE concern
23
+ # and needs ActiveRecord. The Mongoid predicates escape values for regex instead.
24
+ def formatted_values_for_attribute(attr)
25
+ formatted = casted_values_for_attribute(attr).map do |val|
26
+ val = attr.ransacker.formatter.call(val) if attr.ransacker && attr.ransacker.formatter
27
+ predicate.format(val)
28
+ end
29
+ predicate.wants_array ? formatted : formatted.first
30
+ end
31
+
32
+ end # Condition
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ module Ransack
2
+ module Translate
3
+
4
+ def self.i18n_key(klass)
5
+ # if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
6
+ # klass.model_name.i18n_key.to_s.tr('.', '/')
7
+ # else
8
+ # klass.model_name.i18n_key.to_s
9
+ # end
10
+ klass.model_name.i18n_key.to_s
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module Ransack
2
+ class Visitor
3
+ def visit_and(object)
4
+ nodes = object.values.map { |o| accept(o) }.compact
5
+ nodes.inject(&:and)
6
+ end
7
+
8
+ def visit_or(object)
9
+ nodes = object.values.map { |o| accept(o) }.compact
10
+ nodes.inject(&:or)
11
+ end
12
+
13
+ def quoted?(object)
14
+ raise "not implemented"
15
+ end
16
+
17
+ def visit_Ransack_Nodes_Sort(object)
18
+ object.attr.send(object.dir) if object.valid?
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module Ransack
2
+ module Adapters
3
+ module Mongoid
4
+ class Table
5
+ attr_accessor :name
6
+
7
+ alias :table_name :name
8
+
9
+ def initialize(object, engine = nil)
10
+ @object = object
11
+ @name = object.collection.name
12
+ @engine = engine
13
+ @columns = nil
14
+ @aliases = []
15
+ @table_alias = nil
16
+ @primary_key = nil
17
+
18
+ if Hash === engine
19
+ # @engine = engine[:engine] || Table.engine
20
+
21
+ # Sometime AR sends an :as parameter to table, to let the table know
22
+ # that it is an Alias. We may want to override new, and return a
23
+ # TableAlias node?
24
+ # @table_alias = engine[:as] unless engine[:as].to_s == @name
25
+ end
26
+ end
27
+
28
+ def [](name)
29
+ Ransack::Adapters::Mongoid::Attribute.new self, name
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ require 'ransack/adapters/mongoid/base'
2
+ ::Mongoid::Document.send :include, Ransack::Adapters::Mongoid::Base
3
+
4
+ require 'ransack/adapters/mongoid/attributes/attribute'
5
+ require 'ransack/adapters/mongoid/table'
6
+ require 'ransack/adapters/mongoid/inquiry_hash'
7
+
8
+ case ::Mongoid::VERSION
9
+ when /^3\.2\./
10
+ require 'ransack/adapters/mongoid/3.2/context'
11
+ else
12
+ require 'ransack/adapters/mongoid/context'
13
+ end
14
+
15
+ Ransack::SUPPORTS_ATTRIBUTE_ALIAS = false
@@ -0,0 +1,5 @@
1
+ module Ransack
2
+ module Mongoid
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ require "mongoid"
2
+ require "ransack"
3
+ require "ransack/mongoid/version"
4
+
5
+ module Ransack
6
+ module Mongoid
7
+ end
8
+ end
9
+
10
+ # Ransack 4 only wires itself into ActiveRecord and no longer loads adapters for other
11
+ # ORMs, so load the Mongoid adapter here, in the order ransack 2's MongoidAdapter did.
12
+ require "ransack/adapters/mongoid/ransack/constants"
13
+ require "ransack/adapters/mongoid/ransack/translate"
14
+ require "ransack/adapters/mongoid"
15
+ require "ransack/adapters/mongoid/ransack/visitor"
16
+ require "ransack/adapters/mongoid/ransack/nodes/condition"
17
+ require "ransack/adapters/mongoid/ransack/context"
@@ -0,0 +1 @@
1
+ require "ransack/mongoid"
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ransack-mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ernie Miller
8
+ - Ryan Bigg
9
+ - Jon Atack
10
+ - Sean Carroll
11
+ - JD Guzman
12
+ - Nic Boie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+ date: 2026-12-02 00:00:00.000000000 Z
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: ransack
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: '4.0'
25
+ - - "<"
26
+ - !ruby/object:Gem::Version
27
+ version: '5'
28
+ type: :runtime
29
+ prerelease: false
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '4.0'
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: '5'
38
+ - !ruby/object:Gem::Dependency
39
+ name: mongoid
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '7.0'
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: '9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '7.0'
55
+ - - "<"
56
+ - !ruby/object:Gem::Version
57
+ version: '9'
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '13.0'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '13.0'
72
+ - !ruby/object:Gem::Dependency
73
+ name: rspec
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - "~>"
77
+ - !ruby/object:Gem::Version
78
+ version: '3'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - "~>"
84
+ - !ruby/object:Gem::Version
85
+ version: '3'
86
+ - !ruby/object:Gem::Dependency
87
+ name: machinist
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: 1.0.6
93
+ type: :development
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 1.0.6
100
+ - !ruby/object:Gem::Dependency
101
+ name: faker
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - "~>"
105
+ - !ruby/object:Gem::Version
106
+ version: 3.2.0
107
+ type: :development
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: 3.2.0
114
+ description: Ransack is the successor to the MetaSearch gem. It improves and expands
115
+ upon MetaSearch's functionality, but does not have a 100%-compatible API. This is
116
+ the gem with Mongoid support
117
+ email:
118
+ - ernie@erniemiller.org
119
+ - radarlistener@gmail.com
120
+ - jonnyatack@gmail.com
121
+ - sfcarroll@gmail.com
122
+ - jdguzman@mac.com
123
+ - boie0025@gmail.com
124
+ executables: []
125
+ extensions: []
126
+ extra_rdoc_files: []
127
+ files:
128
+ - CHANGELOG.md
129
+ - LICENSE.txt
130
+ - README.md
131
+ - lib/ransack-mongoid.rb
132
+ - lib/ransack/adapters/mongoid.rb
133
+ - lib/ransack/adapters/mongoid/attributes/attribute.rb
134
+ - lib/ransack/adapters/mongoid/attributes/order_predications.rb
135
+ - lib/ransack/adapters/mongoid/attributes/predications.rb
136
+ - lib/ransack/adapters/mongoid/base.rb
137
+ - lib/ransack/adapters/mongoid/context.rb
138
+ - lib/ransack/adapters/mongoid/inquiry_hash.rb
139
+ - lib/ransack/adapters/mongoid/ransack/constants.rb
140
+ - lib/ransack/adapters/mongoid/ransack/context.rb
141
+ - lib/ransack/adapters/mongoid/ransack/nodes/condition.rb
142
+ - lib/ransack/adapters/mongoid/ransack/translate.rb
143
+ - lib/ransack/adapters/mongoid/ransack/visitor.rb
144
+ - lib/ransack/adapters/mongoid/table.rb
145
+ - lib/ransack/mongoid.rb
146
+ - lib/ransack/mongoid/version.rb
147
+ homepage: https://github.com/riverock/ransack-mongoid
148
+ licenses:
149
+ - MIT
150
+ metadata:
151
+ source_code_uri: https://github.com/riverock/ransack-mongoid
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '3.1'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubygems_version: 3.3.26
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Object-based searching for Mongoid.
171
+ test_files: []