baby_squeel 1.0.0 → 1.0.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
  SHA1:
3
- metadata.gz: 394545743835687d87f67feb11e76e787cebc34a
4
- data.tar.gz: 4a3f636a36f4e83a889e3948ea6acf35f235b402
3
+ metadata.gz: dccc30465ef9e1b82eed32a0e96c17a5a7fc4e45
4
+ data.tar.gz: 9a89635d8f91cdd04f6015141de2ab04dda6c224
5
5
  SHA512:
6
- metadata.gz: ba300fa29631298a8143a580ab34bb66298b022aa9e32d0204f4855e23a464090c3ba0032f1c63152bd3de90a46f12f2ed609234b735de41cce50cd841a512e6
7
- data.tar.gz: 6e565e66b5e8dd456f4a388f5b1e0b903ddcc2f1baaee86aed0ca0c295238a66d9a0d16f31cd6f9ce4e4679700354b2c98782284e418d2c981d7db88fab864fc
6
+ metadata.gz: 7c07d0fdbb62c82a2e24575d31c46c7f2e4f6acb87c637d57204c357e1507e682b89f5d996b69ea69580279d9e0afea0fc22c20d3d720bc945c78bccee6ea397
7
+ data.tar.gz: 82203055a8712beed7bef9651f45863d187ca5518dc0012e1d8ef29cb3c4266ceeeca5835f6fded0498c6130de01a44c47d24526ce5a7b9a0e35b65b9d637459
data/CHANGELOG.md CHANGED
@@ -1,4 +1,18 @@
1
- ## [1.0.0] - 2016-9-9
1
+ ## [Unreleased]
2
+
3
+ *Nothing yet.*
4
+
5
+ ## [1.0.1] - 2016-11-07
6
+ ### Added
7
+ - Add DSL#_ for wrapping expressions in Arel::Node::Grouping. Thanks to [@odedniv].
8
+
9
+ ### Fixed
10
+ - Use strings for attribute names like Rails does. Symbols were preventing things like `unscope` from working. Thanks to [@chewi].
11
+ - `where.has {}` will now accept `nil`.
12
+ - Arel::Nodes::Function did not previously include Arel::Math, so now you can do math operations on the result of SQL functions.
13
+ - Arel::Nodes::Binary did not previously include Arel::AliasPredication. Binary nodes can now be aliased using `as`.
14
+
15
+ ## [1.0.0] - 2016-09-09
2
16
  ### Added
3
17
  - Polyamorous. Unfortunately, this *does* monkey-patch Active Record internals, but there just isn't any other reliable way to generate outer joins. Baby Squeel, itself, will still keep monkey patching to an absolute minimum.
4
18
  - Within DSL blocks, you can use `exists` and `not_exists` with Active Record relations. For example: `Post.where.has { exists Post.where(title: 'Fun') }`.`
@@ -67,9 +81,14 @@
67
81
  ### Added
68
82
  - Initial support for selects, orders, wheres, and joins.
69
83
 
70
- [Unreleased]: https://github.com/rzane/baby_squeel/compare/v0.3.1...HEAD
84
+ [Unreleased]: https://github.com/rzane/baby_squeel/compare/v1.0.1...HEAD
85
+ [1.0.1]: https://github.com/rzane/baby_squeel/compare/v1.0.0...v1.0.1
86
+ [1.0.0]: https://github.com/rzane/baby_squeel/compare/v0.3.1...v1.0.0
71
87
  [0.3.1]: https://github.com/rzane/baby_squeel/compare/v0.3.0...v0.3.1
72
88
  [0.3.0]: https://github.com/rzane/baby_squeel/compare/v0.2.2...v0.3.0
73
89
  [0.2.2]: https://github.com/rzane/baby_squeel/compare/v0.2.1...v0.2.2
74
90
  [0.2.1]: https://github.com/rzane/baby_squeel/compare/v0.2.0...v0.2.1
75
91
  [0.2.0]: https://github.com/rzane/baby_squeel/compare/v0.1.0...v0.2.0
92
+
93
+ [@chewi]: https://github.com/chewi
94
+ [@odedniv]: https://github.com/odedniv
data/README.md CHANGED
@@ -246,7 +246,7 @@ Post.select('1 as one').ordering { sql('one').desc }
246
246
  Post.selecting { title.op('||', quoted('diddly')) }
247
247
 
248
248
  # Functions
249
- Post.select { func('coalesce', id, 1) }
249
+ Post.selecting { func('coalesce', id, 1) }
250
250
  ```
251
251
 
252
252
  ## Sifters
