ransack 1.8.4 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +5 -5
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/SECURITY.md +12 -0
  4. data/.github/workflows/cronjob.yml +102 -0
  5. data/.github/workflows/deploy.yml +35 -0
  6. data/.github/workflows/rubocop.yml +20 -0
  7. data/.github/workflows/test-deploy.yml +29 -0
  8. data/.github/workflows/test.yml +130 -0
  9. data/.gitignore +3 -0
  10. data/{lib/ransack/adapters/mongoid/3.2/.gitkeep → .nojekyll} +0 -0
  11. data/.rubocop.yml +44 -0
  12. data/CHANGELOG.md +352 -0
  13. data/CONTRIBUTING.md +25 -13
  14. data/Gemfile +26 -27
  15. data/README.md +65 -815
  16. data/Rakefile +1 -22
  17. data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +78 -0
  18. data/bug_report_templates/test-ransacker-arel-present-predicate.rb +71 -0
  19. data/docs/.gitignore +19 -0
  20. data/docs/.nojekyll +0 -0
  21. data/docs/babel.config.js +3 -0
  22. data/docs/blog/2022-03-27-ransack-3.0.0.md +20 -0
  23. data/docs/docs/getting-started/_category_.json +4 -0
  24. data/docs/docs/getting-started/advanced-mode.md +46 -0
  25. data/docs/docs/getting-started/configuration.md +47 -0
  26. data/docs/docs/getting-started/search-matches.md +67 -0
  27. data/docs/docs/getting-started/simple-mode.md +284 -0
  28. data/docs/docs/getting-started/sorting.md +79 -0
  29. data/docs/docs/getting-started/using-predicates.md +282 -0
  30. data/docs/docs/going-further/_category_.json +4 -0
  31. data/docs/docs/going-further/acts-as-taggable-on.md +114 -0
  32. data/docs/docs/going-further/associations.md +70 -0
  33. data/docs/docs/going-further/custom-predicates.md +52 -0
  34. data/docs/docs/going-further/documentation.md +43 -0
  35. data/docs/docs/going-further/exporting-to-csv.md +49 -0
  36. data/docs/docs/going-further/external-guides.md +57 -0
  37. data/docs/docs/going-further/form-customisation.md +63 -0
  38. data/docs/docs/going-further/i18n.md +53 -0
  39. data/docs/docs/going-further/img/create_release.png +0 -0
  40. data/docs/docs/going-further/merging-searches.md +41 -0
  41. data/docs/docs/going-further/other-notes.md +428 -0
  42. data/docs/docs/going-further/polymorphic-search.md +40 -0
  43. data/docs/docs/going-further/ransackers.md +331 -0
  44. data/docs/docs/going-further/release_process.md +36 -0
  45. data/docs/docs/going-further/saving-queries.md +82 -0
  46. data/docs/docs/going-further/searching-postgres.md +57 -0
  47. data/docs/docs/going-further/wiki-contributors.md +82 -0
  48. data/docs/docs/intro.md +99 -0
  49. data/docs/docusaurus.config.js +120 -0
  50. data/docs/package.json +38 -0
  51. data/docs/sidebars.js +31 -0
  52. data/docs/src/components/HomepageFeatures/index.js +64 -0
  53. data/docs/src/components/HomepageFeatures/styles.module.css +11 -0
  54. data/docs/src/css/custom.css +39 -0
  55. data/docs/src/pages/index.module.css +23 -0
  56. data/docs/src/pages/markdown-page.md +7 -0
  57. data/docs/static/.nojekyll +0 -0
  58. data/docs/static/img/docusaurus.png +0 -0
  59. data/docs/static/img/favicon.ico +0 -0
  60. data/docs/static/img/logo.svg +1 -0
  61. data/docs/static/img/tutorial/docsVersionDropdown.png +0 -0
  62. data/docs/static/img/tutorial/localeDropdown.png +0 -0
  63. data/docs/static/img/undraw_docusaurus_mountain.svg +171 -0
  64. data/docs/static/img/undraw_docusaurus_react.svg +170 -0
  65. data/docs/static/img/undraw_docusaurus_tree.svg +40 -0
  66. data/docs/static/logo/ransack-h.png +0 -0
  67. data/docs/static/logo/ransack-h.svg +34 -0
  68. data/docs/static/logo/ransack-v.png +0 -0
  69. data/docs/static/logo/ransack-v.svg +34 -0
  70. data/docs/static/logo/ransack.png +0 -0
  71. data/docs/static/logo/ransack.svg +21 -0
  72. data/docs/yarn.lock +8436 -0
  73. data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +70 -0
  74. data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +92 -0
  75. data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +11 -0
  76. data/lib/polyamorous/activerecord_7.0_ruby_2/join_association.rb +1 -0
  77. data/lib/polyamorous/activerecord_7.0_ruby_2/join_dependency.rb +1 -0
  78. data/lib/polyamorous/activerecord_7.0_ruby_2/reflection.rb +1 -0
  79. data/lib/polyamorous/activerecord_7.1_ruby_2/join_association.rb +1 -0
  80. data/lib/polyamorous/activerecord_7.1_ruby_2/join_dependency.rb +1 -0
  81. data/lib/polyamorous/activerecord_7.1_ruby_2/reflection.rb +1 -0
  82. data/lib/polyamorous/join.rb +70 -0
  83. data/lib/polyamorous/polyamorous.rb +24 -0
  84. data/lib/polyamorous/swapping_reflection_class.rb +11 -0
  85. data/lib/polyamorous/tree_node.rb +7 -0
  86. data/lib/polyamorous.rb +1 -0
  87. data/lib/ransack/adapters/active_record/base.rb +14 -3
  88. data/lib/ransack/adapters/active_record/context.rb +140 -196
  89. data/lib/ransack/adapters/active_record/ransack/constants.rb +19 -4
  90. data/lib/ransack/adapters/active_record/ransack/context.rb +9 -19
  91. data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +7 -7
  92. data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -5
  93. data/lib/ransack/adapters/active_record/ransack/visitor.rb +23 -0
  94. data/lib/ransack/adapters/active_record.rb +0 -9
  95. data/lib/ransack/adapters.rb +2 -0
  96. data/lib/ransack/configuration.rb +52 -2
  97. data/lib/ransack/constants.rb +1 -5
  98. data/lib/ransack/context.rb +29 -24
  99. data/lib/ransack/helpers/form_builder.rb +12 -6
  100. data/lib/ransack/helpers/form_helper.rb +11 -3
  101. data/lib/ransack/helpers.rb +1 -1
  102. data/lib/ransack/locale/ar.yml +70 -0
  103. data/lib/ransack/locale/az.yml +70 -0
  104. data/lib/ransack/locale/bg.yml +70 -0
  105. data/lib/ransack/locale/ca.yml +70 -0
  106. data/lib/ransack/locale/el.yml +70 -0
  107. data/lib/ransack/locale/es.yml +22 -22
  108. data/lib/ransack/locale/fa.yml +70 -0
  109. data/lib/ransack/locale/fi.yml +71 -0
  110. data/lib/ransack/locale/nl.yml +4 -4
  111. data/lib/ransack/locale/ru.yml +70 -0
  112. data/lib/ransack/locale/sk.yml +70 -0
  113. data/lib/ransack/locale/sv.yml +70 -0
  114. data/lib/ransack/locale/tr.yml +70 -0
  115. data/lib/ransack/locale/zh-CN.yml +12 -12
  116. data/lib/ransack/nodes/attribute.rb +2 -2
  117. data/lib/ransack/nodes/condition.rb +7 -1
  118. data/lib/ransack/nodes/grouping.rb +3 -8
  119. data/lib/ransack/nodes/sort.rb +3 -3
  120. data/lib/ransack/nodes/value.rb +3 -3
  121. data/lib/ransack/predicate.rb +13 -20
  122. data/lib/ransack/search.rb +7 -4
  123. data/lib/ransack/translate.rb +115 -115
  124. data/lib/ransack/version.rb +1 -1
  125. data/lib/ransack/visitor.rb +1 -12
  126. data/lib/ransack.rb +7 -5
  127. data/ransack.gemspec +9 -25
  128. data/spec/blueprints/articles.rb +1 -1
  129. data/spec/blueprints/comments.rb +1 -1
  130. data/spec/blueprints/notes.rb +1 -1
  131. data/spec/blueprints/tags.rb +1 -1
  132. data/spec/console.rb +5 -5
  133. data/spec/helpers/polyamorous_helper.rb +13 -0
  134. data/spec/helpers/ransack_helper.rb +1 -1
  135. data/spec/polyamorous/activerecord_compatibility_spec.rb +15 -0
  136. data/spec/polyamorous/join_association_spec.rb +30 -0
  137. data/spec/polyamorous/join_dependency_spec.rb +81 -0
  138. data/spec/polyamorous/join_spec.rb +19 -0
  139. data/spec/ransack/adapters/active_record/base_spec.rb +105 -11
  140. data/spec/ransack/adapters/active_record/context_spec.rb +63 -24
  141. data/spec/ransack/configuration_spec.rb +24 -0
  142. data/spec/ransack/helpers/form_builder_spec.rb +3 -15
  143. data/spec/ransack/helpers/form_helper_spec.rb +135 -168
  144. data/spec/ransack/nodes/condition_spec.rb +13 -0
  145. data/spec/ransack/nodes/grouping_spec.rb +2 -2
  146. data/spec/ransack/nodes/value_spec.rb +115 -0
  147. data/spec/ransack/predicate_spec.rb +54 -2
  148. data/spec/ransack/search_spec.rb +266 -36
  149. data/spec/spec_helper.rb +14 -5
  150. data/spec/support/schema.rb +99 -21
  151. metadata +117 -187
  152. data/.travis.yml +0 -86
  153. data/lib/ransack/adapters/active_record/3.0/compat.rb +0 -179
  154. data/lib/ransack/adapters/active_record/3.0/context.rb +0 -203
  155. data/lib/ransack/adapters/active_record/3.1/context.rb +0 -212
  156. data/lib/ransack/adapters/active_record/3.2/context.rb +0 -44
  157. data/lib/ransack/adapters/active_record/compat.rb +0 -14
  158. data/lib/ransack/adapters/mongoid/attributes/attribute.rb +0 -37
  159. data/lib/ransack/adapters/mongoid/attributes/order_predications.rb +0 -17
  160. data/lib/ransack/adapters/mongoid/attributes/predications.rb +0 -141
  161. data/lib/ransack/adapters/mongoid/base.rb +0 -134
  162. data/lib/ransack/adapters/mongoid/context.rb +0 -212
  163. data/lib/ransack/adapters/mongoid/inquiry_hash.rb +0 -23
  164. data/lib/ransack/adapters/mongoid/ransack/constants.rb +0 -88
  165. data/lib/ransack/adapters/mongoid/ransack/context.rb +0 -60
  166. data/lib/ransack/adapters/mongoid/ransack/nodes/condition.rb +0 -27
  167. data/lib/ransack/adapters/mongoid/ransack/translate.rb +0 -13
  168. data/lib/ransack/adapters/mongoid/ransack/visitor.rb +0 -24
  169. data/lib/ransack/adapters/mongoid/table.rb +0 -35
  170. data/lib/ransack/adapters/mongoid.rb +0 -15
  171. data/spec/mongoid/adapters/mongoid/base_spec.rb +0 -314
  172. data/spec/mongoid/adapters/mongoid/context_spec.rb +0 -56
  173. data/spec/mongoid/configuration_spec.rb +0 -162
  174. data/spec/mongoid/dependencies_spec.rb +0 -8
  175. data/spec/mongoid/helpers/ransack_helper.rb +0 -11
  176. data/spec/mongoid/nodes/condition_spec.rb +0 -49
  177. data/spec/mongoid/nodes/grouping_spec.rb +0 -13
  178. data/spec/mongoid/predicate_spec.rb +0 -155
  179. data/spec/mongoid/search_spec.rb +0 -445
  180. data/spec/mongoid/support/mongoid.yml +0 -11
  181. data/spec/mongoid/support/schema.rb +0 -135
  182. data/spec/mongoid/translate_spec.rb +0 -14
  183. data/spec/mongoid_spec_helper.rb +0 -63
  184. data/spec/ransack/dependencies_spec.rb +0 -12
