ransack 2.3.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +3 -0
- data/.github/SECURITY.md +12 -0
- data/.github/workflows/codeql.yml +72 -0
- data/.github/workflows/cronjob.yml +102 -0
- data/.github/workflows/deploy.yml +35 -0
- data/.github/workflows/rubocop.yml +20 -0
- data/.github/workflows/test-deploy.yml +29 -0
- data/.github/workflows/test.yml +128 -0
- data/.nojekyll +0 -0
- data/.rubocop.yml +47 -0
- data/CHANGELOG.md +228 -1
- data/CONTRIBUTING.md +47 -22
- data/Gemfile +24 -10
- data/README.md +49 -917
- data/bug_report_templates/test-ransack-scope-and-column-same-name.rb +78 -0
- data/bug_report_templates/test-ransacker-arel-present-predicate.rb +75 -0
- data/docs/.gitignore +19 -0
- data/docs/.nojekyll +0 -0
- data/docs/babel.config.js +3 -0
- data/docs/blog/2022-03-27-ransack-3.0.0.md +20 -0
- data/docs/docs/getting-started/_category_.json +4 -0
- data/docs/docs/getting-started/advanced-mode.md +46 -0
- data/docs/docs/getting-started/configuration.md +47 -0
- data/docs/docs/getting-started/search-matches.md +67 -0
- data/docs/docs/getting-started/simple-mode.md +288 -0
- data/docs/docs/getting-started/sorting.md +79 -0
- data/docs/docs/getting-started/using-predicates.md +282 -0
- data/docs/docs/going-further/_category_.json +4 -0
- data/docs/docs/going-further/acts-as-taggable-on.md +114 -0
- data/docs/docs/going-further/associations.md +70 -0
- data/docs/docs/going-further/custom-predicates.md +52 -0
- data/docs/docs/going-further/documentation.md +43 -0
- data/docs/docs/going-further/exporting-to-csv.md +49 -0
- data/docs/docs/going-further/external-guides.md +57 -0
- data/docs/docs/going-further/form-customisation.md +63 -0
- data/docs/docs/going-further/i18n.md +53 -0
- data/docs/docs/going-further/img/create_release.png +0 -0
- data/docs/docs/going-further/merging-searches.md +41 -0
- data/docs/docs/going-further/other-notes.md +428 -0
- data/docs/docs/going-further/polymorphic-search.md +40 -0
- data/docs/docs/going-further/ransackers.md +331 -0
- data/docs/docs/going-further/release_process.md +36 -0
- data/docs/docs/going-further/saving-queries.md +82 -0
- data/docs/docs/going-further/searching-postgres.md +57 -0
- data/docs/docs/going-further/wiki-contributors.md +82 -0
- data/docs/docs/intro.md +99 -0
- data/docs/docusaurus.config.js +120 -0
- data/docs/package.json +42 -0
- data/docs/sidebars.js +31 -0
- data/docs/src/components/HomepageFeatures/index.js +64 -0
- data/docs/src/components/HomepageFeatures/styles.module.css +11 -0
- data/docs/src/css/custom.css +39 -0
- data/docs/src/pages/index.module.css +23 -0
- data/docs/src/pages/markdown-page.md +7 -0
- data/docs/static/.nojekyll +0 -0
- data/docs/static/img/docusaurus.png +0 -0
- data/docs/static/img/favicon.ico +0 -0
- data/docs/static/img/logo.svg +1 -0
- data/docs/static/img/tutorial/docsVersionDropdown.png +0 -0
- data/docs/static/img/tutorial/localeDropdown.png +0 -0
- data/docs/static/img/undraw_docusaurus_mountain.svg +171 -0
- data/docs/static/img/undraw_docusaurus_react.svg +170 -0
- data/docs/static/img/undraw_docusaurus_tree.svg +40 -0
- data/docs/yarn.lock +8790 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +70 -0
- data/{polyamorous/lib/polyamorous/activerecord_6.0_ruby_2 → lib/polyamorous/activerecord_6.1_ruby_2}/join_dependency.rb +23 -12
- data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +11 -0
- data/lib/polyamorous/activerecord_7.0_ruby_2/join_association.rb +1 -0
- data/lib/polyamorous/activerecord_7.0_ruby_2/join_dependency.rb +1 -0
- data/lib/polyamorous/activerecord_7.0_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/activerecord_7.1_ruby_2/join_association.rb +1 -0
- data/lib/polyamorous/activerecord_7.1_ruby_2/join_dependency.rb +1 -0
- data/lib/polyamorous/activerecord_7.1_ruby_2/reflection.rb +1 -0
- data/{polyamorous/lib → lib/polyamorous}/polyamorous.rb +3 -8
- data/lib/ransack/adapters/active_record/base.rb +83 -10
- data/lib/ransack/adapters/active_record/context.rb +59 -115
- data/lib/ransack/configuration.rb +53 -10
- data/lib/ransack/constants.rb +126 -7
- data/lib/ransack/context.rb +34 -5
- data/lib/ransack/helpers/form_builder.rb +11 -17
- data/lib/ransack/helpers/form_helper.rb +14 -5
- data/lib/ransack/helpers.rb +1 -1
- data/lib/ransack/locale/sk.yml +70 -0
- data/lib/ransack/locale/sv.yml +70 -0
- data/lib/ransack/nodes/attribute.rb +3 -3
- data/lib/ransack/nodes/condition.rb +87 -8
- data/lib/ransack/nodes/grouping.rb +4 -4
- data/lib/ransack/nodes/node.rb +1 -1
- data/lib/ransack/nodes/sort.rb +3 -3
- data/lib/ransack/nodes/value.rb +3 -3
- data/lib/ransack/predicate.rb +3 -2
- data/lib/ransack/ransacker.rb +1 -1
- data/lib/ransack/search.rb +15 -7
- data/lib/ransack/translate.rb +6 -6
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack/visitor.rb +38 -2
- data/lib/ransack.rb +6 -10
- data/ransack.gemspec +9 -24
- data/spec/blueprints/articles.rb +1 -1
- data/spec/blueprints/comments.rb +1 -1
- data/spec/blueprints/notes.rb +1 -1
- data/spec/blueprints/tags.rb +1 -1
- data/spec/console.rb +5 -5
- data/spec/helpers/polyamorous_helper.rb +2 -17
- data/spec/helpers/ransack_helper.rb +1 -1
- data/spec/polyamorous/activerecord_compatibility_spec.rb +15 -0
- data/spec/{ransack → polyamorous}/join_association_spec.rb +3 -1
- data/spec/{ransack → polyamorous}/join_dependency_spec.rb +0 -16
- data/spec/ransack/adapters/active_record/base_spec.rb +109 -16
- data/spec/ransack/adapters/active_record/context_spec.rb +19 -18
- data/spec/ransack/configuration_spec.rb +33 -9
- data/spec/ransack/helpers/form_builder_spec.rb +8 -8
- data/spec/ransack/helpers/form_helper_spec.rb +109 -20
- data/spec/ransack/nodes/condition_spec.rb +37 -0
- data/spec/ransack/nodes/grouping_spec.rb +2 -2
- data/spec/ransack/nodes/value_spec.rb +115 -0
- data/spec/ransack/predicate_spec.rb +75 -2
- data/spec/ransack/search_spec.rb +239 -38
- data/spec/ransack/translate_spec.rb +1 -1
- data/spec/spec_helper.rb +9 -5
- data/spec/support/schema.rb +83 -12
- metadata +105 -195
- data/.travis.yml +0 -49
- data/lib/ransack/adapters/active_record/ransack/constants.rb +0 -116
- data/lib/ransack/adapters/active_record/ransack/context.rb +0 -60
- data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +0 -61
- data/lib/ransack/adapters/active_record/ransack/translate.rb +0 -8
- data/lib/ransack/adapters/active_record/ransack/visitor.rb +0 -47
- data/lib/ransack/adapters.rb +0 -64
- data/lib/ransack/nodes.rb +0 -8
- data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_association.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_5.0_ruby_2/join_dependency.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_association.rb +0 -31
- data/polyamorous/lib/polyamorous/activerecord_5.1_ruby_2/join_dependency.rb +0 -112
- data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_association.rb +0 -31
- data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/join_dependency.rb +0 -112
- data/polyamorous/lib/polyamorous/activerecord_5.2.0_ruby_2/reflection.rb +0 -12
- data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_association.rb +0 -22
- data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/join_dependency.rb +0 -81
- data/polyamorous/lib/polyamorous/activerecord_5.2.1_ruby_2/reflection.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +0 -2
- data/polyamorous/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +0 -2
- data/polyamorous/lib/polyamorous/version.rb +0 -3
- data/polyamorous/polyamorous.gemspec +0 -35
- /data/{logo → docs/static/logo}/ransack-h.png +0 -0
- /data/{logo → docs/static/logo}/ransack-h.svg +0 -0
- /data/{logo → docs/static/logo}/ransack-v.png +0 -0
- /data/{logo → docs/static/logo}/ransack-v.svg +0 -0
- /data/{logo → docs/static/logo}/ransack.png +0 -0
- /data/{logo → docs/static/logo}/ransack.svg +0 -0
- /data/{polyamorous/lib → lib}/polyamorous/join.rb +0 -0
- /data/{polyamorous/lib → lib}/polyamorous/swapping_reflection_class.rb +0 -0
- /data/{polyamorous/lib → lib}/polyamorous/tree_node.rb +0 -0
- /data/lib/ransack/{adapters/active_record.rb → active_record.rb} +0 -0
- /data/spec/{ransack → polyamorous}/join_spec.rb +0 -0
@@ -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,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
|
data/lib/ransack/adapters.rb
DELETED
@@ -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,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
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# active_record_5.2.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
|
-
end
|
22
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
# active_record_5.2.1_ruby_2/join_dependency.rb
|
2
|
-
|
3
|
-
module Polyamorous
|
4
|
-
module JoinDependencyExtensions
|
5
|
-
# Replaces ActiveRecord::Associations::JoinDependency#build
|
6
|
-
def build(associations, base_klass)
|
7
|
-
associations.map do |name, right|
|
8
|
-
if name.is_a? Join
|
9
|
-
reflection = find_reflection base_klass, name.name
|
10
|
-
reflection.check_validity!
|
11
|
-
reflection.check_eager_loadable!
|
12
|
-
|
13
|
-
klass = if reflection.polymorphic?
|
14
|
-
name.klass || base_klass
|
15
|
-
else
|
16
|
-
reflection.klass
|
17
|
-
end
|
18
|
-
JoinAssociation.new(reflection, build(right, klass), name.klass, name.type)
|
19
|
-
else
|
20
|
-
reflection = find_reflection base_klass, name
|
21
|
-
reflection.check_validity!
|
22
|
-
reflection.check_eager_loadable!
|
23
|
-
|
24
|
-
if reflection.polymorphic?
|
25
|
-
raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
|
26
|
-
end
|
27
|
-
JoinAssociation.new(reflection, build(right, reflection.klass))
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def join_constraints(joins_to_add, join_type, alias_tracker)
|
33
|
-
@alias_tracker = alias_tracker
|
34
|
-
|
35
|
-
construct_tables!(join_root)
|
36
|
-
joins = make_join_constraints(join_root, join_type)
|
37
|
-
|
38
|
-
joins.concat joins_to_add.flat_map { |oj|
|
39
|
-
construct_tables!(oj.join_root)
|
40
|
-
if join_root.match?(oj.join_root) && join_root.table.name == oj.join_root.table.name
|
41
|
-
walk join_root, oj.join_root
|
42
|
-
else
|
43
|
-
make_join_constraints(oj.join_root, join_type)
|
44
|
-
end
|
45
|
-
}
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
def make_constraints(parent, child, join_type = Arel::Nodes::OuterJoin)
|
50
|
-
foreign_table = parent.table
|
51
|
-
foreign_klass = parent.base_klass
|
52
|
-
join_type = child.join_type || join_type if join_type == Arel::Nodes::InnerJoin
|
53
|
-
joins = child.join_constraints(foreign_table, foreign_klass, join_type, alias_tracker)
|
54
|
-
joins.concat child.children.flat_map { |c| make_constraints(child, c, join_type) }
|
55
|
-
end
|
56
|
-
|
57
|
-
module ClassMethods
|
58
|
-
# Prepended before ActiveRecord::Associations::JoinDependency#walk_tree
|
59
|
-
#
|
60
|
-
def walk_tree(associations, hash)
|
61
|
-
case associations
|
62
|
-
when TreeNode
|
63
|
-
associations.add_to_tree(hash)
|
64
|
-
when Hash
|
65
|
-
associations.each do |k, v|
|
66
|
-
cache =
|
67
|
-
if TreeNode === k
|
68
|
-
k.add_to_tree(hash)
|
69
|
-
else
|
70
|
-
hash[k] ||= {}
|
71
|
-
end
|
72
|
-
walk_tree(v, cache)
|
73
|
-
end
|
74
|
-
else
|
75
|
-
super(associations, hash)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "polyamorous/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "polyamorous"
|
7
|
-
s.version = Polyamorous::VERSION
|
8
|
-
s.authors = ["Ernie Miller", "Ryan Bigg", "Jon Atack", "Xiang Li"]
|
9
|
-
s.email = ["ernie@erniemiller.org", "radarlistener@gmail.com", "jonnyatack@gmail.com", "bigxiang@gmail.com"]
|
10
|
-
s.homepage = "https://github.com/activerecord-hackery/ransack/tree/master/polyamorous"
|
11
|
-
s.license = "MIT"
|
12
|
-
s.summary = %q{
|
13
|
-
Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...
|
14
|
-
}
|
15
|
-
s.description = %q{
|
16
|
-
This is just an extraction from Ransack/Squeel. You probably don't want to use this
|
17
|
-
directly. It extends ActiveRecord's associations to support polymorphic belongs_to
|
18
|
-
associations.
|
19
|
-
}
|
20
|
-
|
21
|
-
s.add_dependency 'activerecord', '>= 5.0'
|
22
|
-
s.add_development_dependency 'rspec', '~> 3'
|
23
|
-
s.add_development_dependency 'machinist', '~> 1.0.6'
|
24
|
-
s.add_development_dependency 'faker', '~> 1.6.5'
|
25
|
-
s.add_development_dependency 'sqlite3', '~> 1.3.3'
|
26
|
-
|
27
|
-
s.files = `git ls-files`.split("\n")
|
28
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
29
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
30
|
-
s.require_paths = ["lib"]
|
31
|
-
|
32
|
-
# specify any dependencies here; for example:
|
33
|
-
# s.add_development_dependency "rspec"
|
34
|
-
# s.add_runtime_dependency "rest-client"
|
35
|
-
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|