ransack 2.3.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/SECURITY.md +12 -0
  4. data/.github/workflows/codeql.yml +72 -0
  5. data/.github/workflows/cronjob.yml +99 -0
  6. data/.github/workflows/deploy.yml +35 -0
  7. data/.github/workflows/rubocop.yml +20 -0
  8. data/.github/workflows/test-deploy.yml +29 -0
  9. data/.github/workflows/test.yml +131 -0
  10. data/.nojekyll +0 -0
  11. data/.rubocop.yml +50 -0
  12. data/CHANGELOG.md +263 -1
  13. data/CONTRIBUTING.md +51 -29
  14. data/Gemfile +24 -10
  15. data/README.md +49 -917
  16. data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +78 -0
  17. data/bug_report_templates/test-ransacker-arel-present-predicate.rb +75 -0
  18. data/docs/.gitignore +19 -0
  19. data/docs/.nojekyll +0 -0
  20. data/docs/babel.config.js +3 -0
  21. data/docs/blog/2022-03-27-ransack-3.0.0.md +20 -0
  22. data/docs/docs/getting-started/_category_.json +4 -0
  23. data/docs/docs/getting-started/advanced-mode.md +46 -0
  24. data/docs/docs/getting-started/configuration.md +47 -0
  25. data/docs/docs/getting-started/search-matches.md +67 -0
  26. data/docs/docs/getting-started/simple-mode.md +288 -0
  27. data/docs/docs/getting-started/sorting.md +71 -0
  28. data/docs/docs/getting-started/using-predicates.md +282 -0
  29. data/docs/docs/going-further/_category_.json +4 -0
  30. data/docs/docs/going-further/acts-as-taggable-on.md +114 -0
  31. data/docs/docs/going-further/associations.md +70 -0
  32. data/docs/docs/going-further/custom-predicates.md +52 -0
  33. data/docs/docs/going-further/documentation.md +43 -0
  34. data/docs/docs/going-further/exporting-to-csv.md +49 -0
  35. data/docs/docs/going-further/external-guides.md +57 -0
  36. data/docs/docs/going-further/form-customisation.md +63 -0
  37. data/docs/docs/going-further/i18n.md +53 -0
  38. data/docs/docs/going-further/img/create_release.png +0 -0
  39. data/docs/docs/going-further/merging-searches.md +41 -0
  40. data/docs/docs/going-further/other-notes.md +428 -0
  41. data/docs/docs/going-further/polymorphic-search.md +46 -0
  42. data/docs/docs/going-further/ransackers.md +331 -0
  43. data/docs/docs/going-further/release_process.md +36 -0
  44. data/docs/docs/going-further/saving-queries.md +82 -0
  45. data/docs/docs/going-further/searching-postgres.md +57 -0
  46. data/docs/docs/going-further/wiki-contributors.md +82 -0
  47. data/docs/docs/intro.md +99 -0
  48. data/docs/docusaurus.config.js +120 -0
  49. data/docs/package.json +42 -0
  50. data/docs/sidebars.js +31 -0
  51. data/docs/src/components/HomepageFeatures/index.js +64 -0
  52. data/docs/src/components/HomepageFeatures/styles.module.css +11 -0
  53. data/docs/src/css/custom.css +39 -0
  54. data/docs/src/pages/index.module.css +23 -0
  55. data/docs/src/pages/markdown-page.md +7 -0
  56. data/docs/static/.nojekyll +0 -0
  57. data/docs/static/img/docusaurus.png +0 -0
  58. data/docs/static/img/favicon.ico +0 -0
  59. data/docs/static/img/logo.svg +1 -0
  60. data/docs/static/img/tutorial/docsVersionDropdown.png +0 -0
  61. data/docs/static/img/tutorial/localeDropdown.png +0 -0
  62. data/docs/static/img/undraw_docusaurus_mountain.svg +171 -0
  63. data/docs/static/img/undraw_docusaurus_react.svg +170 -0
  64. data/docs/static/img/undraw_docusaurus_tree.svg +40 -0
  65. data/docs/yarn.lock +8879 -0
  66. data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +70 -0
  67. data/{polyamorous/lib/polyamorous/activerecord_6.0_ruby_2 → lib/polyamorous/activerecord_6.1_ruby_2}/join_dependency.rb +33 -12
  68. data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +11 -0
  69. data/lib/polyamorous/activerecord_7.0_ruby_2/join_association.rb +1 -0
  70. data/lib/polyamorous/activerecord_7.0_ruby_2/join_dependency.rb +1 -0
  71. data/lib/polyamorous/activerecord_7.0_ruby_2/reflection.rb +1 -0
  72. data/lib/polyamorous/activerecord_7.1_ruby_2/join_association.rb +1 -0
  73. data/lib/polyamorous/activerecord_7.1_ruby_2/join_dependency.rb +1 -0
  74. data/lib/polyamorous/activerecord_7.1_ruby_2/reflection.rb +1 -0
  75. data/{polyamorous/lib → lib/polyamorous}/polyamorous.rb +3 -8
  76. data/lib/ransack/adapters/active_record/base.rb +83 -10
  77. data/lib/ransack/adapters/active_record/context.rb +61 -116
  78. data/lib/ransack/configuration.rb +53 -10
  79. data/lib/ransack/constants.rb +126 -7
  80. data/lib/ransack/context.rb +34 -5
  81. data/lib/ransack/helpers/form_builder.rb +11 -17
  82. data/lib/ransack/helpers/form_helper.rb +14 -5
  83. data/lib/ransack/helpers.rb +1 -1
  84. data/lib/ransack/locale/sk.yml +70 -0
  85. data/lib/ransack/locale/sv.yml +70 -0
  86. data/lib/ransack/nodes/attribute.rb +3 -3
  87. data/lib/ransack/nodes/condition.rb +87 -8
  88. data/lib/ransack/nodes/grouping.rb +4 -4
  89. data/lib/ransack/nodes/node.rb +1 -1
  90. data/lib/ransack/nodes/sort.rb +3 -3
  91. data/lib/ransack/nodes/value.rb +3 -3
  92. data/lib/ransack/predicate.rb +3 -2
  93. data/lib/ransack/ransacker.rb +1 -1
  94. data/lib/ransack/search.rb +15 -7
  95. data/lib/ransack/translate.rb +6 -6
  96. data/lib/ransack/version.rb +1 -1
  97. data/lib/ransack/visitor.rb +38 -2
  98. data/lib/ransack.rb +6 -10
  99. data/ransack.gemspec +9 -24
  100. data/spec/blueprints/articles.rb +1 -1
  101. data/spec/blueprints/comments.rb +1 -1
  102. data/spec/blueprints/notes.rb +1 -1
  103. data/spec/blueprints/tags.rb +1 -1
  104. data/spec/console.rb +5 -5
  105. data/spec/helpers/polyamorous_helper.rb +2 -17
  106. data/spec/helpers/ransack_helper.rb +1 -1
  107. data/spec/polyamorous/activerecord_compatibility_spec.rb +15 -0
  108. data/spec/{ransack → polyamorous}/join_association_spec.rb +3 -1
  109. data/spec/{ransack → polyamorous}/join_dependency_spec.rb +0 -16
  110. data/spec/ransack/adapters/active_record/base_spec.rb +125 -16
  111. data/spec/ransack/adapters/active_record/context_spec.rb +19 -18
  112. data/spec/ransack/configuration_spec.rb +33 -9
  113. data/spec/ransack/helpers/form_builder_spec.rb +8 -8
  114. data/spec/ransack/helpers/form_helper_spec.rb +109 -20
  115. data/spec/ransack/nodes/condition_spec.rb +37 -0
  116. data/spec/ransack/nodes/grouping_spec.rb +2 -2
  117. data/spec/ransack/nodes/value_spec.rb +115 -0
  118. data/spec/ransack/predicate_spec.rb +75 -2
  119. data/spec/ransack/search_spec.rb +239 -38
  120. data/spec/ransack/translate_spec.rb +1 -1
  121. data/spec/spec_helper.rb +9 -5
  122. data/spec/support/schema.rb +111 -12
  123. metadata +105 -195
  124. data/.travis.yml +0 -49
  125. data/lib/ransack/adapters/active_record/ransack/constants.rb +0 -116
  126. data/lib/ransack/adapters/active_record/ransack/context.rb +0 -60
  127. data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +0 -61
  128. data/lib/ransack/adapters/active_record/ransack/translate.rb +0 -8
  129. data/lib/ransack/adapters/active_record/ransack/visitor.rb +0 -47
  130. data/lib/ransack/adapters.rb +0 -64
  131. data/lib/ransack/nodes.rb +0 -8
  132. data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +0 -2
  133. data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +0 -2
  134. data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +0 -31
  135. data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +0 -112
  136. data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +0 -31
  137. data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +0 -112
  138. data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb +0 -12
  139. data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb +0 -22
  140. data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb +0 -81
  141. data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb +0 -2
  142. data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +0 -2
  143. data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +0 -2
  144. data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +0 -2
  145. data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +0 -2
  146. data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +0 -2
  147. data/polyamorous/lib/polyamorous/version.rb +0 -3
  148. data/polyamorous/polyamorous.gemspec +0 -35
  149. /data/{logo → docs/static/logo}/ransack-h.png +0 -0
  150. /data/{logo → docs/static/logo}/ransack-h.svg +0 -0
  151. /data/{logo → docs/static/logo}/ransack-v.png +0 -0
  152. /data/{logo → docs/static/logo}/ransack-v.svg +0 -0
  153. /data/{logo → docs/static/logo}/ransack.png +0 -0
  154. /data/{logo → docs/static/logo}/ransack.svg +0 -0
  155. /data/{polyamorous/lib → lib}/polyamorous/join.rb +0 -0
  156. /data/{polyamorous/lib → lib}/polyamorous/swapping_reflection_class.rb +0 -0
  157. /data/{polyamorous/lib → lib}/polyamorous/tree_node.rb +0 -0
  158. /data/lib/ransack/{adapters/active_record.rb → active_record.rb} +0 -0
  159. /data/spec/{ransack → polyamorous}/join_spec.rb +0 -0