@@ -1,44 +0,0 @@
1
- require 'ransack/context'
2
- require 'ransack/adapters/active_record/3.1/context'
3
- require 'ransack/adapters/active_record/compat'
4
- require 'polyamorous'
5
-
6
- module Ransack
7
- module Adapters
8
- module ActiveRecord
9
- class Context < ::Ransack::Context
10
-
11
- # Redefine a few things for ActiveRecord 3.2.
12
-
13
- def initialize(object, options = {})
14
- super
15
- @arel_visitor = @engine.connection.visitor
16
- end
17
-
18
- def relation_for(object)
19
- object.scoped
20
- end
21
-
22
- def type_for(attr)
23
- return nil unless attr && attr.valid?
24
- name = attr.arel_attribute.name.to_s
25
- table = attr.arel_attribute.relation.table_name
26
-
27
- schema_cache = @engine.connection.schema_cache
28
- raise "No table named #{table} exists" unless schema_cache.table_exists?(table)
29
- schema_cache.columns_hash[table][name].type
30
- end
31
-
32
- def evaluate(search, opts = {})
33
- viz = Visitor.new
34
- relation = @object.where(viz.accept(search.base))
35
- if search.sorts.any?
36
- relation = relation.except(:order).reorder(viz.accept(search.sorts))
37
- end
38
- opts[:distinct] ? relation.uniq : relation
39
- end
40
-
41
- end
42
- end
43
- end
44
- end
@@ -1,14 +0,0 @@
1
- module Arel
2
-
3
- module Visitors
4
-
5
- class DepthFirst < Visitor
6
-
7
- unless method_defined?(:visit_Arel_Nodes_InfixOperation)
8
- alias :visit_Arel_Nodes_InfixOperation :binary
9
- end
10
-
11
- end
12
-
13
- end
14
- end
@@ -1,37 +0,0 @@
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
@@ -1,17 +0,0 @@
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
@@ -1,141 +0,0 @@
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
@@ -1,134 +0,0 @@
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
- # imitating active record
89
-
90
- def joins_values *args
91
- []
92
- end
93
-
94
- def custom_join_ast *args
95
- []
96
- end
97
-
98
- def first(*args)
99
- if args.size == 0
100
- super
101
- else
102
- self.criteria.limit(args.first)
103
- end
104
- end
105
-
106
- # def group_by *args, &block
107
- # criteria
108
- # end
109
-
110
- def columns
111
- @columns ||= fields.map(&:second).map{ |c| ColumnWrapper.new(c) }
112
- end
113
-
114
- def column_names
115
- @column_names ||= fields.map(&:first)
116
- end
117
-
118
- def columns_hash
119
- columns.index_by(&:name)
120
- end
121
-
122
- def table
123
- name = ::Ransack::Adapters::Mongoid::Attributes::Attribute.new(
124
- self.criteria, :name
125
- )
126
- { :name => name }
127
- end
128
-
129
- end
130
-
131
- end # Base
132
- end
133
- end
134
- end
@@ -1,212 +0,0 @@
1
- require 'ransack/context'
2
- require '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_pairs[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(/_/)).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(/_/)).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
@@ -1,23 +0,0 @@
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