activerecord-where-any-of 1.0.6 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fef5ed54bad29379534fe78509785670884e727a
4
- data.tar.gz: 20680a21436a675196da93736f5a3ee737547d84
3
+ metadata.gz: 07f33898c696d787dbb54fe69e22445332f59d74
4
+ data.tar.gz: ab421f360ba43c7bf6ef5dcb36dd37cc3130884a
5
5
  SHA512:
6
- metadata.gz: cbe1832d8d6b711ce196c91808edc0c34fe6384d959b4d39f6d1943e99dfa69b6d2878f63cfe2e82b76881f968e3fd2f3c17b1ea3def9731f7c1786ca0ce3551
7
- data.tar.gz: 0637588ca5ed891adb966c8dafc45c4ad9adf650569463e73f776fbe33a79f0cf4d35fc8603bd47a63b04bc5ee656358bd1d3e934aefe774d8d1f742be5d9660
6
+ metadata.gz: ea1dae0d404b1cf67210eee30a18df9e498cd99685c48b582536cb15b4c750f8e22c79776aa1c1ab07b94b9c724fb8587112e10adbf18517ca516306401c35c3
7
+ data.tar.gz: 55e261b044362ddbf309c9c1e2a9cc5fff22da86f3b78654c2a1ade5c3ff0e18ebe41fe5906f130055aa471e02acc18f2171dee7837ba62bdef639e5a29197cb
data/Rakefile CHANGED
@@ -1 +1,23 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ namespace :rspec do
4
+
5
+ desc "Run specs against ActiveRecord 3"
6
+ task :active_record_3 do
7
+ sh 'ACTIVE_RECORD_VERSION=3 rspec -O spec/spec.opts'
8
+ end
9
+
10
+ desc "Run specs against ActiveRecord 4"
11
+ task :active_record_4 do
12
+ sh 'ACTIVE_RECORD_VERSION=4 rspec -O spec/spec.opts'
13
+ end
14
+
15
+ desc "Run specs against ActiveRecord 5"
16
+ task :active_record_5 do
17
+ sh 'ACTIVE_RECORD_VERSION=5 rspec -O spec/spec.opts'
18
+ end
19
+
20
+ desc "Run specs against all three versions of ActiveRecord"
21
+ task all: [:active_record_3, :active_record_4, :active_record_5]
22
+
23
+ end
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'activerecord-where-any-of'
3
- s.version = '1.0.6'
3
+ s.version = '1.1.0'
4
4
  s.license = 'MIT'
5
- s.date = '2015-12-28'
5
+ s.date = '2016-12-25'
6
6
  s.summary = 'A simple mixin to ActiveRecord::Base that allows use of OR arel relations.'
7
- s.description = '...'
7
+ s.description = 'Should work in Rails 3, 4, & 5. However, in Rails 5 use of #or is prefered.'
8
8
  s.description = File.read(File.expand_path('../README.md', __FILE__))[/Description:\n-+\s*(.*?)\n\n/m, 1]
9
9
  s.authors = ['David McCullars']
10
10
  s.email = ['david.mccullars@gmail.com']
@@ -15,4 +15,5 @@ Gem::Specification.new do |s|
15
15
  s.add_runtime_dependency 'activerecord', '>= 3.0'
16
16
  s.add_development_dependency 'bundler', '>= 1.3'
17
17
  s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'rspec'
18
19
  end
@@ -0,0 +1,19 @@
1
+ # Rails 5+ mixin
2
+ module ActiveRecord
3
+ module WhereAnyOfMixin
4
+
5
+ def where_any_of(*conditions)
6
+ # Rails 5 comes with an #or method for relations, so we can just use that
7
+ # It is advised that once on Rails 5, this gem should be removed.
8
+ conditions = conditions.map do |c|
9
+ if c.is_a? Symbol or c.is_a? String
10
+ self.unscoped.send(c)
11
+ else
12
+ c
13
+ end
14
+ end
15
+ all.merge conditions.reduce(:or)
16
+ end
17
+
18
+ end
19
+ end
@@ -1,16 +1,21 @@
1
+ # Rails 4+ mixin
1
2
  module ActiveRecord
