rails_or 1.1.6 → 1.1.7

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: 8f9c88ae5f5c4ed9ac8fb2cfc64dbe22be904da8
4
- data.tar.gz: 639ff10eb8da02ab73d0a5701b75c2fa5a438b08
3
+ metadata.gz: 06d5dd12cf0c23251f01c2b591bf8f313e3e99e8
4
+ data.tar.gz: 936f18b090e9f5b56bc483ce353aa347190d3956
5
5
  SHA512:
6
- metadata.gz: 22dabe94aee61e0c55067c1249b1e247069d0d63ea3273360148408ba6614fcf30cc7ea64ca38edca4d22e092461884a6fa3fe3b0f656543859b035a5a8cf75a
7
- data.tar.gz: a4ad1799eb864d973f8fa0bdc8581e2e71792104a53c155c5729fc6d66277dedb4355064ba63260076ce8887a3509cc17468ec46b690e7b89d457fd7dcb0067a
6
+ metadata.gz: f01e2a4d78a54f9d318088ca27d3c534ab149bebd53566df64b9cd84121f67ce994b8719f282766b029f0bc104919cb82405ccdd615968dc11059ea3021b776c
7
+ data.tar.gz: 4420743158b6612111c00af6dd03e98fbb129aa0f20d70fe7b4cbc5ff30366de41101448b56de049cf8b512c0282b3ff93711cad0fd3aa1e99147856bd86402a
data/CHANGELOG.md CHANGED
@@ -3,6 +3,15 @@
3
3
 
4
4
  - No changes.
5
5
 
6
+ ### RailsOr [1.1.7] - (September 12, 2017)
7
+
8
+ - Better Rails 5 Support. [[#21](https://github.com/khiav223577/rails_or/pull/21)] @khiav223577
9
+
10
+ ### RailsOr [1.1.6] - (June 30, 2017)
11
+
12
+ - Supports `or` with `.none`. [[#20](https://github.com/khiav223577/rails_or/pull/20)] @khiav223577
13
+ <br><br>
14
+
6
15
  ### RailsOr [1.1.5] - (June 21, 2017)
7
16
 
8
17
  - Fix bind values mismatch after merging common where values. [[#19](https://github.com/khiav223577/rails_or/pull/19)] @khiav223577
@@ -50,7 +59,9 @@
50
59
  - Add #or method [[#1](https://github.com/khiav223577/rails_or/pull/1)] @khiav223577
51
60
  <br><br>
52
61
 
53
- [Unreleased]: https://github.com/khiav223577/rails_or/compare/v1.1.5...HEAD
62
+ [Unreleased]: https://github.com/khiav223577/rails_or/compare/v1.1.7...HEAD
63
+ [1.1.7]: https://github.com/khiav223577/rails_or/compare/v1.1.6...v1.1.7
64
+ [1.1.6]: https://github.com/khiav223577/rails_or/compare/v1.1.5...v1.1.6
54
65
  [1.1.5]: https://github.com/khiav223577/rails_or/compare/v1.1.4...v1.1.5
55
66
  [1.1.4]: https://github.com/khiav223577/rails_or/compare/v1.1.3...v1.1.4
56
67
  [1.1.3]: https://github.com/khiav223577/rails_or/compare/v1.1.2...v1.1.3
data/lib/rails_or.rb CHANGED
@@ -4,10 +4,13 @@ require 'active_record'
4
4
 
5
5
  class ActiveRecord::Relation
6
6
  IS_RAILS3_FLAG = Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
7
+ IS_RAILS5_FLAG = Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.0.0')
7
8
  if method_defined?(:or)
8
- alias rails5_or or
9
- def or(*other)
10
- return rails5_or(rails_or_parse_parameter(*other))
9
+ if not method_defined?(:rails5_or)
10
+ alias_method :rails5_or, :or
11
+ def or(*other)
12
+ return rails5_or(rails_or_parse_parameter(*other))
13
+ end
11
14
  end
12
15
  else
13
16
  def or(*other)
@@ -38,40 +41,61 @@ class ActiveRecord::Relation
38
41
  return relation
39
42
  end
40
43
  end
41
- def or_not(*args)
42
- raise 'This method is not support in Rails 3' if IS_RAILS3_FLAG
43
- return self.or(klass.where.not(*args))
44
+
45
+ def or_not(*args) # Works in Rails 4+
46
+ self.or(klass.where.not(*args))
44
47
  end
45
- def or_having(*args)
46
- self.or(klass.having(*args))
48
+
49
+ def or_having(hash)
50
+ self.or(rails_or_spwan_relation(:having, hash))
47
51
  end
48
52
 
49
- private
53
+ private
50
54
 
51
55
  def rails_or_values_to_arel(values)
52
56
  values.map!{|x| rails_or_wrap_arel(x) }
53
57
  return (values.size > 1 ? Arel::Nodes::And.new(values) : values)
54
58
  end
59
+
55
60
  def rails_or_wrap_arel(node)
56
61
  return node if Arel::Nodes::Equality === node
57
62
  return Arel::Nodes::Grouping.new(String === node ? Arel.sql(node) : node)
58
63
  end
64
+
59
65
  def rails_or_parse_parameter(*other)
60
66
  other = other.first if other.size == 1
61
67
  case other
62
- when Hash ; klass.where(other)
63
- when Array ; klass.where(other)
64
- when String ; klass.where(other)
68
+ when Hash ; rails_or_spwan_relation(:where, other)
69
+ when Array ; rails_or_spwan_relation(:where, other)
70
+ when String ; rails_or_spwan_relation(:where, other)
65
71
  else ; other
66
72
  end
67
73
  end
74
+
75
+ def rails_or_copy_values_to(relation) # For Rails 5
76
+ relation.joins_values = self.joins_values
77
+ relation.limit_value = self.limit_value
78
+ relation.group_values = self.group_values
79
+ relation.distinct_value = self.distinct_value
80
+ relation.order_values = self.order_values
81
+ relation.offset_value = self.offset_value
82
+ relation.references_values = self.references_values
83
+ end
84
+
85
+ def rails_or_spwan_relation(method, condition)
86
+ relation = klass.send(method, condition)
87
+ rails_or_copy_values_to(relation) if IS_RAILS5_FLAG
88
+ return relation
89
+ end
90
+
68
91
  def rails_or_get_current_scope
69
92
  return self.clone if IS_RAILS3_FLAG
70
- #ref: https://github.com/rails/rails/blob/17ef58db1776a795c9f9e31a1634db7bcdc3ecdf/activerecord/lib/active_record/scoping/named.rb#L26
71
- #return self.all # <- cannot use this because some gem changes this method's behavior
93
+ # ref: https://github.com/rails/rails/blob/17ef58db1776a795c9f9e31a1634db7bcdc3ecdf/activerecord/lib/active_record/scoping/named.rb#L26
94
+ # return self.all # <- cannot use this because some gem changes this method's behavior
72
95
  return (self.current_scope || self.default_scoped).clone
73
96
  end
74
97
  end
98
+
75
99
  class ActiveRecord::Base
76
100
  def self.or(*args)
77
101
  self.where('').or(*args)
@@ -1,3 +1,3 @@
1
1
  module RailsOr
2
- VERSION = "1.1.6"
2
+ VERSION = "1.1.7"
3
3
  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.6
4
+ version: 1.1.7
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-07-30 00:00:00.000000000 Z
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  requirements: []
125
125
  rubyforge_project:
126
- rubygems_version: 2.4.8
126
+ rubygems_version: 2.6.13
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: 'Support && Add syntax sugar to #or query method.'