baby_squeel 1.4.0.beta1 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2df3535278f1724c6db1656ab9c5bbe0e9097e80ee6f2c1fdaeb3491bc838ae9
4
- data.tar.gz: bddd464023f9f53f0ff32cc1fbaa88928f5736b3e6541418b8c36f26b01b8d34
3
+ metadata.gz: c1c744d9857aadfec2192c82fef09f2fb3f1c67765852da080bfcf3a1f5aba72
4
+ data.tar.gz: d3dfb10bcf6496c530f29a87146a53039c925cc3dc86fe9cfbced8eb8d0afd6f
5
5
  SHA512:
6
- metadata.gz: 44f8ecb2269017a1e141f0f90d5c9a7eee7d5b877500897aba0a60646f38de0cdae4b992f505d022740c03e73d6293990d9f509d5b3188b5be8051819e168069
7
- data.tar.gz: a63f7b474504ef598508879d8ad702396f194c663cc46ececdb9c8dd31aabb10752323332bc26b3cec5cfb2df93e5328e11d2ce1983efe23793c618ccce9294d
6
+ metadata.gz: 211d541b43c1117bf46f79f220c34fbe5959fbc3597440ccfc3b4565fe087192d23342d365f757bda6ed5f03ba80cfbe2b03eea6e40955489f53e5f5813bb688
7
+ data.tar.gz: 431218e9d183f1507095036417af43b3d80b2189bb0c9abab5dc9e99c995f44cd79a0965b249ae468442ce3fa7436c8b86db2258efa6531a5ed80d00f16495ee
data/CHANGELOG.md CHANGED
@@ -1,6 +1,21 @@
1
1
  ## [Unreleased]
2
2
 