2
3
  module WhereAnyOfMixin
3
4
 
4
5
  def where_any_of(*conditions)
5
6
  # Takes any number of scopes and OR them together
6
7
  # into a new scope which can be combined with other scopes
8
+ bind_values = []
7
9
  conditions = conditions.map do |c|
8
10
  if c.is_a? Symbol or c.is_a? String
9
11
  c = self.unscoped.send(c)
10
12
  end
13
+ bind_values.concat(c.bind_values)
11
14
  __convert_string_wheres(c.where_values).reduce(:and)
12
15
  end
13
- where(conditions.reduce(:or))
16
+ s = where(conditions.reduce(:or))
17
+ s.bind_values += bind_values if bind_values.present?
18
+ s
14
19
  end
15
20
 
16
21
  def __convert_string_wheres(wheres)
@@ -1,3 +1,7 @@
1
1
  require 'active_record'
2
- require 'active_record/where-any-of-mixin'
2
+ if ActiveRecord::VERSION::MAJOR >= 5
3
+ require 'active_record/where-any-of-mixin-rails-5'
4
+ else
5
+ require 'active_record/where-any-of-mixin'
6
+ end
3
7
  ActiveRecord::Base.send(:extend, ActiveRecord::WhereAnyOfMixin)
Binary file
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+ require 'activerecord-where-any-of'
3
+
4
+ describe 'where_any_of' do
5
+
6
+ subject { ExampleModel }
7
+
8
+ let(:lparen) do
9
+ '(' if ActiveRecord::VERSION::MAJOR <= 3
10
+ end
11
+
12
+ let(:rparen) do
13
+ ')' if ActiveRecord::VERSION::MAJOR <= 3
14
+ end
15
+
16
+ describe '(*relations)' do
17
+
18
+ let(:relation) do
19
+ subject.where_any_of(
20
+ subject.active.on_shelf('h'),
21
+ subject.inactive.recent,
22
+ subject.with_name('apple'),
23
+ )
24
+ end
25
+
26
+ specify :count do
27
+ expect(relation.count).to eq(6)
28
+ end
29
+
30
+ specify :first do
31
+ m = relation.first
32
+ expect(m.id).to eq(1)
33
+ expect(m.name).to eq('apple')
34
+ end
35
+
36
+ specify :to_sql do
37
+ expect(relation.to_sql).to match_ignoring_whitespace(<<-END)
38
+ SELECT "example_models".*
39
+ FROM "example_models"
40
+ WHERE #{lparen}((
41
+ "example_models"."active" = 't' AND (name LIKE 'h%')
42
+ OR "example_models"."active" = 'f' AND (created_at > '2016-11-01'))
43
+ OR "example_models"."name" = 'apple'
44
+ )#{rparen}
45
+ END
46
+ end
47
+
48
+ specify :group_count do
49
+ expect(relation.group(:active).count).to match(true => 2, false => 4)
50
+ end
51
+
52
+ specify :order_limit do
53
+ expect(relation.order('updated_at DESC').limit(2).pluck(:name)).to eq [
54
+ "historiette boxthorn quinaldine",
55
+ "slipcase diorthotic holocentrid plessimetry prestudiously",
56
+ ]
57
+ end
58
+
59
+ end
60
+
61
+ describe "(:symbol, 'string', relation)" do
62
+
63
+ let(:relation) do
64
+ subject.where('name <> ?', 'bad').where_any_of(
65
+ :recent,
66
+ 'active',
67
+ subject.on_shelf('z'),
68
+ )
69
+ end
70
+
71
+ specify :count do
72
+ expect(relation.count).to eq(38)
73
+ end
74
+
75
+ specify :merge_and_count do
76
+ expect(relation.merge(subject.inactive).count).to eq(4)
77
+ end
78
+
79
+ specify :to_sql do
80
+ expect(relation.to_sql).to match_ignoring_whitespace(<<-END)
81
+ SELECT "example_models".*
82
+ FROM "example_models"
83
+ WHERE (name <> 'bad') AND #{lparen}((
84
+ (created_at > '2016-11-01')
85
+ OR "example_models"."active" = 't')
86
+ OR (name LIKE 'z%')
87
+ )#{rparen}
88
+ END
89
+ end
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format progress
@@ -0,0 +1,11 @@
1
+ if ENV['COVERAGE'] =~ /^t/i
2
+ require 'simplecov'
3
+
4
+ SimpleCov.start 'root_filter' do
5
+ add_filter '/spec/'
6
+ end
7
+ end
8
+
9
+ require 'rspec'
10
+
11
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
@@ -0,0 +1,10 @@
1
+ version = ENV.fetch('ACTIVE_RECORD_VERSION', '3.0').to_f
2
+ raise "Unsupported ActiveRecord version #{version.inspect}" unless version >= 3.0 && version < 6.0
3
+ gem 'activerecord', "~> #{version}"
4
+
5
+ require 'active_record'
6
+
7
+ ActiveRecord::Base.establish_connection(
8
+ adapter: 'sqlite3',
9
+ database: File.expand_path('../../database.sqlite3', __FILE__),
10
+ )
@@ -0,0 +1,23 @@
1
+ class ExampleModel < ActiveRecord::Base
2
+
3
+ def self.with_name(name)
4
+ where(name: name)
5
+ end
6
+
7
+ def self.on_shelf(letter)
8
+ letter =~ /\A[a-z]\z/i ? where("name LIKE '#{letter}%'") : where('1=0')
9
+ end
10
+
11
+ def self.active
12
+ where(active: true)
13
+ end
14
+
15
+ def self.inactive
16
+ where(active: false)
17
+ end
18
+
19
+ def self.recent
20
+ where('created_at > ?', Date.parse('2016-11-01'))
21
+ end
22
+
23
+ end
@@ -0,0 +1,18 @@
1
+ RSpec::Matchers.define :match_ignoring_whitespace do |expected|
2
+ match do |actual|
3
+ expected.to_s.gsub(/\s/, '') == actual.to_s.gsub(/\s/, '')
4
+ end
5
+
6
+ failure_message do |actual|
7
+ sep = '=' * 80
8
+ <<-END
9
+ #{sep}
10
+ Expected:
11
+ #{expected.to_s.strip}
12
+ #{sep}
13
+ Actual:
14
+ #{actual.to_s.strip}
15
+ #{sep}
16
+ END
17
+ end
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-where-any-of
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David McCullars
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-28 00:00:00.000000000 Z
11
+ date: 2016-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: |-
56
70
  Provides a mechanism for OR'ing together one or more AREL relations. Normally when
57
71
  one performs `SomeModel.where(condition_1).where(condition_2)`, the result is to AND
@@ -69,8 +83,16 @@ files:
69
83
  - README.md
70
84
  - Rakefile
71
85
  - activerecord-where-any-of.gemspec
86
+ - lib/active_record/where-any-of-mixin-rails-5.rb
72
87
  - lib/active_record/where-any-of-mixin.rb
73
88
  - lib/activerecord-where-any-of.rb
89
+ - spec/database.sqlite3
90
+ - spec/lib/activerecord_where_any_of_spec.rb
91
+ - spec/spec.opts
92
+ - spec/spec_helper.rb
93
+ - spec/support/active_record.rb
94
+ - spec/support/example_model.rb
95
+ - spec/support/match_ignoring_whitespace.rb
74
96
  homepage:
75
97
  licenses:
76
98
  - MIT
@@ -91,8 +113,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
113
  version: '0'
92
114
  requirements: []
93
115
  rubyforge_project:
94
- rubygems_version: 2.2.2
116
+ rubygems_version: 2.6.7
95
117
  signing_key:
96
118
  specification_version: 4
97
119
  summary: A simple mixin to ActiveRecord::Base that allows use of OR arel relations.
98
120
  test_files: []
121
+ has_rdoc: