ransack 1.8.10 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.travis.yml +12 -3
  4. data/CHANGELOG.md +29 -5
  5. data/Gemfile +1 -9
  6. data/README.md +16 -25
  7. data/Rakefile +1 -22
  8. data/lib/polyamorous.rb +5 -18
  9. data/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +2 -2
  10. data/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +2 -2
  11. data/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb +38 -0
  12. data/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb +75 -0
  13. data/lib/ransack/adapters/active_record.rb +0 -9
  14. data/lib/ransack/adapters/active_record/base.rb +8 -0
  15. data/lib/ransack/adapters/active_record/context.rb +118 -182
  16. data/lib/ransack/adapters/active_record/ransack/context.rb +1 -8
  17. data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -5
  18. data/lib/ransack/constants.rb +1 -1
  19. data/lib/ransack/context.rb +4 -0
  20. data/lib/ransack/nodes/value.rb +1 -1
  21. data/lib/ransack/search.rb +1 -1
  22. data/lib/ransack/version.rb +1 -1
  23. data/ransack.gemspec +4 -3
  24. data/spec/helpers/polyamorous_helper.rb +7 -9
  25. data/spec/ransack/adapters/active_record/base_spec.rb +35 -0
  26. data/spec/ransack/adapters/active_record/context_spec.rb +3 -6
  27. data/spec/ransack/helpers/form_builder_spec.rb +3 -15
  28. data/spec/ransack/helpers/form_helper_spec.rb +11 -99
  29. data/spec/ransack/join_association_spec.rb +1 -6
  30. data/spec/ransack/join_dependency_spec.rb +1 -6
  31. data/spec/ransack/search_spec.rb +2 -2
  32. data/spec/support/schema.rb +3 -21
  33. metadata +10 -83
  34. data/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_association.rb +0 -76
  35. data/lib/polyamorous/activerecord_3_and_4.0_ruby_1.9/join_dependency.rb +0 -96
  36. data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_association.rb +0 -2
  37. data/lib/polyamorous/activerecord_4.1_ruby_1.9/join_dependency.rb +0 -4
  38. data/lib/polyamorous/activerecord_4.1_ruby_2/join_association.rb +0 -2
  39. data/lib/polyamorous/activerecord_4.1_ruby_2/join_dependency.rb +0 -3
  40. data/lib/polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins.rb +0 -14
  41. data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_association.rb +0 -46
  42. data/lib/polyamorous/activerecord_4.2_ruby_1.9/join_dependency.rb +0 -87
  43. data/lib/polyamorous/activerecord_4.2_ruby_2/join_association.rb +0 -2
  44. data/lib/polyamorous/activerecord_4.2_ruby_2/join_dependency.rb +0 -24
  45. data/lib/ransack/adapters/active_record/3.0/compat.rb +0 -173
  46. data/lib/ransack/adapters/active_record/3.0/context.rb +0 -203
  47. data/lib/ransack/adapters/active_record/3.1/context.rb +0 -212
  48. data/lib/ransack/adapters/active_record/3.2/context.rb +0 -44
  49. data/lib/ransack/adapters/mongoid.rb +0 -15
  50. data/lib/ransack/adapters/mongoid/3.2/.gitkeep +0 -0
  51. data/lib/ransack/adapters/mongoid/attributes/attribute.rb +0 -37
  52. data/lib/ransack/adapters/mongoid/attributes/order_predications.rb +0 -17
  53. data/lib/ransack/adapters/mongoid/attributes/predications.rb +0 -141
  54. data/lib/ransack/adapters/mongoid/base.rb +0 -134
  55. data/lib/ransack/adapters/mongoid/context.rb +0 -212
  56. data/lib/ransack/adapters/mongoid/inquiry_hash.rb +0 -23
  57. data/lib/ransack/adapters/mongoid/ransack/constants.rb +0 -88
  58. data/lib/ransack/adapters/mongoid/ransack/context.rb +0 -59
  59. data/lib/ransack/adapters/mongoid/ransack/nodes/condition.rb +0 -22
  60. data/lib/ransack/adapters/mongoid/ransack/translate.rb +0 -13
  61. data/lib/ransack/adapters/mongoid/ransack/visitor.rb +0 -18
  62. data/lib/ransack/adapters/mongoid/table.rb +0 -35
  63. data/spec/mongoid/adapters/mongoid/base_spec.rb +0 -314
  64. data/spec/mongoid/adapters/mongoid/context_spec.rb +0 -56
  65. data/spec/mongoid/configuration_spec.rb +0 -162
  66. data/spec/mongoid/dependencies_spec.rb +0 -8
  67. data/spec/mongoid/helpers/ransack_helper.rb +0 -11
  68. data/spec/mongoid/nodes/condition_spec.rb +0 -49
  69. data/spec/mongoid/nodes/grouping_spec.rb +0 -13
  70. data/spec/mongoid/predicate_spec.rb +0 -155
  71. data/spec/mongoid/search_spec.rb +0 -445
  72. data/spec/mongoid/support/mongoid.yml +0 -11
  73. data/spec/mongoid/support/schema.rb +0 -135
  74. data/spec/mongoid/translate_spec.rb +0 -14
  75. data/spec/mongoid_spec_helper.rb +0 -63
  76. data/spec/ransack/dependencies_spec.rb +0 -12
