activerecord-like 1.0.0 → 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: 7d5747b7416a46d6d651022ba3fa6924fd8db7a0
4
- data.tar.gz: a12c215ff69771f33fc772c81c52328d553afc3e
3
+ metadata.gz: fc1b9bf4a828daa0ebcaa68966c12654f34532ca
4
+ data.tar.gz: 60d7e06e58f04d72ddf0a431faef0bc006e7935e
5
5
  SHA512:
6
- metadata.gz: 28e1151781fee66992b46a476bf7390f56febd409616f31af9cf41a5c1c8e2354df120fb2927bc7a12983e04db7ff34947427cb1314c6ddfe36a38dd38c468d3
7
- data.tar.gz: bcf5e1ee0ae802a3bdbf6ea1444e720e5e2a74ad540b88cf9e538ebdf0e35586172269bc54a932d3e255ee0c11d431ace76d79b43a8d3b119ef6f5e3769f5a20
6
+ metadata.gz: 4ec3097f54125b876a19982ce32081ecc005413420374ae8cb19c447967cfee5c555f4079f58a25c0bad8241cabf6c434868adfdc7e767e83a74e7be87338a96
7
+ data.tar.gz: c25277c34a1387c51bfb9f33e836436533e636a41fb337959a8b4d70bb3857a3ff80ae5745c8e16de5a1781ee05761f66ebe888ecd5ca9b4176c09546cb28cde
data/.gitignore CHANGED
@@ -16,3 +16,6 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .rvmrc
19
+
20
+ # Ignore Appraisal related files
21
+ gemfiles/*.gemfile.lock
@@ -1,13 +1,23 @@
1
+ # Send builds to container-based infrastructure
2
+ # http://docs.travis-ci.com/user/workers/container-based-infrastructure/
3
+ sudo: false
1
4
  language: ruby
2
5
  cache: bundler
3
6
  rvm:
4
- - 2.1.9
5
- - 2.2.5
6
- - 2.3.1
7
+ - 2.2.6
8
+ - 2.3.3
9
+ - 2.4.0
10
+ - ruby-head
7
11
  env:
8
12
  - DB=pg
9
13
  - DB=sqlite3
10
14
  - DB=mysql
15
+ gemfile:
16
+ - gemfiles/rails_5_0.gemfile
11
17
  before_script:
12
18
  - psql -c 'create database activerecord_like_test' -U postgres
13
19
  - mysql -e 'create database activerecord_like_test'
20
+ matrix:
21
+ fast_finish: true
22
+ allow_failures:
23
+ - rvm: ruby-head
@@ -0,0 +1,3 @@
1
+ appraise "rails_5_0" do
2
+ gem "activerecord", "~> 5.0.0"
3
+ end
data/README.md CHANGED
@@ -7,9 +7,9 @@
7
7
 
8
8
  [activerecord-like on Github](https://github.com/ReneB/activerecord-like)
9
9
 
10
- An Active Record Plugin that allows chaining a more DSL-style 'like' or 'not-like' query to an ActiveRecord::Base#where. Requires Rails 4 beta or higher.
10
+ An Active Record Plugin that allows chaining a more DSL-style 'like' or 'not-like' query to an ActiveRecord::Base#where. Requires Rails 5 or higher.
11
11
 
12
- This plugin has been salvaged from the stellar work done by @amatsuda and @claudiob. Most of the code was previously in Active Record master, but was subsequently removed due to, amongst other, scope creep.
12
+ This plugin has been salvaged from the stellar work done by @amatsuda and @claudiob, and updated to ActiveRecord 5 by @PikachuEXE. Most of the code was previously in Active Record master, but was subsequently removed due to, amongst other, scope creep.
13
13
  Array parameter handling was added by @rzane - thanks!
14
14
 
15
15
  ## Installation
@@ -43,7 +43,7 @@ Post.where.not_like(title: ["%ruby%", "%rails%"])
43
43
  ```
44
44
 
45
45
  ## Dependencies
46
- Does not work with Arel 4.0.1 or lower or Arel 5.0.0. Do a `bundle update arel` to get the latest Arel that is compatible with your ActiveRecord version (4.0 for ActiveRecord 4.0, 5.0 for ActiveRecord 4.1).
46
+ ActiveRecord 5 or higher
47
47
 
48
48
  ## Contributing
49
49
 
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env rake
2
2
  require 'bundler/gem_tasks'
3
3
  require 'rake/testtask'
4
+ require 'appraisal'
4
5
 
5
6
  Rake::TestTask.new do |t|
6
7
  t.libs = ["test"]
@@ -10,4 +11,11 @@ Rake::TestTask.new do |t|
10
11
  t.ruby_opts = ['-w']
11
12
  end
12
13
 
13
- task :default => :test
14
+ if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
15
+ task :default do
16
+ sh "appraisal install && rake appraisal test"
17
+ end
18
+ else
19
+ task :default => [:test]
20
+ end
21
+
@@ -15,5 +15,11 @@ Gem::Specification.new do |gem|
15
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
16
  gem.require_paths = ["lib"]
17
17
 
18
- gem.add_dependency "activerecord", ">= 4.0.0"
18
+ gem.add_dependency "activerecord", ">= 5.0.0"
19
+
20
+ # Required for Travis build to pass
21
+ gem.add_development_dependency "pg"
22
+ gem.add_development_dependency "mysql2"
23
+
24
+ gem.add_development_dependency "appraisal", "~> 2.0"
19
25
  end
@@ -0,0 +1,13 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0.0"
6
+
7
+ group :test do
8
+ gem "rake"
9
+ gem "sqlite3"
10
+ gem "minitest", ">= 3"
11
+ end
12
+
13
+ gemspec :path => "../"
@@ -1,8 +1,8 @@
1
1
  require "active_record"
2
2
 
3
3
  module ActiveRecord
4
- module QueryMethods
5
- module Like
4
+ module Like
5
+ module WhereChainExtensions
6
6
  def like(opts, *rest)
7
7
  opts.each do |k,v|
8
8
  if v.is_a?(Array) && v.empty?
@@ -22,17 +22,30 @@ module ActiveRecord
22
22
  end
23
23
  end
24
24
 
25
- private
25
+ private
26
26
 
27
27
  def chain_node(node_type, opts, *rest, &block)
28
28
  @scope.tap do |s|
29
- s.where_values += s.send(:build_where, opts, *rest).map do |r|
30
- if r.right.is_a?(Array)
31
- nodes = r.right.map { |expr| node_type.new(r.left, expr) }
29
+ # Assuming `opts` to be `Hash`
30
+ opts.each_pair do |key, value|
31
+ # 1. Build a where clause to generate "predicates" & "binds"
32
+ # 2. Convert "predicates" into the one that matches `node_type` (like/not like)
33
+ # 3. Re-use binding values to create new where clause
34
+ equal_where_clause = s.send(:where_clause_factory).build({key => value}, rest)
35
+ equal_where_clause_predicate = equal_where_clause.send(:predicates).first
36
+
37
+ new_predicate = if equal_where_clause_predicate.right.is_a?(Array)
38
+ nodes = equal_where_clause_predicate.right.map do |expr|
39
+ node_type.new(equal_where_clause_predicate.left, expr)
40
+ end
32
41
  Arel::Nodes::Grouping.new block.call(nodes)
33
42
  else
34
- node_type.new(r.left, r.right)
43
+ node_type.new(equal_where_clause_predicate.left, equal_where_clause_predicate.right)
35
44
  end
45
+
46
+ new_where_clause = s.send(:where_clause_factory).build(new_predicate, equal_where_clause.binds)
47
+
48
+ s.where_clause += new_where_clause
36
49
  end
37
50
  end
38
51
  end
@@ -40,4 +53,8 @@ module ActiveRecord
40
53
  end
41
54
  end
42
55
 
43
- ActiveRecord::QueryMethods::WhereChain.send(:include, ActiveRecord::QueryMethods::Like)
56
+ ActiveSupport.on_load(:active_record) do
57
+ ActiveRecord.eager_load!
58
+
59
+ ActiveRecord::QueryMethods::WhereChain.send(:include, ::ActiveRecord::Like::WhereChainExtensions)
60
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Like
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0"
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ describe ActiveRecord::QueryMethods::WhereChain do
5
5
  it "creates an Arel Matches node in the relation" do
6
6
  relation = Post.where.like(title: '')
7
7
 
8
- relation.where_values.first.must_be_instance_of(Arel::Nodes::Matches)
8
+ relation.where_clause.send(:predicates).first.must_be_instance_of(Arel::Nodes::Matches)
9
9
  end
10
10
 
11
11
  describe "the Arel Node" do
@@ -13,11 +13,17 @@ describe ActiveRecord::QueryMethods::WhereChain do
13
13
  @attribute = "title"
14
14
  @value = '%value%'
15
15
 
16
- @relation_specifier = Post.where.like(@attribute => @value).where_values.first
16
+ relation = Post.where.like(@attribute => @value)
17
+ @first_predicate = relation.where_clause.send(:predicates).first
18
+ @first_bind = relation.where_clause.send(:binds).first
17
19
  end
18
20
 
19
21
  it "has the attribute as the left operand" do
20
- @relation_specifier.left.name.must_equal @attribute
22
+ @first_predicate.left.name.must_equal @attribute
23
+ end
24
+
25
+ it "has the value as the right operand" do
26
+ @first_bind.value.must_equal @value
21
27
  end
22
28
  end
23
29
  end
@@ -5,7 +5,7 @@ describe ActiveRecord::QueryMethods::WhereChain do
5
5
  it "creates an Arel DoesNotMatch node in the relation" do
6
6
  relation = Post.where.not_like(title: '')
7
7
 
8
- relation.where_values.first.must_be_instance_of(Arel::Nodes::DoesNotMatch)
8
+ relation.where_clause.send(:predicates).first.must_be_instance_of(Arel::Nodes::DoesNotMatch)
9
9
  end
10
10
 
11
11
  describe "the Arel Node" do
@@ -13,13 +13,18 @@ describe ActiveRecord::QueryMethods::WhereChain do
13
13
  @attribute = "title"
14
14
  @value = '%value%'
15
15
 
16
- @relation_specifier = Post.where.not_like(@attribute => @value).where_values.first
16
+ relation = Post.where.like(@attribute => @value)
17
+ @first_predicate = relation.where_clause.send(:predicates).first
18
+ @first_bind = relation.where_clause.send(:binds).first
17
19
  end
18
20
 
19
21
  it "has the attribute as the left operand" do
20
- @relation_specifier.left.name.must_equal @attribute
22
+ @first_predicate.left.name.must_equal @attribute
23
+ end
24
+
25
+ it "has the value as the right operand" do
26
+ @first_bind.value.must_equal @value
21
27
  end
22
28
  end
23
29
  end
24
30
  end
25
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-like
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - René van den Berg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-10 00:00:00.000000000 Z
11
+ date: 2017-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,56 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 5.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: appraisal
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
27
69
  description: An ActiveRecord plugin providing a higher-level abstraction for SQL 'LIKE'
28
70
  queries
29
71
  email:
@@ -34,11 +76,13 @@ extra_rdoc_files: []
34
76
  files:
35
77
  - ".gitignore"
36
78
  - ".travis.yml"
79
+ - Appraisals
37
80
  - Gemfile
38
81
  - LICENSE.txt
39
82
  - README.md
40
83
  - Rakefile
41
84
  - activerecord-like.gemspec
85
+ - gemfiles/rails_5_0.gemfile
42
86
  - lib/active_record/like.rb
43
87
  - lib/active_record/like/version.rb
44
88
  - lib/activerecord-like.rb
@@ -66,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
110
  version: '0'
67
111
  requirements: []
68
112
  rubyforge_project:
69
- rubygems_version: 2.4.6
113
+ rubygems_version: 2.5.1
70
114
  signing_key:
71
115
  specification_version: 4
72
116
  summary: 'ActiveRecord::Like provides ActiveRecord::Base with where.like(attribute: