usefull_scopes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +124 -0
- data/Rakefile +1 -0
- data/lib/usefull_scopes.rb +65 -0
- data/lib/usefull_scopes/version.rb +3 -0
- data/usefull_scopes.gemspec +21 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mokevnin Kirill
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# UsefullScopes
|
2
|
+
|
3
|
+
This gem provides additional scopes for your ActiveRecord models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'usefull_scopes'
|
10
|
+
|
11
|
+
Or install it yourself as:
|
12
|
+
|
13
|
+
$ gem install usefull_scopes
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
In order to use these scopes, we need to include `UsefullScopes` module in our model.
|
18
|
+
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
include UsefullScopes
|
21
|
+
end
|
22
|
+
|
23
|
+
### Global scopes
|
24
|
+
|
25
|
+
<table>
|
26
|
+
<tr>
|
27
|
+
<th>Name</th>
|
28
|
+
<th>Description</th>
|
29
|
+
</tr>
|
30
|
+
<tr>
|
31
|
+
<td>random</td>
|
32
|
+
<td>Fetches a random record</td>
|
33
|
+
</tr>
|
34
|
+
<tr>
|
35
|
+
<td>exclude</td>
|
36
|
+
<td>Selects only those records who are not in a given array (you could also provide a single object as an argument)</td>
|
37
|
+
</tr>
|
38
|
+
<tr>
|
39
|
+
<td>with</td>
|
40
|
+
<td>Returns records, where attributes' values are corresponding to a given hash.</td>
|
41
|
+
</tr>
|
42
|
+
<tr>
|
43
|
+
<td>without</td>
|
44
|
+
<td>Returns records, where attributes' values are `NULL` or aren't equal to values from a given hash.</td>
|
45
|
+
</tr>
|
46
|
+
</table>
|
47
|
+
|
48
|
+
### Scopes per attribute
|
49
|
+
|
50
|
+
These are the scopes created for each model's attribute.
|
51
|
+
|
52
|
+
<table>
|
53
|
+
<tr>
|
54
|
+
<th>Name</th>
|
55
|
+
<th>Description</th>
|
56
|
+
</tr>
|
57
|
+
<tr>
|
58
|
+
<td>by_attribute</td>
|
59
|
+
<td>Returns records ordered by `attribute` in descending order</td>
|
60
|
+
</tr>
|
61
|
+
<tr>
|
62
|
+
<td>asc_by_attribute</td>
|
63
|
+
<td>Returns records ordered by `attribute` in ascending order</td>
|
64
|
+
</tr>
|
65
|
+
<tr>
|
66
|
+
<td>like_by_attribute</td>
|
67
|
+
<td>Returns records, where attribute's value like a given term </td>
|
68
|
+
</tr>
|
69
|
+
<tr>
|
70
|
+
<td>ilike_by_attribute</td>
|
71
|
+
<td>Сase insensitive implementation of `like_by_attribute`</td>
|
72
|
+
</tr>
|
73
|
+
<tr>
|
74
|
+
<td>with_attribute</td>
|
75
|
+
<td>Returns records, where attribute's value is equal to a given value.<br/><b>Note: this scope is deprecated and will be removed in the following versions. Please, use `with` scope instead.</b></td>
|
76
|
+
</tr>
|
77
|
+
<tr>
|
78
|
+
<td>without_attribute</td>
|
79
|
+
<td>Returns records, where attribute's value is `NULL`.<br/><b>Note: this scope is deprecated and will be removed in the following versions. Please, use `without` scope instead.</b></td>
|
80
|
+
</tr>
|
81
|
+
</table>
|
82
|
+
|
83
|
+
### Example
|
84
|
+
|
85
|
+
Now, it is time to play with our model!
|
86
|
+
|
87
|
+
User.create([{name: 'Mike'}, {name: 'Paul'}])
|
88
|
+
|
89
|
+
user = User.random
|
90
|
+
user.name
|
91
|
+
=> 'Mike'
|
92
|
+
|
93
|
+
User.asc_by_name.map(&:name)
|
94
|
+
=> ['Mike', 'Paul']
|
95
|
+
|
96
|
+
User.by_name.map(&:name)
|
97
|
+
=> ['Paul', 'Mike']
|
98
|
+
|
99
|
+
users = User.with_name('Mike')
|
100
|
+
users.map(&:name)
|
101
|
+
=> ['Mike']
|
102
|
+
|
103
|
+
users = User.with(name: 'Mike')
|
104
|
+
=> SELECT "users".* FROM "users" WHERE ("users"."name" = 'Mike')
|
105
|
+
users.map(&:name)
|
106
|
+
=> ['Mike']
|
107
|
+
|
108
|
+
users = User.without(name: ['Mike', 'Paul'])
|
109
|
+
=> SELECT "users".* FROM "users" WHERE ("users"."name" NOT IN ('Mike','Paul'))
|
110
|
+
users
|
111
|
+
=> []
|
112
|
+
|
113
|
+
users = User.without(:name, :id)
|
114
|
+
=> SELECT "users".* FROM "users" WHERE ("users"."name" IS NULL AND "users"."id" IS NULL)
|
115
|
+
users.count
|
116
|
+
=> 2
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
1. Fork it
|
121
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
122
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
123
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
124
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module UsefullScopes
|
2
|
+
autoload :Version, 'usefull_scopes/version'
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
scope :random, order("RANDOM()")
|
7
|
+
scope :exclude, lambda {|collection_or_object|
|
8
|
+
collection = Array(collection_or_object)
|
9
|
+
values = collection.map do |id_or_object|
|
10
|
+
id_or_object.is_a?(ActiveRecord::Base) ? id_or_object.id : id_or_object
|
11
|
+
end
|
12
|
+
return scoped unless values.any?
|
13
|
+
where("#{quoted_table_name}.id not in (?)", values)
|
14
|
+
}
|
15
|
+
|
16
|
+
scope :with, lambda {|attrs_hash|
|
17
|
+
case attrs_hash
|
18
|
+
when Hash
|
19
|
+
where(attrs_hash)
|
20
|
+
else
|
21
|
+
raise TypeError, "Hash is expected"
|
22
|
+
end
|
23
|
+
}
|
24
|
+
|
25
|
+
scope :without, lambda {|*attrs|
|
26
|
+
attrs_hash = attrs.extract_options!
|
27
|
+
query_params = []
|
28
|
+
|
29
|
+
attrs.each do |attr_name|
|
30
|
+
query_params << "#{quoted_table_name}.#{attr_name} IS NULL"
|
31
|
+
end
|
32
|
+
|
33
|
+
attrs_hash.each do |attr_name, attr_value|
|
34
|
+
query_params << "#{quoted_table_name}.#{attr_name} NOT IN (:#{attr_name})"
|
35
|
+
end
|
36
|
+
|
37
|
+
return scoped if query_params.blank?
|
38
|
+
where query_params.join(" AND "), attrs_hash
|
39
|
+
}
|
40
|
+
|
41
|
+
attribute_names.each do |a|
|
42
|
+
scope "by_#{a}", order("#{quoted_table_name}.#{a} DESC")
|
43
|
+
scope "asc_by_#{a}", order("#{quoted_table_name}.#{a} ASC")
|
44
|
+
|
45
|
+
scope "like_by_#{a}", lambda {|term|
|
46
|
+
quoted_term = connection.quote(term + '%')
|
47
|
+
where("lower(#{quoted_table_name}.#{a}) like #{quoted_term}")
|
48
|
+
}
|
49
|
+
scope "ilike_by_#{a}", lambda {|term|
|
50
|
+
quoted_term = connection.quote(term + '%')
|
51
|
+
where("#{quoted_table_name}.#{a} ilike #{quoted_term}")
|
52
|
+
}
|
53
|
+
|
54
|
+
scope "with_#{a}", lambda { |value|
|
55
|
+
puts "*** DEPRECATION WARNING: Scope `with_#{a}` is deprecated and will be removed in the following versions. Please, use `with` scope instead."
|
56
|
+
where("#{quoted_table_name}.#{a} = ?", value)
|
57
|
+
}
|
58
|
+
scope "without_#{a}", lambda {
|
59
|
+
puts "*** DEPRECATION WARNING: Scope `without_#{a}` is deprecated and will be removed in the following versions. Please, use `without` scope instead."
|
60
|
+
where("#{quoted_table_name}.#{a} IS NULL")
|
61
|
+
}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "usefull_scopes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "usefull_scopes"
|
7
|
+
s.version = UsefullScopes::VERSION
|
8
|
+
s.authors = ["Kaize Team"]
|
9
|
+
s.email = ["info@kaize.ru"]
|
10
|
+
s.homepage = "http://github.com/kaize/usefull_scopes"
|
11
|
+
s.summary = "Additional scopes for Active:Record models."
|
12
|
+
s.description = "This gem provides additional scopes for your Active:Record models within Ruby on Rails framework."
|
13
|
+
|
14
|
+
s.rubyforge_project = "usefull_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
|
+
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: usefull_scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kaize Team
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This gem provides additional scopes for your Active:Record models within
|
15
|
+
Ruby on Rails framework.
|
16
|
+
email:
|
17
|
+
- info@kaize.ru
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/usefull_scopes.rb
|
28
|
+
- lib/usefull_scopes/version.rb
|
29
|
+
- usefull_scopes.gemspec
|
30
|
+
homepage: http://github.com/kaize/usefull_scopes
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project: usefull_scopes
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Additional scopes for Active:Record models.
|
54
|
+
test_files: []
|
55
|
+
has_rdoc:
|