ransack 1.8.3 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.travis.yml +26 -63
- data/CHANGELOG.md +187 -24
- data/CONTRIBUTING.md +9 -0
- data/Gemfile +5 -20
- data/README.md +163 -40
- data/Rakefile +1 -22
- data/lib/ransack/adapters/active_record/base.rb +11 -2
- data/lib/ransack/adapters/active_record/context.rb +178 -168
- data/lib/ransack/adapters/active_record/ransack/constants.rb +6 -3
- data/lib/ransack/adapters/active_record/ransack/context.rb +10 -16
- data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +3 -3
- data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -5
- data/lib/ransack/adapters/active_record/ransack/visitor.rb +23 -0
- data/lib/ransack/adapters/active_record.rb +0 -9
- data/lib/ransack/adapters.rb +2 -0
- data/lib/ransack/configuration.rb +30 -4
- data/lib/ransack/constants.rb +4 -1
- data/lib/ransack/context.rb +29 -24
- data/lib/ransack/helpers/form_builder.rb +15 -3
- data/lib/ransack/helpers/form_helper.rb +8 -3
- data/lib/ransack/locale/ar.yml +70 -0
- data/lib/ransack/locale/az.yml +70 -0
- data/lib/ransack/locale/bg.yml +70 -0
- data/lib/ransack/locale/ca.yml +70 -0
- data/lib/ransack/locale/el.yml +70 -0
- data/lib/ransack/locale/es.yml +22 -22
- data/lib/ransack/locale/fa.yml +70 -0
- data/lib/ransack/locale/fi.yml +71 -0
- data/lib/ransack/locale/it.yml +70 -0
- data/lib/ransack/locale/nl.yml +4 -4
- data/lib/ransack/locale/ru.yml +70 -0
- data/lib/ransack/locale/tr.yml +70 -0
- data/lib/ransack/locale/zh-CN.yml +12 -12
- data/lib/ransack/nodes/attribute.rb +1 -1
- data/lib/ransack/nodes/grouping.rb +2 -7
- data/lib/ransack/nodes/value.rb +74 -68
- data/lib/ransack/predicate.rb +11 -19
- data/lib/ransack/search.rb +1 -1
- data/lib/ransack/translate.rb +115 -115
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack/visitor.rb +1 -12
- data/lib/ransack.rb +5 -2
- data/logo/ransack-h.png +0 -0
- data/logo/ransack-h.svg +34 -0
- data/logo/ransack-v.png +0 -0
- data/logo/ransack-v.svg +34 -0
- data/logo/ransack.png +0 -0
- data/logo/ransack.svg +21 -0
- data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +31 -0
- data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +112 -0
- data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +31 -0
- data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +112 -0
- data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb +12 -0
- data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb +22 -0
- data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb +81 -0
- data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +81 -0
- data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +2 -0
- data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +2 -0
- data/polyamorous/lib/polyamorous/join.rb +70 -0
- data/polyamorous/lib/polyamorous/swapping_reflection_class.rb +11 -0
- data/polyamorous/lib/polyamorous/tree_node.rb +7 -0
- data/polyamorous/lib/polyamorous/version.rb +3 -0
- data/polyamorous/lib/polyamorous.rb +29 -0
- data/polyamorous/polyamorous.gemspec +35 -0
- data/ransack.gemspec +9 -10
- data/spec/helpers/polyamorous_helper.rb +28 -0
- data/spec/ransack/adapters/active_record/base_spec.rb +74 -0
- data/spec/ransack/adapters/active_record/context_spec.rb +44 -6
- data/spec/ransack/configuration_spec.rb +17 -2
- data/spec/ransack/helpers/form_builder_spec.rb +3 -15
- data/spec/ransack/helpers/form_helper_spec.rb +88 -151
- data/spec/ransack/join_association_spec.rb +28 -0
- data/spec/ransack/join_dependency_spec.rb +97 -0
- data/spec/ransack/join_spec.rb +19 -0
- data/spec/ransack/predicate_spec.rb +16 -2
- data/spec/ransack/search_spec.rb +32 -3
- data/spec/spec_helper.rb +5 -0
- data/spec/support/schema.rb +45 -21
- metadata +81 -67
- data/lib/ransack/adapters/active_record/3.0/compat.rb +0 -179
- data/lib/ransack/adapters/active_record/3.0/context.rb +0 -203
- data/lib/ransack/adapters/active_record/3.1/context.rb +0 -212
- data/lib/ransack/adapters/active_record/3.2/context.rb +0 -44
- data/lib/ransack/adapters/active_record/compat.rb +0 -14
- data/lib/ransack/adapters/mongoid/3.2/.gitkeep +0 -0
- data/lib/ransack/adapters/mongoid/attributes/attribute.rb +0 -37
- data/lib/ransack/adapters/mongoid/attributes/order_predications.rb +0 -17
- data/lib/ransack/adapters/mongoid/attributes/predications.rb +0 -141
- data/lib/ransack/adapters/mongoid/base.rb +0 -134
- data/lib/ransack/adapters/mongoid/context.rb +0 -212
- data/lib/ransack/adapters/mongoid/inquiry_hash.rb +0 -23
- data/lib/ransack/adapters/mongoid/ransack/constants.rb +0 -88
- data/lib/ransack/adapters/mongoid/ransack/context.rb +0 -60
- data/lib/ransack/adapters/mongoid/ransack/nodes/condition.rb +0 -27
- data/lib/ransack/adapters/mongoid/ransack/translate.rb +0 -13
- data/lib/ransack/adapters/mongoid/ransack/visitor.rb +0 -24
- data/lib/ransack/adapters/mongoid/table.rb +0 -35
- data/lib/ransack/adapters/mongoid.rb +0 -15
- data/spec/mongoid/adapters/mongoid/base_spec.rb +0 -314
- data/spec/mongoid/adapters/mongoid/context_spec.rb +0 -56
- data/spec/mongoid/configuration_spec.rb +0 -162
- data/spec/mongoid/dependencies_spec.rb +0 -8
- data/spec/mongoid/helpers/ransack_helper.rb +0 -11
- data/spec/mongoid/nodes/condition_spec.rb +0 -49
- data/spec/mongoid/nodes/grouping_spec.rb +0 -13
- data/spec/mongoid/predicate_spec.rb +0 -155
- data/spec/mongoid/search_spec.rb +0 -445
- data/spec/mongoid/support/mongoid.yml +0 -11
- data/spec/mongoid/support/schema.rb +0 -135
- data/spec/mongoid/translate_spec.rb +0 -14
- data/spec/mongoid_spec_helper.rb +0 -63
- data/spec/ransack/dependencies_spec.rb +0 -12
@@ -1,179 +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
|
-
return nil unless nodes.size > 0
|
7
|
-
|
8
|
-
if nodes.size > 1
|
9
|
-
nodes.inject(&:and)
|
10
|
-
else
|
11
|
-
nodes.first
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase
|
18
|
-
def table
|
19
|
-
Arel::Table.new(
|
20
|
-
table_name,
|
21
|
-
:as => aliased_table_name,
|
22
|
-
:engine => active_record.arel_engine,
|
23
|
-
:columns => active_record.columns
|
24
|
-
)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module Arel
|
29
|
-
|
30
|
-
class Table
|
31
|
-
alias :table_name :name
|
32
|
-
|
33
|
-
def [] name
|
34
|
-
::Arel::Attribute.new self, name.to_sym
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
module Nodes
|
39
|
-
class Node
|
40
|
-
def not
|
41
|
-
Nodes::Not.new self
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
remove_const :And
|
46
|
-
class And < Arel::Nodes::Node
|
47
|
-
attr_reader :children
|
48
|
-
|
49
|
-
def initialize children, right = nil
|
50
|
-
unless Array === children
|
51
|
-
children = [children, right]
|
52
|
-
end
|
53
|
-
@children = children
|
54
|
-
end
|
55
|
-
|
56
|
-
def left
|
57
|
-
children.first
|
58
|
-
end
|
59
|
-
|
60
|
-
def right
|
61
|
-
children[1]
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
class NamedFunction < Arel::Nodes::Function
|
66
|
-
attr_accessor :name, :distinct
|
67
|
-
|
68
|
-
include Arel::Predications
|
69
|
-
|
70
|
-
def initialize name, expr, aliaz = nil
|
71
|
-
super(expr, aliaz)
|
72
|
-
@name = name
|
73
|
-
@distinct = false
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
class InfixOperation < Binary
|
78
|
-
include Arel::Expressions
|
79
|
-
include Arel::Predications
|
80
|
-
|
81
|
-
attr_reader :operator
|
82
|
-
|
83
|
-
def initialize operator, left, right
|
84
|
-
super(left, right)
|
85
|
-
@operator = operator
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
class Multiplication < InfixOperation
|
90
|
-
def initialize left, right
|
91
|
-
super(:*, left, right)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
class Division < InfixOperation
|
96
|
-
def initialize left, right
|
97
|
-
super(:/, left, right)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
class Addition < InfixOperation
|
102
|
-
def initialize left, right
|
103
|
-
super(:+, left, right)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
class Subtraction < InfixOperation
|
108
|
-
def initialize left, right
|
109
|
-
super(:-, left, right)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
module Visitors
|
115
|
-
class ToSql
|
116
|
-
def column_for attr
|
117
|
-
name = attr.name.to_s
|
118
|
-
table = attr.relation.table_name
|
119
|
-
|
120
|
-
column_cache[table][name]
|
121
|
-
end
|
122
|
-
|
123
|
-
def column_cache
|
124
|
-
@column_cache ||= Hash.new do |hash, key|
|
125
|
-
hash[key] = Hash[
|
126
|
-
@engine.connection
|
127
|
-
.columns(key, "#{key} Columns")
|
128
|
-
.map { |c| [c.name, c] }
|
129
|
-
]
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def visit_Arel_Nodes_InfixOperation o
|
134
|
-
"#{visit o.left} #{o.operator} #{visit o.right}"
|
135
|
-
end
|
136
|
-
|
137
|
-
def visit_Arel_Nodes_NamedFunction o
|
138
|
-
"#{
|
139
|
-
o.name
|
140
|
-
}(#{
|
141
|
-
o.distinct ? Ransack::Constants::DISTINCT : ''.freeze
|
142
|
-
}#{
|
143
|
-
o.expressions.map { |x| visit x }.join(', '.freeze)
|
144
|
-
})#{
|
145
|
-
o.alias ? " AS #{visit o.alias}" : ''.freeze
|
146
|
-
}"
|
147
|
-
end
|
148
|
-
|
149
|
-
def visit_Arel_Nodes_And o
|
150
|
-
o.children.map { |x| visit x }.join(' AND '.freeze)
|
151
|
-
end
|
152
|
-
|
153
|
-
def visit_Arel_Nodes_Not o
|
154
|
-
"NOT (#{visit o.expr})"
|
155
|
-
end
|
156
|
-
|
157
|
-
def visit_Arel_Nodes_Values o
|
158
|
-
"VALUES (#{
|
159
|
-
o.expressions.zip(o.columns)
|
160
|
-
.map { |value, attr|
|
161
|
-
if Nodes::SqlLiteral === value
|
162
|
-
visit_Arel_Nodes_SqlLiteral value
|
163
|
-
else
|
164
|
-
quote(value, attr && column_for(attr))
|
165
|
-
end
|
166
|
-
}
|
167
|
-
.join(', '.freeze)
|
168
|
-
})"
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
module Predications
|
174
|
-
def as other
|
175
|
-
Nodes::As.new self, Nodes::SqlLiteral.new(other)
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
end
|
@@ -1,203 +0,0 @@
|
|
1
|
-
require 'ransack/context'
|
2
|
-
require 'polyamorous'
|
3
|
-
require 'ransack/adapters/active_record/3.0/compat'
|
4
|
-
|
5
|
-
module Ransack
|
6
|
-
|
7
|
-
module Adapters
|
8
|
-
module ActiveRecord
|
9
|
-
class Context < ::Ransack::Context
|
10
|
-
|
11
|
-
# Because the AR::Associations namespace is insane
|
12
|
-
if defined? ::ActiveRecord::Associations::ClassMethods::JoinDependency
|
13
|
-
JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
|
14
|
-
end
|
15
|
-
|
16
|
-
# Redefine a few things for ActiveRecord 3.0.
|
17
|
-
|
18
|
-
def initialize(object, options = {})
|
19
|
-
super
|
20
|
-
@arel_visitor = Arel::Visitors.visitor_for @engine
|
21
|
-
end
|
22
|
-
|
23
|
-
def relation_for(object)
|
24
|
-
object.scoped
|
25
|
-
end
|
26
|
-
|
27
|
-
def evaluate(search, opts = {})
|
28
|
-
viz = Visitor.new
|
29
|
-
relation = @object.where(viz.accept(search.base))
|
30
|
-
if search.sorts.any?
|
31
|
-
relation = relation.except(:order)
|
32
|
-
.reorder(viz.accept(search.sorts))
|
33
|
-
end
|
34
|
-
if opts[:distinct]
|
35
|
-
relation.select(Constants::DISTINCT + @klass.quoted_table_name +
|
36
|
-
Constants::DOT_ASTERIX)
|
37
|
-
else
|
38
|
-
relation
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def attribute_method?(str, klass = @klass)
|
43
|
-
exists = false
|
44
|
-
|
45
|
-
if ransackable_attribute?(str, klass)
|
46
|
-
exists = true
|
47
|
-
elsif (segments = str.split(/_/)).size > 1
|
48
|
-
remainder = []
|
49
|
-
found_assoc = nil
|
50
|
-
while !found_assoc && remainder.unshift(segments.pop) &&
|
51
|
-
segments.size > 0 do
|
52
|
-
assoc, poly_class = unpolymorphize_association(
|
53
|
-
segments.join(Constants::UNDERSCORE)
|
54
|
-
)
|
55
|
-
if found_assoc = get_association(assoc, klass)
|
56
|
-
exists = attribute_method?(
|
57
|
-
remainder.join(Constants::UNDERSCORE),
|
58
|
-
poly_class || found_assoc.klass
|
59
|
-
)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
exists
|
65
|
-
end
|
66
|
-
|
67
|
-
def table_for(parent)
|
68
|
-
parent.table
|
69
|
-
end
|
70
|
-
|
71
|
-
def type_for(attr)
|
72
|
-
return nil unless attr && attr.valid?
|
73
|
-
klassify(attr.parent)
|
74
|
-
.columns_hash[attr.arel_attribute.name.to_s]
|
75
|
-
.type
|
76
|
-
end
|
77
|
-
|
78
|
-
# All dependent JoinAssociation items used in the search query
|
79
|
-
#
|
80
|
-
def join_associations
|
81
|
-
@join_dependency.join_associations
|
82
|
-
end
|
83
|
-
|
84
|
-
def join_sources
|
85
|
-
raise NotImplementedError,
|
86
|
-
"ActiveRecord 3.0 does not use join_sources or support joining relations with Arel::Join nodes. Use join_associations."
|
87
|
-
end
|
88
|
-
|
89
|
-
def alias_tracker
|
90
|
-
raise NotImplementedError,
|
91
|
-
"ActiveRecord 3.0 does not have an alias tracker"
|
92
|
-
end
|
93
|
-
|
94
|
-
private
|
95
|
-
|
96
|
-
def get_parent_and_attribute_name(str, parent = @base)
|
97
|
-
attr_name = nil
|
98
|
-
|
99
|
-
if ransackable_attribute?(str, klassify(parent))
|
100
|
-
attr_name = str
|
101
|
-
elsif (segments = str.split(/_/)).size > 1
|
102
|
-
remainder = []
|
103
|
-
found_assoc = nil
|
104
|
-
while remainder.unshift(segments.pop) && segments.size > 0 &&
|
105
|
-
!found_assoc do
|
106
|
-
assoc, klass = unpolymorphize_association(
|
107
|
-
segments.join(Constants::UNDERSCORE)
|
108
|
-
)
|
109
|
-
if found_assoc = get_association(assoc, parent)
|
110
|
-
join = build_or_find_association(
|
111
|
-
found_assoc.name, parent, klass
|
112
|
-
)
|
113
|
-
parent, attr_name = get_parent_and_attribute_name(
|
114
|
-
remainder.join(Constants::UNDERSCORE), join
|
115
|
-
)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
[parent, attr_name]
|
120
|
-
end
|
121
|
-
|
122
|
-
def get_association(str, parent = @base)
|
123
|
-
klass = klassify parent
|
124
|
-
ransackable_association?(str, klass) &&
|
125
|
-
klass.reflect_on_all_associations.detect { |a| a.name.to_s == str }
|
126
|
-
end
|
127
|
-
|
128
|
-
def join_dependency(relation)
|
129
|
-
if relation.respond_to?(:join_dependency) # Polyamorous enables this
|
130
|
-
relation.join_dependency
|
131
|
-
else
|
132
|
-
build_join_dependency(relation)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def build_join_dependency(relation)
|
137
|
-
buckets = relation.joins_values.group_by do |join|
|
138
|
-
case join
|
139
|
-
when String
|
140
|
-
Constants::STRING_JOIN
|
141
|
-
when Hash, Symbol, Array
|
142
|
-
Constants::ASSOCIATION_JOIN
|
143
|
-
when ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
|
144
|
-
Constants::STASHED_JOIN
|
145
|
-
when Arel::Nodes::Join
|
146
|
-
Constants::JOIN_NODE
|
147
|
-
else
|
148
|
-
raise 'unknown class: %s' % join.class.name
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
association_joins = buckets[Constants::ASSOCIATION_JOIN] || []
|
153
|
-
|
154
|
-
stashed_association_joins = buckets[Constants::STASHED_JOIN] || []
|
155
|
-
|
156
|
-
join_nodes = buckets[Constants::JOIN_NODE] || []
|
157
|
-
|
158
|
-
string_joins = (buckets[Constants::STRING_JOIN] || []).map(&:strip).uniq
|
159
|
-
|
160
|
-
join_list = relation.send :custom_join_sql, (string_joins + join_nodes)
|
161
|
-
|
162
|
-
join_dependency = JoinDependency.new(
|
163
|
-
relation.klass,
|
164
|
-
association_joins,
|
165
|
-
join_list
|
166
|
-
)
|
167
|
-
|
168
|
-
join_nodes.each do |join|
|
169
|
-
join_dependency.table_aliases[join.left.name.downcase] = 1
|
170
|
-
end
|
171
|
-
|
172
|
-
join_dependency.graft(*stashed_association_joins)
|
173
|
-
end
|
174
|
-
|
175
|
-
def build_or_find_association(name, parent = @base, klass = nil)
|
176
|
-
found_association = @join_dependency.join_associations
|
177
|
-
.detect do |assoc|
|
178
|
-
assoc.reflection.name == name &&
|
179
|
-
assoc.parent == parent &&
|
180
|
-
(!klass || assoc.reflection.klass == klass)
|
181
|
-
end
|
182
|
-
unless found_association
|
183
|
-
@join_dependency.send(
|
184
|
-
:build, Polyamorous::Join.new(name, @join_type, klass), parent
|
185
|
-
)
|
186
|
-
found_association = @join_dependency.join_associations.last
|
187
|
-
|
188
|
-
default_conditions = found_association.active_record.scoped.arel.constraints
|
189
|
-
if default_conditions.any?
|
190
|
-
and_default_conditions = "AND #{default_conditions.reduce(&:and).to_sql}"
|
191
|
-
end
|
192
|
-
|
193
|
-
# Leverage the stashed association functionality in AR
|
194
|
-
@object = @object.joins(found_association).joins(and_default_conditions)
|
195
|
-
end
|
196
|
-
|
197
|
-
found_association
|
198
|
-
end
|
199
|
-
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
@@ -1,212 +0,0 @@
|
|
1
|
-
require 'ransack/context'
|
2
|
-
require 'ransack/adapters/active_record/compat'
|
3
|
-
require 'polyamorous'
|
4
|
-
|
5
|
-
module Ransack
|
6
|
-
module Adapters
|
7
|
-
module ActiveRecord
|
8
|
-
class Context < ::Ransack::Context
|
9
|
-
|
10
|
-
# Redefine a few things for ActiveRecord 3.1.
|
11
|
-
|
12
|
-
def initialize(object, options = {})
|
13
|
-
super
|
14
|
-
@arel_visitor = Arel::Visitors.visitor_for @engine
|
15
|
-
end
|
16
|
-
|
17
|
-
def relation_for(object)
|
18
|
-
object.scoped
|
19
|
-
end
|
20
|
-
|
21
|
-
def evaluate(search, opts = {})
|
22
|
-
viz = Visitor.new
|
23
|
-
relation = @object.where(viz.accept(search.base))
|
24
|
-
if search.sorts.any?
|
25
|
-
relation = relation.except(:order)
|
26
|
-
.reorder(viz.accept(search.sorts))
|
27
|
-
end
|
28
|
-
if opts[:distinct]
|
29
|
-
relation.select(Constants::DISTINCT + @klass.quoted_table_name +
|
30
|
-
Constants::DOT_ASTERIX)
|
31
|
-
else
|
32
|
-
relation
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def attribute_method?(str, klass = @klass)
|
37
|
-
exists = false
|
38
|
-
|
39
|
-
if ransackable_attribute?(str, klass)
|
40
|
-
exists = true
|
41
|
-
elsif (segments = str.split(/_/)).size > 1
|
42
|
-
remainder = []
|
43
|
-
found_assoc = nil
|
44
|
-
while !found_assoc && remainder.unshift(segments.pop) &&
|
45
|
-
segments.size > 0 do
|
46
|
-
assoc, poly_class = unpolymorphize_association(
|
47
|
-
segments.join(Constants::UNDERSCORE)
|
48
|
-
)
|
49
|
-
if found_assoc = get_association(assoc, klass)
|
50
|
-
exists = attribute_method?(
|
51
|
-
remainder.join(Constants::UNDERSCORE),
|
52
|
-
poly_class || found_assoc.klass
|
53
|
-
)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
exists
|
59
|
-
end
|
60
|
-
|
61
|
-
def table_for(parent)
|
62
|
-
parent.table
|
63
|
-
end
|
64
|
-
|
65
|
-
def type_for(attr)
|
66
|
-
return nil unless attr && attr.valid?
|
67
|
-
name = attr.arel_attribute.name.to_s
|
68
|
-
table = attr.arel_attribute.relation.table_name
|
69
|
-
|
70
|
-
unless @engine.connection_pool.table_exists?(table)
|
71
|
-
raise "No table named #{table} exists"
|
72
|
-
end
|
73
|
-
|
74
|
-
@engine.connection_pool.columns_hash[table][name].type
|
75
|
-
end
|
76
|
-
|
77
|
-
def join_associations
|
78
|
-
@join_dependency.join_associations
|
79
|
-
end
|
80
|
-
|
81
|
-
# All dependent Arel::Join nodes used in the search query
|
82
|
-
#
|
83
|
-
# This could otherwise be done as `@object.arel.join_sources`, except
|
84
|
-
# that ActiveRecord's build_joins sets up its own JoinDependency.
|
85
|
-
# This extracts what we need to access the joins using our existing
|
86
|
-
# JoinDependency to track table aliases.
|
87
|
-
#
|
88
|
-
def join_sources
|
89
|
-
base = Arel::SelectManager.new(@object.engine, @object.table)
|
90
|
-
@object.joins_values.each do |assoc|
|
91
|
-
next unless assoc.is_a?(JoinDependency::JoinAssociation)
|
92
|
-
assoc.join_to(base)
|
93
|
-
end
|
94
|
-
base.join_sources
|
95
|
-
end
|
96
|
-
|
97
|
-
def alias_tracker
|
98
|
-
@join_dependency.alias_tracker
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
|
103
|
-
def get_parent_and_attribute_name(str, parent = @base)
|
104
|
-
attr_name = nil
|
105
|
-
|
106
|
-
if ransackable_attribute?(str, klassify(parent))
|
107
|
-
attr_name = str
|
108
|
-
elsif (segments = str.split(/_/)).size > 1
|
109
|
-
remainder = []
|
110
|
-
found_assoc = nil
|
111
|
-
while remainder.unshift(segments.pop) && segments.size > 0 &&
|
112
|
-
!found_assoc do
|
113
|
-
assoc, klass = unpolymorphize_association(
|
114
|
-
segments.join(Constants::UNDERSCORE)
|
115
|
-
)
|
116
|
-
if found_assoc = get_association(assoc, parent)
|
117
|
-
join = build_or_find_association(
|
118
|
-
found_assoc.name, parent, klass
|
119
|
-
)
|
120
|
-
parent, attr_name = get_parent_and_attribute_name(
|
121
|
-
remainder.join(Constants::UNDERSCORE), join
|
122
|
-
)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
[parent, attr_name]
|
128
|
-
end
|
129
|
-
|
130
|
-
def get_association(str, parent = @base)
|
131
|
-
klass = klassify parent
|
132
|
-
ransackable_association?(str, klass) &&
|
133
|
-
klass.reflect_on_all_associations.detect { |a| a.name.to_s == str }
|
134
|
-
end
|
135
|
-
|
136
|
-
def join_dependency(relation)
|
137
|
-
if relation.respond_to?(:join_dependency) # Polyamorous enables this
|
138
|
-
relation.join_dependency
|
139
|
-
else
|
140
|
-
build_join_dependency(relation)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
def build_join_dependency(relation)
|
145
|
-
buckets = relation.joins_values.group_by do |join|
|
146
|
-
case join
|
147
|
-
when String
|
148
|
-
Constants::STRING_JOIN
|
149
|
-
when Hash, Symbol, Array
|
150
|
-
Constants::ASSOCIATION_JOIN
|
151
|
-
when JoinDependency::JoinAssociation
|
152
|
-
Constants::STASHED_JOIN
|
153
|
-
when Arel::Nodes::Join
|
154
|
-
Constants::JOIN_NODE
|
155
|
-
else
|
156
|
-
raise 'unknown class: %s' % join.class.name
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
association_joins = buckets[Constants::ASSOCIATION_JOIN] || []
|
161
|
-
|
162
|
-
stashed_association_joins = buckets[Constants::STASHED_JOIN] || []
|
163
|
-
|
164
|
-
join_nodes = buckets[Constants::JOIN_NODE] || []
|
165
|
-
|
166
|
-
string_joins = (buckets[Constants::STRING_JOIN] || []).map(&:strip).uniq
|
167
|
-
|
168
|
-
join_list = relation.send :custom_join_ast,
|
169
|
-
relation.table.from(relation.table), string_joins
|
170
|
-
|
171
|
-
join_dependency = JoinDependency.new(
|
172
|
-
relation.klass,
|
173
|
-
association_joins,
|
174
|
-
join_list
|
175
|
-
)
|
176
|
-
|
177
|
-
join_nodes.each do |join|
|
178
|
-
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
|
179
|
-
end
|
180
|
-
|
181
|
-
join_dependency.graft(*stashed_association_joins)
|
182
|
-
end
|
183
|
-
|
184
|
-
def build_or_find_association(name, parent = @base, klass = nil)
|
185
|
-
found_association = @join_dependency.join_associations
|
186
|
-
.detect do |assoc|
|
187
|
-
assoc.reflection.name == name &&
|
188
|
-
assoc.parent == parent &&
|
189
|
-
(!klass || assoc.reflection.klass == klass)
|
190
|
-
end
|
191
|
-
unless found_association
|
192
|
-
@join_dependency.send(
|
193
|
-
:build, Polyamorous::Join.new(name, @join_type, klass), parent
|
194
|
-
)
|
195
|
-
found_association = @join_dependency.join_associations.last
|
196
|
-
|
197
|
-
default_conditions = found_association.active_record.scoped.arel.constraints
|
198
|
-
if default_conditions.any?
|
199
|
-
and_default_conditions = "AND #{default_conditions.reduce(&:and).to_sql}"
|
200
|
-
end
|
201
|
-
|
202
|
-
# Leverage the stashed association functionality in AR
|
203
|
-
@object = @object.joins(found_association).joins(and_default_conditions)
|
204
|
-
end
|
205
|
-
|
206
|
-
found_association
|
207
|
-
end
|
208
|
-
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
@@ -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
|
File without changes
|
@@ -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
|