3
- Nothing to see here.
3
+ ## [1.4.1] - 2021-06-17
4
+ ## Fixed
5
+ - Fixed a bug related to checking the Active Record version.
6
+
7
+ ## [1.4.0] - 2021-06-17
8
+ ## Fixed
9
+ - Fix table alias when joining a polymorphic table twice (#108)
10
+ - Removed internal class `BabySqueel::Pluck`. You can still use `plucking`. For example, `Post.joining { author }.plucking { author.name }`
11
+ - Removed old code from Active Record < 5.2
12
+ - Removed dependency `join_dependency`
13
+
14
+ ## [1.4.0.beta1] - 2021-04-21
15
+ ## Fixed
16
+ - Add Support for activerecord '>= 5.2.3'
17
+ - Drop Support for Active Record versions that have reached EOL (activerecord < 5.2)
18
+ - Use polyamorous from ransack '~> 2.3'
4
19
 
5
20
  ## [1.3.1] - 2018-05-15
6
21
  ## Fixed
@@ -139,7 +154,10 @@ Nothing to see here.
139
154
  ### Added
140
155
  - Initial support for selects, orders, wheres, and joins.
141
156
 
142
- [Unreleased]: https://github.com/rzane/baby_squeel/compare/v1.3.1...HEAD
157
+ [Unreleased]: https://github.com/rzane/baby_squeel/compare/v1.4.1...HEAD
158
+ [1.4.1]: https://github.com/rzane/baby_squeel/compare/v1.4.0...v1.4.1
159
+ [1.4.0]: https://github.com/rzane/baby_squeel/compare/v1.4.0.beta1...v1.4.0
160
+ [1.4.0.beta1]: https://github.com/rzane/baby_squeel/compare/v1.3.1...v1.4.0.beta1
143
161
  [1.3.1]: https://github.com/rzane/baby_squeel/compare/v1.3.0...v1.3.1
144
162
  [1.3.0]: https://github.com/rzane/baby_squeel/compare/v1.2.1...v1.3.0
145
163
  [1.2.1]: https://github.com/rzane/baby_squeel/compare/v1.2.0...v1.2.1
data/ISSUE_TEMPLATE.md CHANGED
@@ -11,7 +11,7 @@ require 'minitest/autorun'
11
11
 
12
12
  gemfile true do
13
13
  source 'https://rubygems.org'
14
- gem 'activerecord', '~> 5.0.0' # which Active Record version?
14
+ gem 'activerecord', '~> 5.2.0' # which Active Record version?
15
15
  gem 'sqlite3'
16
16
  gem 'baby_squeel', github: 'rzane/baby_squeel'
17
17
  end
data/README.md CHANGED
@@ -329,7 +329,7 @@ Check out the [migration guide](https://github.com/rzane/baby_squeel/wiki/Migrat
329
329
 
330
330
  ## Development
331
331
 
332
- 1. Pick an Active Record version to develop against, then export it: `export AR=4.2.6`.
332
+ 1. Pick an Active Record version to develop against, then export it: `export AR=5.2.0`.
333
333
  2. Run `bin/setup` to install dependencies.
334
334
  3. Run `rake` to run the specs.
335
335
 
data/baby_squeel.gemspec CHANGED
@@ -21,7 +21,6 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency 'activerecord', '~> 5.2'
23
23
  spec.add_dependency 'ransack', '~> 2.3'
24
- spec.add_dependency 'join_dependency', '~> 0.1.4'
25
24
 
26
25
  spec.add_development_dependency 'bundler', '~> 2'
27
26
  spec.add_development_dependency 'rake', '~> 13.0'
@@ -1,12 +1,10 @@
1
1
  require 'baby_squeel/calculation'
2
- require 'baby_squeel/pluck'
3
2
 
4
3
  module BabySqueel
5
4
  module ActiveRecord
6
5
  module Calculations
7
6
  def plucking(&block)
8
7
  nodes = Array.wrap(DSL.evaluate(self, &block))
9
- nodes = nodes.map { |node| Pluck.decorate(node) }
10
8
  pluck(*nodes)
11
9
  end
12
10
 
@@ -40,17 +38,6 @@ module BabySqueel
40
38
  super
41
39
  end
42
40
  end
43
-
44
- if ::ActiveRecord::VERSION::MAJOR < 5
45
- # @override
46
- def type_for(field)
47
- if field.kind_of? Calculation
48
- field
49
- else
50
- super
51
- end
52
- end
53
- end
54
41
  end
55
42
  end
56
43
  end
@@ -40,14 +40,8 @@ module BabySqueel
40
40
  # Active Record will call `group_by` on the `joins`. The
41
41
  # Injector has a custom `group_by` method that handles
42
42
  # BabySqueel::Join nodes.
43
- if ::ActiveRecord::VERSION::MAJOR >= 5 && ::ActiveRecord::VERSION::MINOR >= 2
44
- def build_joins(manager, joins, aliases)
45
- super manager, BabySqueel::JoinDependency::Injector.new(joins), aliases
46
- end
47
- else
48
- def build_joins(manager, joins)
49
- super manager, BabySqueel::JoinDependency::Injector.new(joins)
50
- end
43
+ def build_joins(manager, joins, aliases)
44
+ super manager, BabySqueel::JoinDependency::Injector.new(joins), aliases
51
45
  end
52
46
  end
53
47
  end
@@ -96,19 +96,13 @@ module BabySqueel
96
96
 
97
97
  private
98
98
 
99
- if ActiveRecord::VERSION::MAJOR >= 5
100
- def build_where_clause(other)
101
- if valid_where_clause?(other)
102
- relation = @parent._scope.all
103
- factory = relation.send(:where_clause_factory)
104
- factory.build({ _reflection.name => other }, [])
105
- else
106
- raise AssociationComparisonError.new(_reflection.name, other)
107
- end
108
- end
109
- else
110
- def build_where_clause(_)
111
- raise AssociationComparisonNotSupportedError.new(_reflection.name)
99
+ def build_where_clause(other)
100
+ if valid_where_clause?(other)
101
+ relation = @parent._scope.all
102
+ factory = relation.send(:where_clause_factory)
103
+ factory.build({ _reflection.name => other }, [])
104
+ else
105
+ raise AssociationComparisonError.new(_reflection.name, other)
112
106
  end
113
107
  end
114
108
 
@@ -6,12 +6,6 @@ module BabySqueel
6
6
  @node = node
7
7
  end
8
8
 
9
- # This is only used in 4.2. We're just pretending to be
10
- # a database column to fake the casting here.
11
- def type_cast_from_database(value)
12
- value
13
- end
14
-
15
9
  # In Active Record 5, we don't *need* this class to make
16
10
  # calculations work. They happily accept arel. However,
17
11
  # when grouping with a calculation, there's a really,
@@ -62,14 +62,4 @@ module BabySqueel
62
62
  super "You can't compare association '#{name}' to #{other}."
63
63
  end
64
64
  end
65
-
66
- class AssociationComparisonNotSupportedError < StandardError # :nodoc:
67
- MESSAGE =
68
- "Querying association '%{name}' with '==' and '!=' " \
69
- "is only supported for ActiveRecord >=5."
70
-
71
- def initialize(name)
72
- super format(MESSAGE, name: name)
73
- end
74
- end
75
65
  end
@@ -1,5 +1,3 @@
1
- require 'join_dependency'
2
-
3
1
  module BabySqueel
4
2
  module JoinDependency
5
3
  # This class allows BabySqueel to slip custom
@@ -26,9 +24,7 @@ module BabySqueel
26
24
  attr_reader :join_dependency
27
25
 
28
26
  def initialize(relation)
29
- @join_dependency = ::JoinDependency.from_relation(relation) do |join|
30
- :association_join if join.kind_of? BabySqueel::Join
31
- end
27
+ @join_dependency = build(relation, collect_joins(relation))
32
28
  end
33
29
 
34
30
  # Find the alias of a BabySqueel::Association, by passing
@@ -38,7 +34,7 @@ module BabySqueel
38
34
  # If we tell join_dependency to construct its tables, Active Record
39
35
  # handles building the correct aliases and attaching them to its
40
36
  # JoinDepenencies.
41
- if ::ActiveRecord::VERSION::STRING >= '5.2.3'
37
+ if at_least?("5.2.3")
42
38
  join_dependency.send(:construct_tables!, join_dependency.send(:join_root))
43
39
  end
44
40
 
@@ -48,17 +44,85 @@ module BabySqueel
48
44
 
49
45
  private
50
46
 
47
+ Associations = ::ActiveRecord::Associations
48
+
51
49
  def find_join_association(associations)
52
50
  current = join_dependency.send(:join_root)
53
51
 
54
52
  associations.each do |association|
55
53
  name = association._reflection.name
56
- current = current.children.find { |c| c.reflection.name == name }
54
+ current = current.children.find { |c| c.reflection.name == name && klass_equal?(association, c) }
57
55
  break if current.nil?
58
56
  end
59
57
 
60
58
  current
61
59
  end
60
+
61
+ # If association is not polymorphic return true.
62
+ # If association is polymorphic compare the association polymorphic class with the join association base_klass
63
+ def klass_equal?(assoc, join_association)
64
+ return true unless assoc._reflection.polymorphic?
65
+
66
+ assoc._polymorphic_klass == join_association.base_klass
67
+ end
68
+
69
+ def collect_joins(relation)
70
+ joins = []
71
+ joins += relation.joins_values
72
+ joins += relation.left_outer_joins_values if at_least?("5")
73
+
74
+ buckets = joins.group_by do |join|
75
+ case join
76
+ when String
77
+ :string_join
78
+ when Hash, Symbol, Array
79
+ :association_join
80
+ when Associations::JoinDependency
81
+ :stashed_join
82
+ when Arel::Nodes::Join
83
+ :join_node
84
+ when BabySqueel::Join
85
+ :association_join
86
+ else
87
+ raise("unknown class: %s" % join.class.name)
88
+ end
89
+ end
90
+ end
91
+
92
+ def build(relation, buckets)
93
+ buckets.default = []
94
+ association_joins = buckets[:association_join]
95
+ stashed_association_joins = buckets[:stashed_join]
96
+ join_nodes = buckets[:join_node].uniq
97
+ string_joins = buckets[:string_join].map(&:strip).uniq
98
+
99
+ joins = string_joins.map do |join|
100
+ relation.table.create_string_join(Arel.sql(join)) unless join.blank?
101
+ end.compact
102
+
103
+ join_list = join_nodes + joins
104
+
105
+ alias_tracker = Associations::AliasTracker.create(relation.klass.connection, relation.table.name, join_list)
106
+ if exactly?("5.2.0")
107
+ join_dependency = Associations::JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker)
108
+ else
109
+ join_dependency = Associations::JoinDependency.new(relation.klass, relation.table, association_joins)
110
+ join_dependency.instance_variable_set(:@alias_tracker, alias_tracker)
111
+ end
112
+ join_nodes.each do |join|
113
+ join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1
114
+ end
115
+
116
+ join_dependency
117
+ end
118
+
119
+ def exactly?(version)
120
+ ::ActiveRecord.gem_version == Gem::Version.new(version)
121
+ end
122
+
123
+ def at_least?(version)
124
+ ::ActiveRecord.gem_version >= Gem::Version.new(version)
125
+ end
62
126
  end
63
127
  end
64
128
  end
@@ -1,3 +1,3 @@
1
1
  module BabySqueel
2
- VERSION = '1.4.0.beta1'.freeze
2
+ VERSION = '1.4.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baby_squeel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0.beta1
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-21 00:00:00.000000000 Z
11
+ date: 2021-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.3'
41
- - !ruby/object:Gem::Dependency
42
- name: join_dependency
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 0.1.4
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 0.1.4
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -140,7 +126,6 @@ files:
140
126
  - lib/baby_squeel/nodes/node.rb
141
127
  - lib/baby_squeel/nodes/proxy.rb
142
128
  - lib/baby_squeel/operators.rb
143
- - lib/baby_squeel/pluck.rb
144
129
  - lib/baby_squeel/relation.rb
145
130
  - lib/baby_squeel/resolver.rb
146
131
  - lib/baby_squeel/table.rb
@@ -160,9 +145,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
145
  version: '0'
161
146
  required_rubygems_version: !ruby/object:Gem::Requirement
162
147
  requirements:
163
- - - ">"
148
+ - - ">="
164
149
  - !ruby/object:Gem::Version
165
- version: 1.3.1
150
+ version: '0'
166
151
  requirements: []
167
152
  rubygems_version: 3.0.3
168
153
  signing_key:
@@ -1,25 +0,0 @@
1
- module BabySqueel
2
- class Pluck # :nodoc:
3
- # In Active Record 4.2, #pluck chokes when you give it
4
- # Arel. It calls #to_s on whatever you pass in. So the
5
- # hacky solution is to wrap the node in a class that
6
- # returns the node when you call #to_s. Then, it works!
7
- #
8
- # In Active Record 5, #pluck accepts Arel, so we won't
9
- # bother to use this there.
10
-
11
- if ::ActiveRecord::VERSION::MAJOR >= 5
12
- def self.decorate(node); node; end
13
- else
14
- def self.decorate(node); new(node); end
15
- end
16
-
17
- def initialize(node)
18
- @node = node
19
- end
20
-
21
- def to_s
22
- @node
23
- end
24
- end
25
- end