where_chain 0.3.1 → 0.5.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
  SHA256:
3
- metadata.gz: f908efafd89e45ee67d4051326d7998aee4dea9d008e6e95a2dc23064754f12f
4
- data.tar.gz: 368905be74dfd90a3eb62cf190b06a800e1764c235870364cf2c79eb243fde92
3
+ metadata.gz: d8cbeba2f22837e6e7df392c402c59a81c2dc7e516d231f5f8fd3695c51cbc0d
4
+ data.tar.gz: b017adeb34d58508c222987c5198fee4653881a5fce32f56d41de4e86b94abd8
5
5
  SHA512:
6
- metadata.gz: 2c71031c54d4e19143165b9ea03f12b548dd1bf3850649a8937a3c376f36168559d359f93a1a250fc61a7ba0a8e22b85a69e8af34d69eaf3d29f01fb4aa991e2
7
- data.tar.gz: 65f15b0f747953b251b4f48c87f84d4eda3064e739d575ea4fa02de81093d05f1efa4b884340a466e020965c8fe8031fe0269a429dce46b7cff163318852729f
6
+ metadata.gz: b68533130e8d4561aa95dd3d117f52565924e07d74c8dd2d7bcc85276ede3aa6e866c1e86f5a10672754bbb838fc7932709cb837efec95baa420e7782c982479
7
+ data.tar.gz: 5e58d5ab2d1810933c5f326d6462b80e2712c0da947393bd2c0265db99ea23ad1f246c36fa94db7ad8397a1dc22e4927caaacd8b7943a5c5a3254a8e35310f09
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Gem Version](https://badge.fury.io/rb/where_chain.svg)](https://badge.fury.io/rb/where_chain) [![TravisCI](https://travis-ci.com/marcinruszkiewicz/where_chain.svg?branch=master)](https://travis-ci.com/marcinruszkiewicz/where_chain)
1
+ [![Gem Version](https://badge.fury.io/rb/where_chain.svg)](https://badge.fury.io/rb/where_chain) ![TravisCI](https://github.com/marcinruszkiewicz/where_chain/actions/workflows/rspec.yml/badge.svg)
2
2
 
3
3
  # WhereChain
4
4
 
@@ -6,11 +6,11 @@ In Rails, we usually use Active Record, which allows us to escape from writing S
6
6
 
7
7
  In the older versions, you also had to write `Post.where('name IS NOT null')` to do a negation. Rails 4.0 added a class called `WhereChain` that added some [new possibilities](https://github.com/rails/rails/commit/de75af7acc5c05c708443de40e78965925165217), one of which was a `not` method. The proper way to write became `Post.where.not(name: nil)` instead.
8
8
 
9
- Within the same commit there were also two new methods that [didn't survive to the release of Rails 4.0](https://github.com/rails/rails/commit/8d02afeaee8993bd0fde69687fdd9bf30921e805) - `.like` and `.not_like`. As you can read in this commit discussion, there has been work made to bring them back, like the [activerecord-like](https://github.com/ReneB/activerecord-like) gem or [Squeel](https://github.com/activerecord-hackery/squeel), but these has their own problems - activerecord-like only adds `.like` and `.not_like` back and the latest version is locked to Active Record 5; and Squeel provides a whole new query DSL, which not everyone will like. There was actually a pull request adding `.gt` and other [inequality methods](https://github.com/rails/rails/pull/8453), which was closed even faster than the first one.
9
+ Within the same commit there were also two new methods that [didn't survive to the release of Rails 4.0](https://github.com/rails/rails/commit/8d02afeaee8993bd0fde69687fdd9bf30921e805) - `.like` and `.not_like`. As you can read in this commit discussion, there has been work made to bring them back, like the [activerecord-like](https://github.com/ReneB/activerecord-like) gem or [Squeel](https://github.com/activerecord-hackery/squeel), but these have their own problems - activerecord-like only adds `.like` and `.not_like` back and the latest version is locked to Active Record 5; and Squeel provides a whole new query DSL, which not everyone will like. There was actually a pull request adding `.gt` and other [inequality methods](https://github.com/rails/rails/pull/8453), which was closed even faster than the first one.
10
10
 
11
11
  This gem brings these two methods back and extends WhereChain with additional methods: `.gt`, `.gte`, `.lt` and `.lte`, so that by using it you can replace the SQL strings like `Post.where('comments > 5')` with `Post.where.gt(comments: 5)`.
12
12
 
13
- WhereChain depends on the Active Record gem in a version higher than 4.2, due to problems with Ruby versions lesser than 2.4. Rails 4.2 is already the version that's being maintained, so you probably should not use an earlier one anyway. The gem is tested on the latest Ruby and all current Rails versions - 4.2, 5.0, 5.1, 5.2 and 6.0.
13
+ WhereChain depends on the Active Record gem in a version higher than 4.2. The gem is tested on the latest Ruby and all current Rails versions - 5.2, 6.0, 6.1, and 7.0. If you have an older version, it will probably still work, because Active Record doesn't really change all that often, but Rails versions older than 5.2 depend on old and unsupported Ruby versions, so I'm not making any promises there.
14
14
 
15
15
  ## Usage
16
16
 
@@ -8,6 +8,7 @@ module ActiveRecord
8
8
 
9
9
  module QueryMethods
10
10
  class WhereChain
11
+ include ActiveModel::ForbiddenAttributesProtection
11
12
  include WhereChainSharedMethods
12
13
 
13
14
  # Returns a new relation expressing WHERE + NOT condition
@@ -30,9 +31,9 @@ module ActiveRecord
30
31
 
31
32
  opts = sanitize_forbidden_attributes(opts)
32
33
 
33
- where_clause = @scope.send(:where_clause_factory).build(opts, rest)
34
+ where_clause = where_clause_factory_build(opts, rest)
34
35
 
35
- @scope.references!(PredicateBuilder.references(opts)) if opts.is_a?(Hash)
36
+ @scope.references!(PredicateBuilder.references(opts.stringify_keys)) if opts.is_a?(Hash)
36
37
  @scope.where_clause += where_clause.invert
37
38
  @scope
38
39
  end
@@ -46,7 +47,7 @@ module ActiveRecord
46
47
  def prepare_where(node_type, infix, opts, rest)
47
48
  @scope.tap do |s|
48
49
  opts.each_pair do |key, value|
49
- equal_where_clause = s.send(:where_clause_factory).build({ key => value }, rest)
50
+ equal_where_clause = where_clause_factory_build({ key => value }, rest)
50
51
  equal_where_clause_predicate = equal_where_clause.send(:predicates).first
51
52
 
52
53
  new_predicate = arel_node(node_type, infix, equal_where_clause_predicate)
@@ -69,6 +70,16 @@ module ActiveRecord
69
70
  Relation::WhereClause.new([new_predicate])
70
71
  end
71
72
  end
73
+
74
+ # Active Record 6.1 removed the where_clause_factory.build method
75
+ # method was replaced by build_where_clause
76
+ def where_clause_factory_build(opts, rest = [])
77
+ if ActiveRecord.version.release >= Gem::Version.new('6.1.0')
78
+ @scope.send(:build_where_clause, opts, rest)
79
+ else
80
+ @scope.send(:where_clause_factory).build(opts, rest)
81
+ end
82
+ end
72
83
  end
73
84
  end
74
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WhereChain
4
- VERSION = '0.3.1'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/where_chain.rb CHANGED
@@ -5,6 +5,6 @@ require 'active_record/where_chain_shared_methods'
5
5
  # Active Record changed a lot in Rails 5 and 4 is still supported
6
6
  if ActiveRecord::VERSION::MAJOR == 4
7
7
  require 'active_record/where_chain_extensions_rails4'
8
- elsif ActiveRecord::VERSION::MAJOR == 5 || ActiveRecord::VERSION::MAJOR == 6
8
+ elsif ActiveRecord::VERSION::MAJOR >= 5
9
9
  require 'active_record/where_chain_extensions_rails5'
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_chain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Ruszkiewicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-25 00:00:00.000000000 Z
11
+ date: 2023-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -126,16 +126,16 @@ dependencies:
126
126
  name: sqlite3
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - "~>"
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: 1.3.0
131
+ version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - "~>"
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 1.3.0
138
+ version: '0'
139
139
  description: |
140
140
  This is a Rails plugin that extends Active Record with additional methods: .like, .unlike,
141
141
  .gt, .gte, .lt and .lte, so that you can replace the SQL strings like
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  - !ruby/object:Gem::Version
174
174
  version: '0'
175
175
  requirements: []
176
- rubygems_version: 3.0.3
176
+ rubygems_version: 3.1.6
177
177
  signing_key:
178
178
  specification_version: 4
179
179
  summary: 'WhereChain extensions - Model.where.lt(created_at: Date.today)'