rails_or 0.0.1 → 1.0.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: 0a3d6f309ba263303a6f91b8d48c7bf42993cf95
4
- data.tar.gz: 6aee8e9dba7f4759fe3051459f2a4304e6aa5ecb
3
+ metadata.gz: 56c4f804332574c8c5417b2350f7d7cf027e6b61
4
+ data.tar.gz: a1ddd0de0517011145406f4b93a0451c3b678669
5
5
  SHA512:
6
- metadata.gz: d05d9db91ffff004ddc800dab34591fdca483a5e70a5162c32e00a548d7af00ccf7189f1e268f62bf9f8aca4a38b8d4142a15ed4777b13cfa212667ea190a02f
7
- data.tar.gz: 81860c5c123712aae0e6cb2226888077bd4d2415bce0579912c8fe83b1d965b8b2cd3e54ec2a0ec738d74e66201db42141e1e96b45f518308ea32b56706f39ed
6
+ metadata.gz: f1cd03c1f89e4f47138edd99edcac8c9fde233a1801fb1ea38f2d1af513a8c60cc8d14acdb765fd52e094da4ba08f2a214e20e1d6045bac4085e9676666086bb
7
+ data.tar.gz: 38a7943e85c4a8fb585d26ba3b7477d407d84f7a6bdbc7fa2373e1f6e294e69179fa29b138907a9d42332447cb0dedd5e2b65054a3aa4103044b964818e9f3f4
data/README.md CHANGED
@@ -25,6 +25,21 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
+ ### Same as Rails 5's #or method
29
+ ```rb
30
+ Person.where(:name => 'Pearl').or(Person.where(:age => 24))
31
+ # is the same as
32
+ Person.where("name = ? OR age = ?", 'Pearl', 24)
33
+ ```
34
+
35
+ ### Support using Hash/Array/String as arguments
36
+ ```rb
37
+ Person.where(:name => 'Pearl').or(:age => 24)
38
+ Person.where(:name => 'Pearl').or(['age = ?', 24])
39
+ Person.where(:name => 'Pearl').or('age = ?', 24)
40
+ Person.where(:name => 'Pearl').or('age = 24')
41
+ ```
42
+
28
43
 
29
44
  ## Development
30
45
 
data/lib/rails_or.rb CHANGED
@@ -5,22 +5,20 @@ require 'active_record'
5
5
  class ActiveRecord::Relation
6
6
  if method_defined?(:or)
7
7
  alias rails5_or or
8
- def or(other)
9
- other = self.except(:where).where(other) if other.class == Hash
10
- return rails5_or(other)
8
+ def or(*other)
9
+ return rails5_or(parse_or_parameter(*other))
11
10
  end
12
11
  else
13
- def or(other)
14
- other = self.except(:where).where(other) if other.class == Hash
15
- combining = group_values.any? ? :having : :where
16
- left_values = send("#{combining}_values")
17
- right_values = other.send("#{combining}_values")
18
- common = left_values & right_values
19
- mine = left_values - common
20
- theirs = right_values - common
12
+ def or(*other)
13
+ combining = group_values.any? ? :having : :where
14
+ left_values = send("#{combining}_values")
15
+ right_values = parse_or_parameter(*other).send("#{combining}_values")
16
+ common = left_values & right_values
17
+ mine = left_values - common
18
+ theirs = right_values - common
21
19
  if mine.any? && theirs.any?
22
- mine = mine.map { |x| String === x ? Arel.sql(x) : x }
23
- theirs = theirs.map { |x| String === x ? Arel.sql(x) : x }
20
+ mine.map!{|x| String === x ? Arel.sql(x) : x }
21
+ theirs.map!{ |x| String === x ? Arel.sql(x) : x }
24
22
  mine = [Arel::Nodes::And.new(mine)] if mine.size > 1
25
23
  theirs = [Arel::Nodes::And.new(theirs)] if theirs.size > 1
26
24
  common << Arel::Nodes::Or.new(mine.first, theirs.first)
@@ -29,6 +27,16 @@ class ActiveRecord::Relation
29
27
  return self
30
28
  end
31
29
  end
30
+ private
31
+ def parse_or_parameter(*other)
32
+ other = other.first if other.size == 1
33
+ case other
34
+ when Hash ; self.except(:where).where(other.to_a.map{|s| s[0] = "#{s[0]} = ?" ; next s}.flatten) #TODO why hash is not working?
35
+ when Array ; self.except(:where).where(other)
36
+ when String ; self.except(:where).where(other)
37
+ else ; other
38
+ end
39
+ end
32
40
  end
33
41
  class ActiveRecord::Base
34
42
  def self.or(*args)
@@ -1,3 +1,3 @@
1
1
  module RailsOr
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
data/rails_or.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["mrtmrt15xn@yahoo.com.tw"]
11
11
 
12
12
  spec.summary = %q{Support #or query method in Rails 3, 4, 5}
13
- spec.description = %q{#or query is support only in new comming Rails 5. We want to support it in Rails 3 and 4.}
13
+ spec.description = %q{#or query is support only in new-coming Rails 5. This gem support it in Rails 3 and 4, too.}
14
14
  spec.homepage = "https://github.com/khiav223577/rails_or"
15
15
  spec.license = "MIT"
16
16
 
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: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-28 00:00:00.000000000 Z
11
+ date: 2016-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,8 +80,8 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3'
83
- description: "#or query is support only in new comming Rails 5. We want to support
84
- it in Rails 3 and 4."
83
+ description: "#or query is support only in new-coming Rails 5. This gem support it
84
+ in Rails 3 and 4, too."
85
85
  email:
86
86
  - mrtmrt15xn@yahoo.com.tw
87
87
  executables: []