baby_squeel 1.1.5 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '01931ce5553f1202e28f1698e03ac5edf72a0166'
4
- data.tar.gz: 6c3a49f9b7a5cb8c9d72f280d05cf0e165ddf3fc
3
+ metadata.gz: 49777d549c657b766ebef88cb758b6883cbf4e2e
4
+ data.tar.gz: 5d4f4e51737e5b252ff0783303b7081cfd8b5d37
5
5
  SHA512:
6
- metadata.gz: 335dd7d2f9e2f8f3d263339441069e68eca7a632b7296f4ed523f91be621ae727e8e63a8725c8f8b216db43a7a7d8b0f70b2fe407a861104885ae032d7ba88b5
7
- data.tar.gz: d25f185e663890ecf277045612eb71206de192d4e79883046a2deaa981378987e872a9516cc654e910ffe5cfb45113bccf3d2880fde90e6129b8e784a9fa14f1
6
+ metadata.gz: 4bf0eea8486a351b13221d37040202906e83b92da78570e5ddae25944ddc82e4ae31acaa9302f3ecb4948677a99966f6a0b297049045f40dc195cd5b9a7a8699
7
+ data.tar.gz: 4d2a82db29d4613dc73a2079bd773bc1e75cf570f51375ab554e00a3c2ca28bd6f40d0c12402843e83986c6ca065de6ce3c0854c0958e36ebd3e741b99509143
@@ -2,6 +2,15 @@
2
2
 
3
3
  Nothing to see here.
4
4
 