@@ -1,76 +0,0 @@
1
- # active_record_3_and_4.0_ruby_1.9/join_association.rb
2
- module Polyamorous
3
- module JoinAssociationExtensions
4
- include SwappingReflectionClass
5
- def self.included(base)
6
- base.class_eval do
7
- alias_method_chain :initialize, :polymorphism
8
- alias_method :equality_without_polymorphism, :==
9
- alias_method :==, :equality_with_polymorphism
10
- if base.method_defined?(:active_record)
11
- alias_method :base_klass, :active_record
12
- end
13
-
14
- if ActiveRecord::VERSION::STRING =~ /^3\.0\./
15
- alias_method_chain :association_join, :polymorphism
16
- else
17
- alias_method_chain :build_constraint, :polymorphism
18
- end
19
- end
20
- end
21
-
22
- def initialize_with_polymorphism(
23
- reflection, join_dependency, parent = nil, polymorphic_class = nil
24
- )
25
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
26
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
27
- initialize_without_polymorphism(reflection, join_dependency, parent)
28
- self.reflection.options[:polymorphic] = true
29
- end
30
- else
31
- initialize_without_polymorphism(reflection, join_dependency, parent)
32
- end
33
- end
34
-
35
- def equality_with_polymorphism(other)
36
- equality_without_polymorphism(other) && base_klass == other.base_klass
37
- end
38
-
39
- def build_constraint_with_polymorphism(
40
- reflection, table, key, foreign_table, foreign_key
41
- )
42
- if reflection.options[:polymorphic]
43
- build_constraint_without_polymorphism(
44
- reflection, table, key, foreign_table, foreign_key
45
- )
46
- .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
47
- else
48
- build_constraint_without_polymorphism(
49
- reflection, table, key, foreign_table, foreign_key
50
- )
51
- end
52
- end
53
-
54
- def association_join_with_polymorphism
55
- return @join if @Join
56
- @join = association_join_without_polymorphism
57
- if reflection.macro == :belongs_to && reflection.options[:polymorphic]
58
- aliased_table = Arel::Table.new(
59
- table_name,
60
- as: @aliased_table_name,
61
- engine: arel_engine,
62
- columns: klass.columns
63
- )
64
- parent_table = Arel::Table.new(
65
- parent.table_name,
66
- as: parent.aliased_table_name,
67
- engine: arel_engine,
68
- columns: parent.base_klass.columns
69
- )
70
- @join << parent_table[reflection.options[:foreign_type]]
71
- .eq(reflection.klass.name)
72
- end
73
- @join
74
- end
75
- end
76
- end
@@ -1,96 +0,0 @@
1
- # active_record_3_and_4.0_ruby_1.9/join_dependency.rb
2
- module Polyamorous
3
- module JoinDependencyExtensions
4
- def self.included(base)
5
- base.class_eval do
6
- alias_method_chain :build, :polymorphism
7
- alias_method_chain :graft, :polymorphism
8
- if base.method_defined?(:active_record)
9
- alias_method :base_klass, :active_record
10
- end
11
- end
12
- end
13
-
14
- def graft_with_polymorphism(*associations)
15
- associations.each do |association|
16
- unless join_associations.detect { |a| association == a }
17
- if association.reflection.options[:polymorphic]
18
- build(
19
- Join.new(
20
- association.reflection.name,
21
- association.join_type,
22
- association.reflection.klass
23
- ),
24
- association.find_parent_in(self) || join_base,
25
- association.join_type
26
- )
27
- else
28
- build(
29
- association.reflection.name,
30
- association.find_parent_in(self) || join_base,
31
- association.join_type
32
- )
33
- end
34
- end
35
- end
36
- self
37
- end
38
-
39
- if ActiveRecord::VERSION::STRING =~ /^3\.0\./
40
- def _join_parts
41
- @joins
42
- end
43
- else
44
- def _join_parts
45
- @join_parts
46
- end
47
- end
48
-
49
- def build_with_polymorphism(
50
- associations, parent = nil, join_type = InnerJoin
51
- )
52
- case associations
53
- when Join
54
- parent ||= _join_parts.last
55
- reflection = parent.reflections[associations.name] or
56
- raise ::ActiveRecord::ConfigurationError,
57
- "Association named '#{associations.name
58
- }' was not found; perhaps you misspelled it?"
59
-
60
- unless join_association = find_join_association_respecting_polymorphism(
61
- reflection, parent, associations.klass
62
- )
63
- @reflections << reflection
64
- join_association = build_join_association_respecting_polymorphism(
65
- reflection, parent, associations.klass
66
- )
67
- join_association.join_type = associations.type
68
- _join_parts << join_association
69
- cache_joined_association(join_association)
70
- end
71
-
72
- join_association
73
- else
74
- build_without_polymorphism(associations, parent, join_type)
75
- end
76
- end
77
-
78
- def find_join_association_respecting_polymorphism(reflection, parent, klass)
79
- if association = find_join_association(reflection, parent)
80
- unless reflection.options[:polymorphic]
81
- association
82
- else
83
- association if association.base_klass == klass
84
- end
85
- end
86
- end
87
-
88
- def build_join_association_respecting_polymorphism(reflection, parent, klass)
89
- if reflection.options[:polymorphic] && klass
90
- JoinAssociation.new(reflection, self, parent, klass)
91
- else
92
- JoinAssociation.new(reflection, self, parent)
93
- end
94
- end
95
- end
96
- end
@@ -1,2 +0,0 @@
1
- # active_record_4.1_ruby_1.9/join_association.rb
2
- require 'polyamorous/activerecord_4.2_ruby_1.9/join_association'
@@ -1,4 +0,0 @@
1
- # active_record_4.1_ruby_1.9/join_dependency.rb
2
- require 'polyamorous/activerecord_4.2_ruby_2/join_dependency'
3
- require 'polyamorous/activerecord_4.2_ruby_1.9/join_dependency'
4
- require 'polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins'
@@ -1,2 +0,0 @@
1
- # active_record_4.1_ruby_2/join_association.rb
2
- require 'polyamorous/activerecord_5.0_ruby_2/join_association'
@@ -1,3 +0,0 @@
1
- # active_record_4.1_ruby_2/join_dependency.rb
2
- require 'polyamorous/activerecord_4.2_ruby_2/join_dependency'
3
- require 'polyamorous/activerecord_4.1_ruby_2/make_polyamorous_inner_joins'
@@ -1,14 +0,0 @@
1
- module Polyamorous
2
- module JoinDependencyExtensions
3
- # Replaces ActiveRecord::Associations::JoinDependency#make_inner_joins
4
- #
5
- def make_polyamorous_inner_joins(parent, child)
6
- make_constraints(
7
- parent, child, child.tables, child.join_type || Arel::Nodes::InnerJoin
8
- )
9
- .concat child.children.flat_map { |c|
10
- make_polyamorous_inner_joins(child, c)
11
- }
12
- end
13
- end
14
- end
@@ -1,46 +0,0 @@
1
- # active_record_4.2_ruby_1.9/join_association.rb
2
- module Polyamorous
3
- module JoinAssociationExtensions
4
- include SwappingReflectionClass
5
- def self.included(base)
6
- base.class_eval do
7
- attr_reader :join_type
8
- alias_method_chain :initialize, :polymorphism
9
- alias_method_chain :build_constraint, :polymorphism
10
- end
11
- end
12
-
13
- def initialize_with_polymorphism(reflection, children,
14
- polymorphic_class = nil, join_type = Arel::Nodes::InnerJoin)
15
- @join_type = join_type
16
- if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
17
- swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
18
- initialize_without_polymorphism(reflection, children)
19
- self.reflection.options[:polymorphic] = true
20
- end
21
- else
22
- initialize_without_polymorphism(reflection, children)
23
- end
24
- end
25
-
26
- # Reference https://github.com/rails/rails/commit/9b15db51b78028bfecdb85595624de4b838adbd1
27
- def ==(other)
28
- base_klass == other.base_klass
29
- end
30
-
31
- def build_constraint_with_polymorphism(
32
- klass, table, key, foreign_table, foreign_key
33
- )
34
- if reflection.polymorphic?
35
- build_constraint_without_polymorphism(
36
- klass, table, key, foreign_table, foreign_key
37
- )
38
- .and(foreign_table[reflection.foreign_type].eq(reflection.klass.name))
39
- else
40
- build_constraint_without_polymorphism(
41
- klass, table, key, foreign_table, foreign_key
42
- )
43
- end
44
- end
45
- end
46
- end
@@ -1,87 +0,0 @@
1
- # active_record_4.2_ruby_1.9/join_dependency.rb
2
- require 'polyamorous/activerecord_4.2_ruby_2/join_dependency'
3
-
4
- module Polyamorous
5
- module JoinDependencyExtensions
6
- def self.included(base)
7
- base.extend ClassMethods
8
- base.class_eval do
9
- class << self
10
- alias_method :walk_tree_without_polymorphism, :walk_tree
11
- alias_method :walk_tree, :walk_tree_with_polymorphism
12
- end
13
-
14
- alias_method :build_without_polymorphism, :build
15
- alias_method :build, :build_with_polymorphism
16
-
17
- alias_method :join_constraints_without_polymorphism, :join_constraints
18
- alias_method :join_constraints, :join_constraints_with_polymorphism
19
- end
20
- end
21
-
22
- # Replaces ActiveRecord::Associations::JoinDependency#build
23
- #
24
- def build_with_polymorphism(associations, base_klass)
25
- associations.map do |name, right|
26
- if name.is_a? Join
27
- reflection = find_reflection base_klass, name.name
28
- reflection.check_validity!
29
- klass = if reflection.polymorphic?
30
- name.klass || base_klass
31
- else
32
- reflection.klass
33
- end
34
- JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
35
- else
36
- reflection = find_reflection base_klass, name
37
- reflection.check_validity!
38
- if reflection.polymorphic?
39
- raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
40
- end
41
- JoinAssociation.new reflection, build(right, reflection.klass)
42
- end
43
- end
44
- end
45
-
46
- # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
47
- # to call #make_polyamorous_inner_joins instead of #make_inner_joins
48
- #
49
- def join_constraints_with_polymorphism(outer_joins)
50
- joins = join_root.children.flat_map { |child|
51
- make_polyamorous_inner_joins join_root, child
52
- }
53
- joins.concat outer_joins.flat_map { |oj|
54
- if join_root.match? oj.join_root
55
- walk(join_root, oj.join_root)
56
- else
57
- oj.join_root.children.flat_map { |child|
58
- make_outer_joins(oj.join_root, child)
59
- }
60
- end
61
- }
62
- end
63
-
64
- module ClassMethods
65
- # Replaces ActiveRecord::Associations::JoinDependency#self.walk_tree
66
- #
67
- def walk_tree_with_polymorphism(associations, hash)
68
- case associations
69
- when TreeNode
70
- associations.add_to_tree(hash)
71
- when Hash
72
- associations.each do |k, v|
73
- cache =
74
- if TreeNode === k
75
- k.add_to_tree(hash)
76
- else
77
- hash[k] ||= {}
78
- end
79
- walk_tree(v, cache)
80
- end
81
- else
82
- walk_tree_without_polymorphism(associations, hash)
83
- end
84
- end
85
- end
86
- end
87
- end
@@ -1,2 +0,0 @@
1
- # active_record_4.2_ruby_2/join_association.rb
2
- require 'polyamorous/activerecord_5.0_ruby_2/join_association'
@@ -1,24 +0,0 @@
1
- # active_record_4.2_ruby_2/join_dependency.rb
2
- require 'polyamorous/activerecord_5.0_ruby_2/join_dependency'
3
-
4
- module Polyamorous
5
- module JoinDependencyExtensions
6
- # Replaces ActiveRecord::Associations::JoinDependency#join_constraints
7
- # to call #make_polyamorous_inner_joins instead of #make_inner_joins.
8
- #
9
- def join_constraints(outer_joins)
10
- joins = join_root.children.flat_map { |child|
11
- make_polyamorous_inner_joins join_root, child
12
- }
13
- joins.concat outer_joins.flat_map { |oj|
14
- if join_root.match? oj.join_root
15
- walk(join_root, oj.join_root)
16
- else
17
- oj.join_root.children.flat_map { |child|
18
- make_outer_joins(oj.join_root, child)
19
- }
20
- end
21
- }
22
- end
23
- end
24
- end
@@ -1,173 +0,0 @@
1
- # UGLY, UGLY MONKEY PATCHES FOR BACKWARDS COMPAT!!! AVERT YOUR EYES!!
2
- if Arel::Nodes::And < Arel::Nodes::Binary
3
- class Ransack::Visitor
4
- def visit_Ransack_Nodes_And(object)
5
- nodes = object.values.map { |o| accept(o) }.compact
6
- nodes.inject(&:and)
7
- end
8
- end
9
- end
10
-
11
- class ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
12
- def table
13
- Arel::Table.new(
14
- table_name,
15
- :as => aliased_table_name,
16
- :engine => active_record.arel_engine,
17
- :columns => active_record.columns
18
- )
19
- end
20
- end
21
-
22
- module Arel
23
-
24
- class Table
25
- alias :table_name :name
26
-
27
- def [] name
28
- ::Arel::Attribute.new self, name.to_sym
29
- end
30
- end
31
-
32
- module Nodes
33
- class Node
34
- def not
35
- Nodes::Not.new self
36
- end
37
- end
38
-
39
- remove_const :And
40
- class And < Arel::Nodes::Node
41
- attr_reader :children
42
-
43
- def initialize children, right = nil
44
- unless Array === children
45
- children = [children, right]
46
- end
47
- @children = children
48
- end
49
-
50
- def left
51
- children.first
52
- end
53
-
54
- def right
55
- children[1]
56
- end
57
- end
58
-
59
- class NamedFunction < Arel::Nodes::Function
60
- attr_accessor :name, :distinct
61
-
62
- include Arel::Predications
63
-
64
- def initialize name, expr, aliaz = nil
65
- super(expr, aliaz)
66
- @name = name
67
- @distinct = false
68
- end
69
- end
70
-
71
- class InfixOperation < Binary
72
- include Arel::Expressions
73
- include Arel::Predications
74
-
75
- attr_reader :operator
76
-
77
- def initialize operator, left, right
78
- super(left, right)
79
- @operator = operator
80
- end
81
- end
82
-
83
- class Multiplication < InfixOperation
84
- def initialize left, right
85
- super(:*, left, right)
86
- end
87
- end
88
-
89
- class Division < InfixOperation
90
- def initialize left, right
91
- super(:/, left, right)
92
- end
93
- end
94
-
95
- class Addition < InfixOperation
96
- def initialize left, right
97
- super(:+, left, right)
98
- end
99
- end
100
-
101
- class Subtraction < InfixOperation
102
- def initialize left, right
103
- super(:-, left, right)
104
- end
105
- end
106
- end
107
-
108
- module Visitors
109
- class ToSql
110
- def column_for attr
111
- name = attr.name.to_s
112
- table = attr.relation.table_name
113
-
114
- column_cache[table][name]
115
- end
116
-
117
- def column_cache
118
- @column_cache ||= Hash.new do |hash, key|
119
- hash[key] = Hash[
120
- @engine.connection
121
- .columns(key, "#{key} Columns")
122
- .map { |c| [c.name, c] }
123
- ]
124
- end
125
- end
126
-
127
- def visit_Arel_Nodes_InfixOperation o
128
- "#{visit o.left} #{o.operator} #{visit o.right}"
129
- end
130
-
131
- def visit_Arel_Nodes_NamedFunction o
132
- "#{
133
- o.name
134
- }(#{
135
- o.distinct ? Ransack::Constants::DISTINCT : ''.freeze
136
- }#{
137
- o.expressions.map { |x| visit x }.join(', '.freeze)
138
- })#{
139
- o.alias ? " AS #{visit o.alias}" : ''.freeze
140
- }"
141
- end
142
-
143
- def visit_Arel_Nodes_And o
144
- o.children.map { |x| visit x }.join(' AND '.freeze)
145
- end
146
-
147
- def visit_Arel_Nodes_Not o
148
- "NOT (#{visit o.expr})"
149
- end
150
-
151
- def visit_Arel_Nodes_Values o
152
- "VALUES (#{
153
- o.expressions.zip(o.columns)
154
- .map { |value, attr|
155
- if Nodes::SqlLiteral === value
156
- visit_Arel_Nodes_SqlLiteral value
157
- else
158
- quote(value, attr && column_for(attr))
159
- end
160
- }
161
- .join(', '.freeze)
162
- })"
163
- end
164
- end
165
- end
166
-
167
- module Predications
168
- def as other
169
- Nodes::As.new self, Nodes::SqlLiteral.new(other)
170
- end
171
- end
172
-
173
- end