active_record_not 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +45 -0
- data/lib/active_record_not/version.rb +1 -1
- data/lib/active_record_not.rb +5 -3
- metadata +3 -2
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# active_record_not
|
2
|
+
|
3
|
+
Adds #not to your ActiveRecord querying. This works similarly to #where, but inversed!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
`gem install active_record_not`, or add active_record_not to your Gemfile and `bundle install`.
|
8
|
+
|
9
|
+
https://rubygems.org/gems/active_record_not
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Given a model...
|
14
|
+
|
15
|
+
class User < ActiveRecord::Base
|
16
|
+
attr_accessible :name, :email, :phone, :employee_number, :sysadmin
|
17
|
+
...
|
18
|
+
scope :contactable, where("email is not null OR phone is not null")
|
19
|
+
scope :employee, where("employee_number > 0")
|
20
|
+
...
|
21
|
+
end
|
22
|
+
|
23
|
+
You could write...
|
24
|
+
|
25
|
+
# Provide a symbol, which corresponds to a scope, to negate that scope.
|
26
|
+
User.not(:contactable) # users that aren't contactable
|
27
|
+
|
28
|
+
# Provide a scope explicitly, to negate it
|
29
|
+
User.not(User.employee) # users that aren't employees
|
30
|
+
|
31
|
+
# Provide a hash or string to negate it
|
32
|
+
User.not(sysadmin: true) # users that aren't sysadmins
|
33
|
+
User.not("employee_number like '1'") # users where the employee number doesn't contain a 1
|
34
|
+
|
35
|
+
You can also use `#not` within a chain of queries...
|
36
|
+
|
37
|
+
User.where(sysadmin: true).not("name like 'Alex'")
|
38
|
+
|
39
|
+
## Credits
|
40
|
+
|
41
|
+
Inspired by [active_record_or](https://github.com/woahdae/active_record_or).
|
42
|
+
|
43
|
+
## Copyright
|
44
|
+
|
45
|
+
Copyright (c) 2013 Alex Ghiculescu. See LICENSE.txt for further details.
|
data/lib/active_record_not.rb
CHANGED
@@ -7,16 +7,18 @@ module ActiveRecordNot
|
|
7
7
|
|
8
8
|
case args
|
9
9
|
when Symbol
|
10
|
-
constrainted_not = left.unscoped.send(args) # eg. User.not(:active), where User#active is a scope
|
10
|
+
constrainted_not = left.unscoped.send(args).constraints.last.not # eg. User.not(:active), where User#active is a scope
|
11
11
|
when Hash
|
12
12
|
constrainted_not = left.unscoped.where(args).constraints.last.not # eg. User.not(active: true), so syntax like #where
|
13
|
+
when String
|
14
|
+
constrainted_not = left.unscoped.where(args).constraints.last.not # eg. User.not("is_active = 't'h;"), so syntax like #where
|
13
15
|
when ActiveRecord::Relation
|
14
16
|
constrainted_not = args.constraints.last.not # eg. User.not(User.active), where User#active is a scope
|
15
17
|
end
|
16
|
-
|
18
|
+
|
17
19
|
merged_not = last_left_constraint.nil? ? constrainted_not : last_left_constraint.and(constrainted_not)
|
18
20
|
|
19
|
-
right = left.
|
21
|
+
right = left.dup
|
20
22
|
right.where_values = [merged_not]
|
21
23
|
right
|
22
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_not
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -36,6 +36,7 @@ extra_rdoc_files: []
|
|
36
36
|
files:
|
37
37
|
- lib/active_record_not/version.rb
|
38
38
|
- lib/active_record_not.rb
|
39
|
+
- README.md
|
39
40
|
homepage: http://rubygems.org/gems/active_record_not
|
40
41
|
licenses: []
|
41
42
|
post_install_message:
|