scopes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in scopes.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Scopes
2
+
3
+ A collection of usefull active record query scopes.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/scopes.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "scopes/version"
2
+ require "active_record"
3
+ require "scopes/like"
4
+ require "scopes/not"
5
+
6
+ module Scopes
7
+ end
@@ -0,0 +1,28 @@
1
+ module Scopes
2
+ module Like
3
+ def self.extended(base)
4
+ delegate :like, :not_like, :to => :scoped
5
+ end
6
+
7
+ module QueryMethods
8
+ def like(opts, *rest)
9
+ return self if opts.blank?
10
+
11
+ relation = clone
12
+ relation.where_values += build_where(opts, rest).map { |relation| Arel::Nodes::Matches.new relation.left, relation.right }
13
+ relation
14
+ end
15
+
16
+ def not_like(opts, *rest)
17
+ return self if opts.blank?
18
+
19
+ relation = clone
20
+ relation.where_values += build_where(opts, rest).map { |relation| Arel::Nodes::DoesNotMatch.new relation.left, relation.right }
21
+ relation
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ ActiveRecord::Base.send :extend, Scopes::Like
28
+ ActiveRecord::QueryMethods.send :include, Scopes::Like::QueryMethods
data/lib/scopes/not.rb ADDED
@@ -0,0 +1,30 @@
1
+ module Scopes
2
+ module Not
3
+ def self.extended(base)
4
+ delegate :not, :to => :scoped
5
+ end
6
+
7
+ module QueryMethods
8
+ def not(opts, *rest)
9
+ return self if opts.blank?
10
+
11
+ relation = clone
12
+ relation.where_values += build_where(opts, rest).map do |relation|
13
+ # TODO: understanding strict module comparison
14
+ case relation.class.name
15
+ when "Arel::Nodes::Equality"
16
+ Arel::Nodes::NotEqual.new relation.left, relation.right
17
+ when "Arel::Nodes::In"
18
+ Arel::Nodes::NotIn.new relation.left, relation.right
19
+ else
20
+ relation.not
21
+ end
22
+ end
23
+ relation
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ ActiveRecord::Base.send :extend, Scopes::Not
30
+ ActiveRecord::QueryMethods.send :include, Scopes::Not::QueryMethods
@@ -0,0 +1,3 @@
1
+ module Scopes
2
+ VERSION = "0.0.1"
3
+ end
data/scopes.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "scopes/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "scopes"
7
+ s.version = Scopes::VERSION
8
+ s.authors = ["Rodrigo Navarro"]
9
+ s.email = ["rnavarro1@gmail.com"]
10
+ s.homepage = "https://github.com/reu/scopes"
11
+ s.summary = %q{ActiveRecord bonus scopes}
12
+ s.description = %q{ActiveRecord bonus scopes}
13
+
14
+ s.rubyforge_project = "scopes"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "sqlite3"
23
+ s.add_runtime_dependency "activerecord", "~> 3"
24
+ end
data/spec/like_spec.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe ".like" do
4
+ context "on a direct table" do
5
+ subject { Post.like(:title => "%test%").to_sql }
6
+
7
+ it { should match %q("posts"."title" LIKE '%test%') }
8
+ end
9
+
10
+ context "on a joined table" do
11
+ subject { Post.joins(:comments).like(:title => "%lol%", :comments => { :body => "%wut" }).to_sql }
12
+
13
+ it { should match %q("posts"."title" LIKE '%lol%') }
14
+ it { should match %q("comments"."body" LIKE '%wut') }
15
+ end
16
+ end
17
+
18
+ describe ".not_like" do
19
+ context "on a direct table" do
20
+ subject { Post.not_like(:title => "%test%").to_sql }
21
+
22
+ it { should match %q("posts"."title" NOT LIKE '%test%') }
23
+ end
24
+
25
+ context "on a joined table" do
26
+ subject { Post.joins(:comments).not_like(:title => "%lol%", :comments => { :body => "%wut" }).to_sql }
27
+
28
+ it { should match %q("posts"."title" NOT LIKE '%lol%') }
29
+ it { should match %q("comments"."body" NOT LIKE '%wut') }
30
+ end
31
+
32
+ context "mixing with like" do
33
+ subject { Post.joins(:comments).like(:title => "%lol%").not_like(:comments => { :body => "%wut" }).to_sql }
34
+
35
+ it { should match %q("posts"."title" LIKE '%lol%') }
36
+ it { should match %q("comments"."body" NOT LIKE '%wut') }
37
+ end
38
+ end
data/spec/not_spec.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+
3
+ describe ".not" do
4
+ context "on a direct table" do
5
+ subject { Post.not(:title => "test").to_sql }
6
+
7
+ it { should match %q("posts"."title" != 'test') }
8
+ end
9
+
10
+ context "on a joined table" do
11
+ subject { Post.joins(:comments).not(:title => "lol", :comments => { :body => "wut" }).to_sql }
12
+
13
+ it { should match %q("posts"."title" != 'lol') }
14
+ it { should match %q("comments"."body" != 'wut') }
15
+ end
16
+
17
+ context "with array argument" do
18
+ subject { Post.joins(:comments).not(:comments => { :id => [1, 3, 4] }).to_sql }
19
+ it { should include %q("comments"."id" NOT IN (1, 3, 4)) }
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ require "scopes"
2
+
3
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
4
+
5
+ class Post < ActiveRecord::Base
6
+ has_many :comments
7
+ end
8
+
9
+ class Comment < ActiveRecord::Base
10
+ belongs_to :post
11
+ end
12
+
13
+ ActiveRecord::Schema.define do
14
+ create_table :posts, :force => true do |t|
15
+ t.string :title
16
+ t.text :body
17
+ end
18
+
19
+ create_table :comments, :force => true do |t|
20
+ t.references :post
21
+ t.text :body
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Navarro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2153021020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2153021020
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &2153020400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2153020400
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ requirement: &2153019700 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2153019700
47
+ description: ActiveRecord bonus scopes
48
+ email:
49
+ - rnavarro1@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - lib/scopes.rb
59
+ - lib/scopes/like.rb
60
+ - lib/scopes/not.rb
61
+ - lib/scopes/version.rb
62
+ - scopes.gemspec
63
+ - spec/like_spec.rb
64
+ - spec/not_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: https://github.com/reu/scopes
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: 3402649617754245914
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: 3402649617754245914
90
+ requirements: []
91
+ rubyforge_project: scopes
92
+ rubygems_version: 1.8.10
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: ActiveRecord bonus scopes
96
+ test_files:
97
+ - spec/like_spec.rb
98
+ - spec/not_spec.rb
99
+ - spec/spec_helper.rb