@@ -5,7 +5,8 @@ module BabySqueel
5
5
  module WhereChain
6
6
  # Constructs Arel for ActiveRecord::Base#where using the DSL.
7
7
  def has(&block)
8
- @scope.where! DSL.evaluate(@scope, &block)
8
+ arel = DSL.evaluate(@scope, &block)
9
+ @scope.where!(arel) unless arel.nil?
9
10
  @scope
10
11
  end
11
12
  end
@@ -24,6 +24,24 @@ module BabySqueel
24
24
  end
25
25
  end
26
26
 
27
+ # Create a Grouping node. This allows you to set balanced
28
+ # pairs of parentheses around your SQL.
29
+ #
30
+ # ==== Arguments
31
+ #
32
+ # * +expr+ - The expression to group.
33
+ #
34
+ # ==== Example
35
+ # Post.where.has{_([summary, description]).in(...)}
36
+ # #=> SELECT "posts".* FROM "posts" WHERE ("posts"."summary", "posts"."description") IN (...)"
37
+ # Post.select{[id, _(Comment.where.has{post_id == posts.id}.selecting{COUNT(id)})]}.as('comment_count')}
38
+ # #=> SELECT "posts"."id", (SELECT COUNT("comments"."id") FROM "comments" WHERE "comments.post_id" = "posts"."id") AS "comment_count" FROM "posts"
39
+ #
40
+ def _(expr)
41
+ expr = Arel.sql(expr.to_sql) if expr.is_a? ::ActiveRecord::Relation
42
+ Nodes.wrap Arel::Nodes::Grouping.new(expr)
43
+ end
44
+
27
45
  # Create a SQL function. See Arel::Nodes::NamedFunction.
28
46
  #
29
47
  # ==== Arguments
@@ -32,7 +50,7 @@ module BabySqueel
32
50
  # * +args+ - The arguments to be passed to the SQL function.
33
51
  #
34
52
  # ==== Example
35
- # Post.select { func('coalesce', id, 1) }
53
+ # Post.selecting { func('coalesce', id, 1) }
36
54
  # #=> SELECT COALESCE("posts"."id", 1) FROM "posts"
37
55
  #
38
56
  def func(name, *args)
@@ -2,6 +2,7 @@ require 'baby_squeel/nodes/node'
2
2
  require 'baby_squeel/nodes/attribute'
3
3
  require 'baby_squeel/nodes/function'
4
4
  require 'baby_squeel/nodes/grouping'
5
+ require 'baby_squeel/nodes/binary'
5
6
 
6
7
  module BabySqueel
7
8
  module Nodes
@@ -14,6 +15,8 @@ module BabySqueel
14
15
  Grouping.new(arel)
15
16
  when Arel::Nodes::Function
16
17
  Function.new(arel)
18
+ when Arel::Nodes::Binary
19
+ Binary.new(arel)
17
20
  when Arel::Nodes::Node, Arel::Nodes::SqlLiteral
18
21
  Node.new(arel)
19
22
  else
@@ -5,8 +5,8 @@ module BabySqueel
5
5
  class Attribute < Node
6
6
  def initialize(parent, name)
7
7
  @parent = parent
8
- @name = name
9
- super(parent._table[name])
8
+ @name = name.to_s
9
+ super(parent._table[@name])
10
10
  end
11
11
 
12
12
  def in(rel)
@@ -0,0 +1,12 @@
1
+ require 'baby_squeel/nodes/node'
2
+
3
+ module BabySqueel
4
+ module Nodes
5
+ class Binary < Node
6
+ def initialize(node)
7
+ super
8
+ node.extend Arel::AliasPredication
9
+ end
10
+ end
11
+ end
12
+ end
@@ -6,6 +6,7 @@ module BabySqueel
6
6
  class Function < Node
7
7
  def initialize(node)
8
8
  super
9
+ node.extend Arel::Math
9
10
  node.extend Arel::OrderPredications
10
11
  end
11
12
  end
@@ -1,3 +1,3 @@
1
1
  module BabySqueel
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.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.0.0
4
+ version: 1.0.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: 2016-09-09 00:00:00.000000000 Z
11
+ date: 2016-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -120,6 +120,7 @@ files:
120
120
  - lib/baby_squeel/join_expression.rb
121
121
  - lib/baby_squeel/nodes.rb
122
122
  - lib/baby_squeel/nodes/attribute.rb
123
+ - lib/baby_squeel/nodes/binary.rb
123
124
  - lib/baby_squeel/nodes/function.rb
124
125
  - lib/baby_squeel/nodes/grouping.rb
125
126
  - lib/baby_squeel/nodes/node.rb