@@ -1,116 +0,0 @@
1
- module Ransack
2
- module Constants
3
- DISTINCT = 'DISTINCT '.freeze
4
-
5
- DERIVED_PREDICATES = [
6
- [CONT, {
7
- arel_predicate: 'matches'.freeze,
8
- formatter: proc { |v| "%#{escape_wildcards(v)}%" }
9
- }
10
- ],
11
- ['not_cont'.freeze, {
12
- arel_predicate: 'does_not_match'.freeze,
13
- formatter: proc { |v| "%#{escape_wildcards(v)}%" }
14
- }
15
- ],
16
- ['start'.freeze, {
17
- arel_predicate: 'matches'.freeze,
18
- formatter: proc { |v| "#{escape_wildcards(v)}%" }
19
- }
20
- ],
21
- ['not_start'.freeze, {
22
- arel_predicate: 'does_not_match'.freeze,
23
- formatter: proc { |v| "#{escape_wildcards(v)}%" }
24
- }
25
- ],
26
- ['end'.freeze, {
27
- arel_predicate: 'matches'.freeze,
28
- formatter: proc { |v| "%#{escape_wildcards(v)}" }
29
- }
30
- ],
31
- ['not_end'.freeze, {
32
- arel_predicate: 'does_not_match'.freeze,
33
- formatter: proc { |v| "%#{escape_wildcards(v)}" }
34
- }
35
- ],
36
- ['true'.freeze, {
37
- arel_predicate: proc { |v| v ? EQ : NOT_EQ },
38
- compounds: false,
39
- type: :boolean,
40
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
41
- formatter: proc { |v| true }
42
- }
43
- ],
44
- ['not_true'.freeze, {
45
- arel_predicate: proc { |v| v ? NOT_EQ : EQ },
46
- compounds: false,
47
- type: :boolean,
48
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
49
- formatter: proc { |v| true }
50
- }
51
- ],
52
- ['false'.freeze, {
53
- arel_predicate: proc { |v| v ? EQ : NOT_EQ },
54
- compounds: false,
55
- type: :boolean,
56
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
57
- formatter: proc { |v| false }
58
- }
59
- ],
60
- ['not_false'.freeze, {
61
- arel_predicate: proc { |v| v ? NOT_EQ : EQ },
62
- compounds: false,
63
- type: :boolean,
64
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
65
- formatter: proc { |v| false }
66
- }
67
- ],
68
- ['present'.freeze, {
69
- arel_predicate: proc { |v| v ? NOT_EQ_ALL : EQ_ANY },
70
- compounds: false,
71
- type: :boolean,
72
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
73
- formatter: proc { |v| [nil, ''.freeze].freeze }
74
- }
75
- ],
76
- ['blank'.freeze, {
77
- arel_predicate: proc { |v| v ? EQ_ANY : NOT_EQ_ALL },
78
- compounds: false,
79
- type: :boolean,
80
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
81
- formatter: proc { |v| [nil, ''.freeze].freeze }
82
- }
83
- ],
84
- ['null'.freeze, {
85
- arel_predicate: proc { |v| v ? EQ : NOT_EQ },
86
- compounds: false,
87
- type: :boolean,
88
- validator: proc { |v| BOOLEAN_VALUES.include?(v)},
89
- formatter: proc { |v| nil }
90
- }
91
- ],
92
- ['not_null'.freeze, {
93
- arel_predicate: proc { |v| v ? NOT_EQ : EQ },
94
- compounds: false,
95
- type: :boolean,
96
- validator: proc { |v| BOOLEAN_VALUES.include?(v) },
97
- formatter: proc { |v| nil } }
98
- ]
99
- ].freeze
100
-
101
- module_function
102
- # replace % \ to \% \\
103
- def escape_wildcards(unescaped)
104
- case ActiveRecord::Base.connection.adapter_name
105
- when "Mysql2".freeze
106
- # Necessary for MySQL
107
- unescaped.to_s.gsub(/([\\%_])/, '\\\\\\1')
108
- when "PostgreSQL".freeze
109
- # Necessary for PostgreSQL
110
- unescaped.to_s.gsub(/([\\%_.])/, '\\\\\\1')
111
- else
112
- unescaped
113
- end
114
- end
115
- end
116
- end
@@ -1,60 +0,0 @@
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 < ActiveRecord::Base
11
- Adapters::ActiveRecord::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] || Polyamorous::OuterJoin
29
- @search_key = options[:search_key] || Ransack.options[:search_key]
30
- @associations_pot = {}
31
- @lock_associations = []
32
-
33
- if ::ActiveRecord::VERSION::STRING >= Constants::RAILS_5_2
34
- @base = @join_dependency.instance_variable_get(:@join_root)
35
- else
36
- @base = @join_dependency.join_root
37
- @engine = @base.base_klass.arel_engine
38
- end
39
- end
40
-
41
- def bind_pair_for(key)
42
- @bind_pairs ||= {}
43
-
44
- @bind_pairs[key] ||= begin
45
- parent, attr_name = get_parent_and_attribute_name(key.to_s)
46
- [parent, attr_name] if parent && attr_name
47
- end
48
- end
49
-
50
- def klassify(obj)
51
- if Class === obj && ::ActiveRecord::Base > obj
52
- obj
53
- elsif obj.respond_to? :klass
54
- obj.klass
55
- else
56
- raise ArgumentError, "Don't know how to klassify #{obj.inspect}"
57
- end
58
- end
59
- end
60
- end
@@ -1,61 +0,0 @@
1
- module Ransack
2
- module Nodes
3
- class Condition
4
-
5
- def arel_predicate
6
- attributes.map { |attribute|
7
- association = attribute.parent
8
- if negative? && attribute.associated_collection?
9
- query = context.build_correlated_subquery(association)
10
- context.remove_association(association)
11
- if self.predicate_name == 'not_null' && self.value
12
- query.where(format_predicate(attribute))
13
- Arel::Nodes::In.new(context.primary_key, Arel.sql(query.to_sql))
14
- else
15
- query.where(format_predicate(attribute).not)
16
- Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
17
- end
18
- else
19
- format_predicate(attribute)
20
- end
21
- }.reduce(combinator_method)
22
- end
23
-
24
- private
25
-
26
- def combinator_method
27
- combinator === Constants::OR ? :or : :and
28
- end
29
-
30
- def format_predicate(attribute)
31
- arel_pred = arel_predicate_for_attribute(attribute)
32
- arel_values = formatted_values_for_attribute(attribute)
33
- predicate = attribute.attr.public_send(arel_pred, arel_values)
34
-
35
- if in_predicate?(predicate)
36
- predicate.right = predicate.right.map do |pr|
37
- casted_array?(pr) ? format_values_for(pr) : pr
38
- end
39
- end
40
-
41
- predicate
42
- end
43
-
44
- def in_predicate?(predicate)
45
- return unless defined?(Arel::Nodes::Casted)
46
- predicate.class == Arel::Nodes::In || predicate.class == Arel::Nodes::NotIn
47
- end
48
-
49
- def casted_array?(predicate)
50
- predicate.respond_to?(:val) && predicate.val.is_a?(Array)
51
- end
52
-
53
- def format_values_for(predicate)
54
- predicate.val.map do |value|
55
- value.is_a?(String) ? Arel::Nodes.build_quoted(value) : value
56
- end
57
- end
58
-
59
- end
60
- end
61
- end
@@ -1,8 +0,0 @@
1
- module Ransack
2
- module Translate
3
-
4
- def self.i18n_key(klass)
5
- klass.model_name.i18n_key
6
- end
7
- end
8
- end
@@ -1,47 +0,0 @@
1
- module Ransack
2
- class Visitor
3
- def visit_and(object)
4
- nodes = object.values.map { |o| accept(o) }.compact
5
- return nil unless nodes.size > 0
6
-
7
- if nodes.size > 1
8
- Arel::Nodes::Grouping.new(Arel::Nodes::And.new(nodes))
9
- else
10
- nodes.first
11
- end
12
- end
13
-
14
- def quoted?(object)
15
- case object
16
- when Arel::Nodes::SqlLiteral, Bignum, Fixnum
17
- false
18
- else
19
- true
20
- end
21
- end
22
-
23
- def visit_Ransack_Nodes_Sort(object)
24
- if object.valid?
25
- if object.attr.is_a?(Arel::Attributes::Attribute)
26
- object.attr.send(object.dir)
27
- else
28
- ordered(object)
29
- end
30
- else
31
- scope_name = :"sort_by_#{object.name}_#{object.dir}"
32
- scope_name if object.context.object.respond_to?(scope_name)
33
- end
34
- end
35
-
36
- private
37
-
38
- def ordered(object)
39
- case object.dir
40
- when 'asc'.freeze
41
- Arel::Nodes::Ascending.new(object.attr)
42
- when 'desc'.freeze
43
- Arel::Nodes::Descending.new(object.attr)
44
- end
45
- end
46
- end
47
- end
@@ -1,64 +0,0 @@
1
- module Ransack
2
- module Adapters
3
-
4
- def self.object_mapper
5
- @object_mapper ||= instantiate_object_mapper
6
- end
7
-
8
- def self.instantiate_object_mapper
9
- if defined?(::ActiveRecord::Base)
10
- ActiveRecordAdapter.new
11
- elsif defined?(::Mongoid)
12
- MongoidAdapter.new
13
- else
14
- raise "Unsupported adapter"
15
- end
16
- end
17
-
18
- class ActiveRecordAdapter
19
- def require_constants
20
- require 'ransack/adapters/active_record/ransack/constants'
21
- end
22
-
23
- def require_adapter
24
- require 'ransack/adapters/active_record/ransack/translate'
25
- require 'ransack/adapters/active_record'
26
- end
27
-
28
- def require_context
29
- require 'ransack/adapters/active_record/ransack/visitor'
30
- end
31
-
32
- def require_nodes
33
- require 'ransack/adapters/active_record/ransack/nodes/condition'
34
- end
35
-
36
- def require_search
37
- require 'ransack/adapters/active_record/ransack/context'
38
- end
39
- end
40
-
41
- class MongoidAdapter
42
- def require_constants
43
- require 'ransack/adapters/mongoid/ransack/constants'
44
- end
45
-
46
- def require_adapter
47
- require 'ransack/adapters/mongoid/ransack/translate'
48
- require 'ransack/adapters/mongoid'
49
- end
50
-
51
- def require_context
52
- require 'ransack/adapters/mongoid/ransack/visitor'
53
- end
54
-
55
- def require_nodes
56
- require 'ransack/adapters/mongoid/ransack/nodes/condition'
57
- end
58
-
59
- def require_search
60
- require 'ransack/adapters/mongoid/ransack/context'
61
- end
62
- end
63
- end
64
- end
data/lib/ransack/nodes.rb DELETED
@@ -1,8 +0,0 @@
1
- require 'ransack/nodes/bindable'
2
- require 'ransack/nodes/node'
3
- require 'ransack/nodes/attribute'
4
- require 'ransack/nodes/value'
5
- require 'ransack/nodes/condition'
6
- Ransack::Adapters.object_mapper.require_nodes
7
- require 'ransack/nodes/sort'
8
- require 'ransack/nodes/grouping'
@@ -1,2 +0,0 @@
1
- # active_record_5.0_ruby_2/join_association.rb
2
- require 'polyamorous/activerecord_5.1_ruby_2/join_association'
@@ -1,2 +0,0 @@
1
- # active_record_5.0_ruby_2/join_dependency.rb
2
- require 'polyamorous/activerecord_5.1_ruby_2/join_dependency'
@@ -1,31 +0,0 @@
1
- # active_record_5.1_ruby_2/join_association.rb
2
-
3
- module Polyamorous
4
- module JoinAssociationExtensions
5
- include SwappingReflectionClass
6
- def self.prepended(base)
7
- base.class_eval { attr_reader :join_type }
8
- end
9
-
10
- def initialize(reflection, children, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
11
- @join_type = join_type
12
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
13
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
14
- super(reflection, children)
15
- self.reflection.options[:polymorphic] = true
16
- end
17
- else
18
- super(reflection, children)
19
- end
20
- end
21
-
22
- def build_constraint(klass, table, key, foreign_table, foreign_key)
23
- if reflection.polymorphic?
24
- super(klass, table, key, foreign_table, foreign_key)
25
- .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
26
- else
27
- super(klass, table, key, foreign_table, foreign_key)
28
- end
29
- end
30
- end
31
- end
@@ -1,112 +0,0 @@
1
- # active_record_5.1_ruby_2/join_dependency.rb
2
-
3
- module Polyamorous
4
- module JoinDependencyExtensions
5
- # Replaces ActiveRecord::Associations::JoinDependency#build
6
- #
7
- def build(associations, base_klass)
8
- associations.map do |name, right|
9
- if name.is_a? Join
10
- reflection = find_reflection base_klass, name.name
11
- reflection.check_validity!
12
- reflection.check_eager_loadable!
13
-
14
- klass = if reflection.polymorphic?
15
- name.klass || base_klass
16
- else
17
- reflection.klass
18
- end
19
- JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
20
- else
21
- reflection = find_reflection base_klass, name
22
- reflection.check_validity!
23
- reflection.check_eager_loadable!
24
-
25
- if reflection.polymorphic?
26
- raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
27
- end
28
- JoinAssociation.new reflection, build(right, reflection.klass)
29
- end
30
- end
31
- end
32
-
33
- # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
34
- #
35
- # This internal method was changed in Rails 5.0 by commit
36
- # https://github.com/rails/rails/commit/e038975 which added
37
- # left_outer_joins (see #make_polyamorous_left_outer_joins below) and added
38
- # passing an additional argument, `join_type`, to #join_constraints.
39
- #
40
- def join_constraints(outer_joins, join_type)
41
- joins = join_root.children.flat_map { |child|
42
- if join_type == Arel::Nodes::OuterJoin
43
- make_polyamorous_left_outer_joins join_root, child
44
- else
45
- make_polyamorous_inner_joins join_root, child
46
- end
47
- }
48
-
49
- joins.concat outer_joins.flat_map { |oj|
50
- if join_root.match? oj.join_root
51
- walk(join_root, oj.join_root)
52
- else
53
- oj.join_root.children.flat_map { |child|
54
- make_outer_joins(oj.join_root, child)
55
- }
56
- end
57
- }
58
- end
59
-
60
- # Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins,
61
- # a new method that was added in Rails 5.0 with the following commit:
62
- # https://github.com/rails/rails/commit/e038975
63
- #
64
- def make_polyamorous_left_outer_joins(parent, child)
65
- tables = child.tables
66
- join_type = Arel::Nodes::OuterJoin
67
- info = make_constraints parent, child, tables, join_type
68
-
69
- [info] + child.children.flat_map { |c|
70
- make_polyamorous_left_outer_joins(child, c)
71
- }
72
- end
73
-
74
- # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
75
- #
76
- def make_polyamorous_inner_joins(parent, child)
77
- tables = child.tables
78
- join_type = child.join_type || Arel::Nodes::InnerJoin
79
- info = make_constraints parent, child, tables, join_type
80
-
81
- [info] + child.children.flat_map { |c|
82
- make_polyamorous_inner_joins(child, c)
83
- }
84
- end
85
-
86
- private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins
87
-
88
- module ClassMethods
89
- # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
90
- #
91
- def walk_tree(associations, hash)
92
- case associations
93
- when TreeNode
94
- associations.add_to_tree(hash)
95
- when Hash
96
- associations.each do |k, v|
97
- cache =
98
- if TreeNode === k
99
- k.add_to_tree(hash)
100
- else
101
- hash[k] ||= {}
102
- end
103
- walk_tree(v, cache)
104
- end
105
- else
106
- super(associations, hash)
107
- end
108
- end
109
- end
110
-
111
- end
112
- end
@@ -1,31 +0,0 @@
1
- # active_record_5.2_ruby_2/join_association.rb
2
-
3
- module Polyamorous
4
- module JoinAssociationExtensions
5
- include SwappingReflectionClass
6
- def self.prepended(base)
7
- base.class_eval { attr_reader :join_type }
8
- end
9
-
10
- def initialize(reflection, children, alias_tracker, polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
11
- @join_type = join_type
12
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
13
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
14
- super(reflection, children, alias_tracker)
15
- self.reflection.options[:polymorphic] = true
16
- end
17
- else
18
- super(reflection, children, alias_tracker)
19
- end
20
- end
21
-
22
- def build_constraint(klass, table, key, foreign_table, foreign_key)
23
- if reflection.polymorphic?
24
- super(klass, table, key, foreign_table, foreign_key)
25
- .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
26
- else
27
- super(klass, table, key, foreign_table, foreign_key)
28
- end
29
- end
30
- end
31
- end
@@ -1,112 +0,0 @@
1
- # active_record_5.2_ruby_2/join_dependency.rb
2
-
3
- module Polyamorous
4
- module JoinDependencyExtensions
5
- # Replaces ActiveRecord::Associations::JoinDependency#build
6
- #
7
- def build(associations, base_klass)
8
- associations.map do |name, right|
9
- if name.is_a? Join
10
- reflection = find_reflection base_klass, name.name
11
- reflection.check_validity!
12
- reflection.check_eager_loadable!
13
-
14
- klass = if reflection.polymorphic?
15
- name.klass || base_klass
16
- else
17
- reflection.klass
18
- end
19
- JoinAssociation.new(reflection, build(right, klass), alias_tracker, name.klass, name.type)
20
- else
21
- reflection = find_reflection base_klass, name
22
- reflection.check_validity!
23
- reflection.check_eager_loadable!
24
-
25
- if reflection.polymorphic?
26
- raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
27
- end
28
- JoinAssociation.new(reflection, build(right, reflection.klass), alias_tracker)
29
- end
30
- end
31
- end
32
-
33
- # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
34
- #
35
- # This internal method was changed in Rails 5.0 by commit
36
- # https://github.com/rails/rails/commit/e038975 which added
37
- # left_outer_joins (see #make_polyamorous_left_outer_joins below) and added
38
- # passing an additional argument, `join_type`, to #join_constraints.
39
- #
40
- def join_constraints(outer_joins, join_type)
41
- joins = join_root.children.flat_map { |child|
42
- if join_type == Arel::Nodes::OuterJoin
43
- make_polyamorous_left_outer_joins join_root, child
44
- else
45
- make_polyamorous_inner_joins join_root, child
46
- end
47
- }
48
-
49
- joins.concat outer_joins.flat_map { |oj|
50
- if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
51
- walk(join_root, oj.join_root)
52
- else
53
- oj.join_root.children.flat_map { |child|
54
- make_outer_joins(oj.join_root, child)
55
- }
56
- end
57
- }
58
- end
59
-
60
- # Replaces ActiveRecord::Associations::JoinDependency#make_left_outer_joins,
61
- # a new method that was added in Rails 5.0 with the following commit:
62
- # https://github.com/rails/rails/commit/e038975
63
- #
64
- def make_polyamorous_left_outer_joins(parent, child)
65
- tables = child.tables
66
- join_type = Arel::Nodes::OuterJoin
67
- info = make_constraints parent, child, tables, join_type
68
-
69
- info + child.children.flat_map { |c|
70
- make_polyamorous_left_outer_joins(child, c)
71
- }
72
- end
73
-
74
- # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
75
- #
76
- def make_polyamorous_inner_joins(parent, child)
77
- tables = child.tables
78
- join_type = child.join_type || Arel::Nodes::InnerJoin
79
- info = make_constraints parent, child, tables, join_type
80
-
81
- info + child.children.flat_map { |c|
82
- make_polyamorous_inner_joins(child, c)
83
- }
84
- end
85
-
86
- private :make_polyamorous_inner_joins, :make_polyamorous_left_outer_joins
87
-
88
- module ClassMethods
89
- # Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
90
- #
91
- def walk_tree(associations, hash)
92
- case associations
93
- when TreeNode
94
- associations.add_to_tree(hash)
95
- when Hash
96
- associations.each do |k, v|
97
- cache =
98
- if TreeNode === k
99
- k.add_to_tree(hash)
100
- else
101
- hash[k] ||= {}
102
- end
103
- walk_tree(v, cache)
104
- end
105
- else
106
- super(associations, hash)
107
- end
108
- end
109
- end
110
-
111
- end
112
- end
@@ -1,12 +0,0 @@
1
- module Polyamorous
2
- module ReflectionExtensions
3
- def build_join_constraint(table, foreign_table)
4
- if polymorphic?
5
- super(table, foreign_table)
6
- .and(foreign_table[foreign_type].eq(klass.name))
7
- else
8
- super(table, foreign_table)
9
- end
10
- end
11
- end
12
- end