rails_or 1.1.4 → 1.1.5

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: 33fb9ef79a69db4693d2590261c370ffac5acfc2
4
- data.tar.gz: 89fea4ac9e53427e18a19ed1e32c841f8e03db0b
3
+ metadata.gz: 0434d1cac1c04acacf74b933dff00a190162cdd9
4
+ data.tar.gz: 8af3108d409a281eb8cab6955eedc4ddb11c76de
5
5
  SHA512:
6
- metadata.gz: 5beeb32b071652b09fcfad2a597a2c3d77d9e27d92d35e1a2f11fb823ffd172d0f78e83c0c9f3afd278b23e16f9b6fe4cad5bdb54ce9be98bef9a4d699c9fcc1
7
- data.tar.gz: f4818b6c585c1ea8a4339c6d8a11d155911e5d00b23cabfc3279aae7e70bab8c776cb07107de9240538be969f2022082ff3a811d5c6b7caa3e597dc877149139
6
+ metadata.gz: f422a1b8d235ddad25daf0beb82882569b4742bda3ab521b6f802d956d0dbdf889f7d1156882c3cf2b80113eb48bdaaa5e7ee33cf2299c293ac72c90112098d0
7
+ data.tar.gz: 5c5e512521f05cb1fdd817005adc14439b0caa413b41d82db300d9928eb0853c8cf066c10fa21dc088ea96a12cf32c1ca4cc511c765a70175f126d4d85a40331
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
  ### RailsOr [Unreleased]
3
3
 
4
4
  - No changes.
5
+
6
+ ### RailsOr [1.1.5] - (June 21, 2017)
7
+
8
+ - Fix bind values mismatch after merging common where values. [[#19](https://github.com/khiav223577/rails_or/pull/19)] @khiav223577
5
9
  <br><br>
6
10
 
7
11
  ### RailsOr [1.1.4] - (April 1, 2017)
@@ -46,7 +50,8 @@
46
50
  - Add #or method [[#1](https://github.com/khiav223577/rails_or/pull/1)] @khiav223577
47
51
  <br><br>
48
52
 
49
- [Unreleased]: https://github.com/khiav223577/rails_or/compare/v1.1.4...HEAD
53
+ [Unreleased]: https://github.com/khiav223577/rails_or/compare/v1.1.5...HEAD
54
+ [1.1.5]: https://github.com/khiav223577/rails_or/compare/v1.1.4...v1.1.5
50
55
  [1.1.4]: https://github.com/khiav223577/rails_or/compare/v1.1.3...v1.1.4
51
56
  [1.1.3]: https://github.com/khiav223577/rails_or/compare/v1.1.2...v1.1.3
52
57
  [1.1.2]: https://github.com/khiav223577/rails_or/compare/v1.1.1...v1.1.2
data/README.md CHANGED
@@ -63,7 +63,24 @@ Person.where(name: 'Pearl').or('age = ?', 24)
63
63
  Person.where(name: 'Pearl').or('age = 24')
64
64
  ```
65
65
 
66
+ ### Other convenient methods
66
67
 
68
+ #### or_not
69
+ (Only supports in Rails 4+)
70
+ ```rb
71
+ Company.where.not(logo_image1: nil)
72
+ .or_not(logo_image2: nil)
73
+ .or_not(logo_image3: nil)
74
+ .or_not(logo_image4: nil)
75
+ ```
76
+
77
+ #### or_having
78
+
79
+ ```rb
80
+ Order.group('user_id')
81
+ .having('SUM(price) > 1000')
82
+ .or_having('COUNT(*) > 10')
83
+ ```
67
84
 
68
85
  ## Development
69
86
 
data/lib/rails_or.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "rails_or/version"
2
+ require "rails_or/where_binding_mixs"
2
3
  require 'active_record'
3
4
 
4
5
  class ActiveRecord::Relation
@@ -12,17 +13,24 @@ class ActiveRecord::Relation
12
13
  def or(*other)
13
14
  other = rails_or_parse_parameter(*other)
14
15
  combining = group_values.any? ? :having : :where
15
- left_values = send("#{combining}_values")
16
- right_values = other.send("#{combining}_values")
17
- common = left_values & right_values
18
- mine = left_values - common
19
- theirs = right_values - common
20
- if mine.any? && theirs.any?
21
- common << Arel::Nodes::Or.new(rails_or_values_to_arel(mine), rails_or_values_to_arel(theirs))
16
+ left = RailsOr::WhereBindingMixs.new(self.send("#{combining}_values"), self.bind_values)
17
+ right = RailsOr::WhereBindingMixs.new(other.send("#{combining}_values"), other.bind_values)
18
+ common = left & right
19
+
20
+ left -= common
21
+ right -= common
22
+
23
+ if left.where_values.any? && right.where_values.any?
24
+ arel_or = Arel::Nodes::Or.new(
25
+ rails_or_values_to_arel(left.where_values),
26
+ rails_or_values_to_arel(right.where_values),
27
+ )
28
+ common += RailsOr::WhereBindingMixs.new([arel_or], left.bind_values + right.bind_values)
22
29
  end
30
+
23
31
  relation = rails_or_get_current_scope
24
- relation.send("#{combining}_values=", common)
25
- relation.bind_values = self.bind_values + other.bind_values
32
+ relation.send("#{combining}_values=", common.where_values)
33
+ relation.bind_values = common.bind_values
26
34
  return relation
27
35
  end
28
36
  end
@@ -33,7 +41,9 @@ class ActiveRecord::Relation
33
41
  def or_having(*args)
34
42
  self.or(klass.having(*args))
35
43
  end
44
+
36
45
  private
46
+
37
47
  def rails_or_values_to_arel(values)
38
48
  values.map!{|x| rails_or_wrap_arel(x) }
39
49
  return (values.size > 1 ? Arel::Nodes::And.new(values) : values)
@@ -1,3 +1,3 @@
1
1
  module RailsOr
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
@@ -0,0 +1,38 @@
1
+ class RailsOr::WhereBindingMixs
2
+ attr_reader :where_values
3
+ attr_reader :bind_values
4
+
5
+ def initialize(where_values, bind_values)
6
+ @where_values = where_values
7
+ @bind_values = bind_values
8
+ end
9
+
10
+ def +(other)
11
+ self.class.new(@where_values + other.where_values, @bind_values + other.bind_values)
12
+ end
13
+
14
+ def -(other)
15
+ self.select{|node| !other.where_values.include?(node) }
16
+ end
17
+
18
+ def &(other)
19
+ common_where_values = @where_values & other.where_values
20
+ return self.select{|node| common_where_values.include?(node) }
21
+ end
22
+
23
+ def select
24
+ binds_index = 0
25
+ new_bind_values = []
26
+ new_where_values = @where_values.select do |node|
27
+ flag = yield(node)
28
+ if not node.is_a?(String)
29
+ binds_contains = node.grep(Arel::Nodes::BindParam).size
30
+ pre_binds_index = binds_index
31
+ binds_index += binds_contains
32
+ (pre_binds_index...binds_index).each{|i| new_bind_values << @bind_values[i] } if flag
33
+ end
34
+ next flag
35
+ end
36
+ return self.class.new(new_where_values, new_bind_values)
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_or
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-02 00:00:00.000000000 Z
11
+ date: 2017-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,7 @@ files:
101
101
  - gemfiles/5.0.gemfile
102
102
  - lib/rails_or.rb
103
103
  - lib/rails_or/version.rb
104
+ - lib/rails_or/where_binding_mixs.rb
104
105
  - rails_or.gemspec
105
106
  homepage: https://github.com/khiav223577/rails_or
106
107
  licenses: