jqr-negative_named_scope 0.0.2
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.
- data/CHANGELOG +2 -0
- data/LICENSE +20 -0
- data/Manifest +11 -0
- data/README.rdoc +39 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/lib/negative_named_scope.rb +34 -0
- data/negative_named_scope.gemspec +34 -0
- data/spec/models.rb +18 -0
- data/spec/negative_named_scope_spec.rb +51 -0
- data/spec/spec_helper.rb +38 -0
- metadata +79 -0
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Elijah Miller
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= Negative Named Scope
|
2
|
+
|
3
|
+
Tired of writing both the positive and negative versions of a named_scope?
|
4
|
+
Negative Named Scope will automatically generate the NOT'd version of your
|
5
|
+
named scope.
|
6
|
+
|
7
|
+
Warning: Ruby may treat !nil == true but SQL treats NOT NULL as NULL, see:
|
8
|
+
http://en.wikipedia.org/wiki/Null_(SQL)
|
9
|
+
|
10
|
+
== Example
|
11
|
+
|
12
|
+
named_scope :approved, :conditions => { :approved => true }
|
13
|
+
# => named_scope :not_approved, :conditions => "NOT (approved = 't')"
|
14
|
+
|
15
|
+
named_scope :published, :conditions => { :published => true }, :negative => :unpublished
|
16
|
+
# => named_scope :unpublished, :conditions => "NOT (published = 't')"
|
17
|
+
|
18
|
+
named_scope :created_after, lambda { |value| { :conditions => ['created_after', value] } }
|
19
|
+
# => named_scope :not_created_after, lambda { |value| :conditions => ["NOT (created_after > = ?)", value] }
|
20
|
+
|
21
|
+
|
22
|
+
# suppress the automatic negative named_scope generation
|
23
|
+
named_scope :great, :conditions => { :great => true }, :negative => false
|
24
|
+
|
25
|
+
|
26
|
+
== Install
|
27
|
+
|
28
|
+
As a Rails plugin:
|
29
|
+
|
30
|
+
./script/plugin install git://github.com/jqr/negative_named_scope.git
|
31
|
+
|
32
|
+
Prefer gems?
|
33
|
+
|
34
|
+
gem sources -a http://gems.github.com
|
35
|
+
gem install jqr-negative_named_scope
|
36
|
+
|
37
|
+
|
38
|
+
Homepage:: http://github.com/jqr/negative_named_scope
|
39
|
+
License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec/rake/spectask'
|
2
|
+
|
3
|
+
require 'echoe'
|
4
|
+
Echoe.new 'negative_named_scope' do |p|
|
5
|
+
p.description = "Negative Named Scope will automatically generate the NOT'd version of your named scope."
|
6
|
+
# p.url = "http://versionable.rubyforge.org"
|
7
|
+
p.author = "Elijah Miller"
|
8
|
+
p.email = "elijah.miller@gmail.com"
|
9
|
+
p.retain_gemspec = true
|
10
|
+
p.need_tar_gz = false
|
11
|
+
p.extra_deps = [
|
12
|
+
]
|
13
|
+
p.ignore_pattern = ['spec/test.sqlite3']
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Default: run specs'
|
17
|
+
task :default => :spec
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
20
|
+
end
|
21
|
+
|
22
|
+
task :test => :spec
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'negative_named_scope'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module NamedScope
|
3
|
+
module ClassMethods
|
4
|
+
def named_scope_with_negative_named_scope(method, *args, &block)
|
5
|
+
options = args.extract_options!
|
6
|
+
lambda = args.first
|
7
|
+
|
8
|
+
args_for_original = lambda || options.except(:negative)
|
9
|
+
|
10
|
+
named_scope_without_negative_named_scope(method, args_for_original, &block)
|
11
|
+
|
12
|
+
if options[:negative] != false
|
13
|
+
negative_name = options.delete(:negative) || "not_#{method}"
|
14
|
+
|
15
|
+
unless respond_to?(negative_name)
|
16
|
+
if !lambda
|
17
|
+
if options.include?(:conditions)
|
18
|
+
named_scope_without_negative_named_scope(negative_name, lambda { { :conditions => "NOT (#{ sanitize_sql_for_conditions(options[:conditions]) })" } }, &block)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
named_scope_without_negative_named_scope(negative_name, lambda { |*args|
|
22
|
+
lambda_options = lambda.call(*args)
|
23
|
+
lambda_options[:conditions] = "NOT (#{ sanitize_sql_for_conditions(lambda_options[:conditions]) })"
|
24
|
+
lambda_options
|
25
|
+
}, &block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias_method_chain :named_scope, :negative_named_scope
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{negative_named_scope}
|
5
|
+
s.version = "0.0.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Elijah Miller"]
|
9
|
+
s.date = %q{2008-12-07}
|
10
|
+
s.description = %q{Negative Named Scope will automatically generate the NOT'd version of your named scope.}
|
11
|
+
s.email = %q{elijah.miller@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/negative_named_scope.rb", "LICENSE", "README.rdoc"]
|
13
|
+
s.files = ["CHANGELOG", "init.rb", "lib/negative_named_scope.rb", "LICENSE", "Manifest", "Rakefile", "README.rdoc", "spec/models.rb", "spec/negative_named_scope_spec.rb", "spec/spec_helper.rb", "negative_named_scope.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Negative_named_scope", "--main", "README.rdoc"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{negative_named_scope}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Negative Named Scope will automatically generate the NOT'd version of your named scope.}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
30
|
+
end
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
33
|
+
end
|
34
|
+
end
|
data/spec/models.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Post < ActiveRecord::Base
|
2
|
+
named_scope :only_10, :limit => 10
|
3
|
+
|
4
|
+
named_scope :approved, :conditions => { :approved => true }
|
5
|
+
named_scope :published, :conditions => { :published => true }, :negative => :unpublished
|
6
|
+
|
7
|
+
named_scope :negative_scope_suppressed, :conditions => { :something => true }, :negative => false
|
8
|
+
|
9
|
+
def self.not_highly_rated
|
10
|
+
'not_highly_rated'
|
11
|
+
end
|
12
|
+
named_scope :highly_rated, { :conditions => 'rating > 10' }
|
13
|
+
|
14
|
+
|
15
|
+
named_scope :created_after, lambda { |value| { :conditions => ['created_after > ?', value] } }
|
16
|
+
named_scope :meaningless, { :conditions => 'rating = 0' }, :negative => :meaningful
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
|
4
|
+
describe "NegativeNamedScopes" do
|
5
|
+
describe "making a named_scope without conditions" do
|
6
|
+
it "should not make a negative named_scope" do
|
7
|
+
Post.scopes.include?(:not_only_10).should be_false
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "making a named_scope with conditions" do
|
12
|
+
it "should make a named_scope with not prepended" do
|
13
|
+
Post.scopes.include?(:not_approved).should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should make a named_scope of specified name" do
|
17
|
+
Post.scopes.include?(:unpublished).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not make a named_scope when :negative is false" do
|
21
|
+
Post.scopes.include?(:not_negative_scope_suppressed).should be_false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should set the conditions to NOT (original conditions)" do
|
25
|
+
Post.unpublished.proxy_options[:conditions].should == %Q{NOT ("posts"."published" = 't')}
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not overwrite a existing method" do
|
29
|
+
Post.not_highly_rated.should == 'not_highly_rated'
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "making a named_scope with a lambda" do
|
35
|
+
it "should make a negative named_scope" do
|
36
|
+
Post.respond_to?(:not_created_after).should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set the conditions to NOT (original conditions)" do
|
40
|
+
Post.not_created_after(1).proxy_options[:conditions].should == %Q{NOT (created_after > 1)}
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should make a named_scope of specified name" do
|
44
|
+
Post.scopes.include?(:meaningful).should be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "test with data" do
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_support/test_case'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'negative_named_scope')
|
7
|
+
|
8
|
+
require File.join(File.dirname(__FILE__), 'models')
|
9
|
+
|
10
|
+
TEST_DATABASE_FILE = File.join(File.dirname(__FILE__), 'test.sqlite3')
|
11
|
+
|
12
|
+
|
13
|
+
def setup_database
|
14
|
+
File.unlink(TEST_DATABASE_FILE) if File.exist?(TEST_DATABASE_FILE)
|
15
|
+
ActiveRecord::Base.establish_connection(
|
16
|
+
"adapter" => "sqlite3", "timeout" => 5000, "database" => TEST_DATABASE_FILE
|
17
|
+
)
|
18
|
+
create_tables
|
19
|
+
load_fixtures
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_tables
|
23
|
+
c = ActiveRecord::Base.connection
|
24
|
+
|
25
|
+
c.create_table :posts, :force => true do |t|
|
26
|
+
t.string :name
|
27
|
+
t.boolean :published, :null => false
|
28
|
+
t.integer :rating, :null => false
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_fixtures
|
34
|
+
Post.create(:name => 'True', :published => true, :rating => 100)
|
35
|
+
Post.create(:name => 'False', :published => false, :rating => 3)
|
36
|
+
end
|
37
|
+
|
38
|
+
setup_database
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jqr-negative_named_scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Elijah Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-07 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: echoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: Negative Named Scope will automatically generate the NOT'd version of your named scope.
|
25
|
+
email: elijah.miller@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- CHANGELOG
|
32
|
+
- lib/negative_named_scope.rb
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
files:
|
36
|
+
- CHANGELOG
|
37
|
+
- init.rb
|
38
|
+
- lib/negative_named_scope.rb
|
39
|
+
- LICENSE
|
40
|
+
- Manifest
|
41
|
+
- Rakefile
|
42
|
+
- README.rdoc
|
43
|
+
- spec/models.rb
|
44
|
+
- spec/negative_named_scope_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
- negative_named_scope.gemspec
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: ""
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --line-numbers
|
52
|
+
- --inline-source
|
53
|
+
- --title
|
54
|
+
- Negative_named_scope
|
55
|
+
- --main
|
56
|
+
- README.rdoc
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "1.2"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project: negative_named_scope
|
74
|
+
rubygems_version: 1.2.0
|
75
|
+
signing_key:
|
76
|
+
specification_version: 2
|
77
|
+
summary: Negative Named Scope will automatically generate the NOT'd version of your named scope.
|
78
|
+
test_files: []
|
79
|
+
|