5
+ ## [1.2.0] - 2017-10-20
6
+ ### Added
7
+ - `reordering`, which is just a BabySqueel version of Active Record's `reorder`.
8
+ - `on` expressions can now be given a block that will yield the current node (#77).
9
+
10
+ ## [1.1.5] - 2017-05-26
11
+ ### Fixed
12
+ - Returning an empty hash from a `where.has {}` block would generate invalid SQL (#69).
13
+
5
14
  ## [1.1.4] - 2017-04-13
6
15
  ### Fixed
7
16
  - Nodes::Attribute#in and #not_in generate valid SQL when given ActiveRecord::NullRelations.
@@ -118,8 +127,10 @@ Nothing to see here.
118
127
  ### Added
119
128
  - Initial support for selects, orders, wheres, and joins.
120
129
 
121
- [Unreleased]: https://github.com/rzane/baby_squeel/compare/v1.1.4...HEAD
122
- [1.1.3]: https://github.com/rzane/baby_squeel/compare/v1.1.3...v1.1.4
130
+ [Unreleased]: https://github.com/rzane/baby_squeel/compare/v1.2.0...HEAD
131
+ [1.2.0]: https://github.com/rzane/baby_squeel/compare/v1.1.5...v1.2.0
132
+ [1.1.5]: https://github.com/rzane/baby_squeel/compare/v1.1.4...v1.1.5
133
+ [1.1.4]: https://github.com/rzane/baby_squeel/compare/v1.1.3...v1.1.4
123
134
  [1.1.3]: https://github.com/rzane/baby_squeel/compare/v1.1.2...v1.1.3
124
135
  [1.1.2]: https://github.com/rzane/baby_squeel/compare/v1.1.1...v1.1.2
125
136
  [1.1.1]: https://github.com/rzane/baby_squeel/compare/v1.1.0...v1.1.1
data/README.md CHANGED
@@ -172,9 +172,13 @@ Post.joining { author.outer.posts }
172
172
  # LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
173
173
  # INNER JOIN "posts" "posts_authors" ON "posts_authors"."author_id" = "authors"."id"
174
174
 
175
- Post.joining { author.alias('a').on((author.id == author_id) | (author.name == title)) }
175
+ Post.joining { author.on((author.id == author_id) | (author.name == title)) }
176
176
  # SELECT "posts".* FROM "posts"
177
- # INNER JOIN "authors" "a" ON ("authors"."id" = "posts"."author_id" OR "authors"."name" = "posts"."title")
177
+ # INNER JOIN "authors" ON ("authors"."id" = "posts"."author_id" OR "authors"."name" = "posts"."title")
178
+
179
+ Post.joining { |post| post.author.as('a').on { (id == post.author_id) | (name == post.title) } }
180
+ # SELECT "posts".* FROM "posts"
181
+ # INNER JOIN "authors" "a" ON ("a"."id" = "posts"."author_id" OR "a"."name" = "posts"."title")
178
182
 
179
183
  Picture.joining { imageable.of(Post) }
180
184
  # SELECT "pictures".* FROM "pictures"
@@ -3,10 +3,19 @@ require 'baby_squeel/dsl'
3
3
  module BabySqueel
4
4
  module ActiveRecord
5
5
  module Base
6
- delegate :joining, :joining!, :selecting, :ordering,
7
- :grouping, :when_having, :plucking,
8
- :averaging, :counting, :maximizing,
9
- :minimizing, :summing, to: :all
6
+ delegate :joining,
7
+ :selecting,
8
+ :ordering,
9
+ :reordering,
10
+ :grouping,
11
+ :when_having,
12
+ :plucking,
13
+ :averaging,
14
+ :counting,
15
+ :maximizing,
16
+ :minimizing,
17
+ :summing,
18
+ to: :all
10
19
 
11
20
  # Define a sifter that can be used within DSL blocks.
12
21
  #
@@ -4,27 +4,32 @@ require 'baby_squeel/join_dependency/injector'
4
4
  module BabySqueel
5
5
  module ActiveRecord
6
6
  module QueryMethods
7
- # Constructs Arel for ActiveRecord::Base#joins using the DSL.
7
+ # Constructs Arel for ActiveRecord::QueryMethods#joins using the DSL.
8
8
  def joining(&block)
9
9
  joins DSL.evaluate(self, &block)
10
10
  end
11
11
 
12
- # Constructs Arel for ActiveRecord::Base#select using the DSL.
12
+ # Constructs Arel for ActiveRecord::QueryMethods#select using the DSL.
13
13
  def selecting(&block)
14
14
  select DSL.evaluate(self, &block)
15
15
  end
16
16
 
17
- # Constructs Arel for ActiveRecord::Base#order using the DSL.
17
+ # Constructs Arel for ActiveRecord::QueryMethods#order using the DSL.
18
18
  def ordering(&block)
19
19
  order DSL.evaluate(self, &block)
20
20
  end
21
21
 
22
- # Constructs Arel for ActiveRecord::Base#group using the DSL.
22
+ # Constructs Arel for ActiveRecord::QueryMethods#reorder using the DSL.
23
+ def reordering(&block)
24
+ reorder DSL.evaluate(self, &block)
25
+ end
26
+
27
+ # Constructs Arel for ActiveRecord::QueryMethods#group using the DSL.
23
28
  def grouping(&block)
24
29
  group DSL.evaluate(self, &block)
25
30
  end
26
31
 
27
- # Constructs Arel for ActiveRecord::Base#having using the DSL.
32
+ # Constructs Arel for ActiveRecord::QueryMethods#having using the DSL.
28
33
  def when_having(&block)
29
34
  having DSL.evaluate(self, &block)
30
35
  end
@@ -85,7 +85,7 @@ module BabySqueel
85
85
  def _arel(associations = [])
86
86
  if _on
87
87
  super
88
- elsif _table.is_a? Arel::Nodes::TableAlias
88
+ elsif alias?
89
89
  raise AssociationAliasingError.new(_reflection.name, _table.right)
90
90
  elsif _reflection.polymorphic? && _polymorphic_klass.nil?
91
91
  raise PolymorphicNotSpecifiedError.new(_reflection.name)
@@ -8,6 +8,46 @@ module BabySqueel
8
8
  ::ActiveRecord::Relation.prepend QueryMethods
9
9
  end
10
10
 
11
+ class KeyPath
12
+ def self.evaluate(&block)
13
+ if block.arity.zero?
14
+ unwrap new.instance_eval(&block)
15
+ else
16
+ unwrap yield(new)
17
+ end
18
+ end
19
+
20
+ def self.unwrap(path)
21
+ if path.kind_of? self
22
+ path.value
23
+ elsif path.respond_to? :map
24
+ path.map(&method(:unwrap))
25
+ else
26
+ []
27
+ end
28
+ end
29
+
30
+ def initialize(*path)
31
+ @path = path
32
+ end
33
+
34
+ def value
35
+ @path.reverse.reduce({}) do |acc, name|
36
+ { name => acc }
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def respond_to_missing?(*)
43
+ true
44
+ end
45
+
46
+ def method_missing(name, *)
47
+ self.class.new(*@path, name)
48
+ end
49
+ end
50
+
11
51
  module DSL
12
52
  # An alias for BabySqueel::DSL#sql
13
53
  def `(str)
@@ -36,6 +76,33 @@ module BabySqueel
36
76
  end
37
77
  end
38
78
 
79
+ # Overrides ActiveRecord::QueryMethods#includes
80
+ def includes(*args, &block)
81
+ if block_given? && args.empty?
82
+ super KeyPath.evaluate(&block)
83
+ else
84
+ super
85
+ end
86
+ end
87
+
88
+ # Overrides ActiveRecord::QueryMethods#eager_load
89
+ def eager_load(*args, &block)
90
+ if block_given? && args.empty?
91
+ super KeyPath.evaluate(&block)
92
+ else
93
+ super
94
+ end
95
+ end
96
+
97
+ # Overrides ActiveRecord::QueryMethods#preload
98
+ def preload(*args, &block)
99
+ if block_given? && args.empty?
100
+ super KeyPath.evaluate(&block)
101
+ else
102
+ super
103
+ end
104
+ end
105
+
39
106
  # Heads up, Array#select conflicts with
40
107
  # ActiveRecord::QueryMethods#select. So, if arity
41
108
  # is given to the block, we'll use Array#select.
@@ -61,6 +128,14 @@ module BabySqueel
61
128
  end
62
129
  end
63
130
 
131
+ def reorder(*args, &block)
132
+ if block_given? && args.empty?
133
+ reordering(&block)
134
+ else
135
+ super
136
+ end
137
+ end
138
+
64
139
  # Overrides ActiveRecord::QueryMethods#group
65
140
  def group(*args, &block)
66
141
  if block_given? && args.empty?
@@ -85,16 +85,6 @@ module BabySqueel
85
85
  sql _scope.connection.quote(value)
86
86
  end
87
87
 
88
- # Evaluates a DSL block. If arity is given, this method
89
- # `yield` itself, rather than `instance_eval`.
90
- def evaluate(&block)
91
- if block.arity.zero?
92
- instance_eval(&block)
93
- else
94
- yield(self)
95
- end
96
- end
97
-
98
88
  private
99
89
 
100
90
  def resolver
@@ -26,7 +26,7 @@ module BabySqueel
26
26
  end
27
27
 
28
28
  def _arel
29
- if @parent.kind_of? BabySqueel::Association
29
+ if @parent.kind_of?(BabySqueel::Association) && !@parent.alias?
30
30
  @parent.find_alias[@name]
31
31
  else
32
32
  super
@@ -7,6 +7,11 @@ module BabySqueel
7
7
  # In the future, these proxy classes should be more specific and only
8
8
  # include necessary/applicable modules.
9
9
  class Node < Proxy
10
+ def initialize(node)
11
+ super
12
+ node.extend Arel::Math
13
+ end
14
+
10
15
  extend Operators::ArelAliasing
11
16
  include Operators::Comparison
12
17
  include Operators::Equality
@@ -20,6 +20,10 @@ module BabySqueel
20
20
  @_join ||= Arel::Nodes::InnerJoin
21
21
  end
22
22
 
23
+ def as(alias_name)
24
+ self.alias(alias_name)
25
+ end
26
+
23
27
  # Alias a table. This is only possible when joining
24
28
  # an association explicitly.
25
29
  def alias(alias_name)
@@ -31,6 +35,10 @@ module BabySqueel
31
35
  self
32
36
  end
33
37
 
38
+ def alias?
39
+ _table.kind_of? Arel::Nodes::TableAlias
40
+ end
41
+
34
42
  # Instruct the table to be joined with a LEFT OUTER JOIN.
35
43
  def outer
36
44
  clone.outer!
@@ -52,15 +60,25 @@ module BabySqueel
52
60
  end
53
61
 
54
62
  # Specify an explicit join.
55
- def on(node)
56
- clone.on! node
63
+ def on(node = nil, &block)
64
+ clone.on!(node, &block)
57
65
  end
58
66
 
59
- def on!(node) # :nodoc:
60
- self._on = node
67
+ def on!(node = nil, &block) # :nodoc:
68
+ self._on = node || evaluate(&block)
61
69
  self
62
70
  end
63
71
 
72
+ # Evaluates a DSL block. If arity is given, this method
73
+ # `yield` itself, rather than `instance_eval`.
74
+ def evaluate(&block)
75
+ if block.arity.zero?
76
+ instance_eval(&block)
77
+ else
78
+ yield(self)
79
+ end
80
+ end
81
+
64
82
  # When referencing a joined table, the tables that
65
83
  # attributes reference can change (due to aliasing).
66
84
  # This method allows BabySqueel::Nodes::Attribute
@@ -1,3 +1,3 @@
1
1
  module BabySqueel
2
- VERSION = '1.1.5'.freeze
2
+ VERSION = '1.2.0'.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.1.5
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-26 00:00:00.000000000 Z
11
+ date: 2017-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord