yfactorial-utility_scopes 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +12 -0
- data/README.textile +128 -0
- data/spec/ordered_spec.rb +4 -0
- metadata +7 -5
- data/README.rdoc +0 -1
data/CHANGELOG
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
0.2.0
|
2
|
+
|
3
|
+
* Added except scope from danielmorrison (http://github.com/danielmorrison)
|
4
|
+
|
5
|
+
Article.except(1, 2, 3) # Get all articles whose id is NOT 1, 2 or 3
|
6
|
+
Article.except(@article) # Get all articles except the given one
|
7
|
+
Article.except(Article.published) # Get all UN-published articles (poor example
|
8
|
+
# since it executes full "published" query first)
|
9
|
+
|
10
|
+
0.1.0
|
11
|
+
|
12
|
+
* Initial release with limit, with (eager loading) and ordered scopes.
|
data/README.textile
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
h1. Utility Scopes
|
2
|
+
|
3
|
+
h2. Summary
|
4
|
+
|
5
|
+
Utility scopes provides a collection of utilitarian "named scopes":http://api.rubyonrails.com/classes/ActiveRecord/NamedScope/ClassMethods.html#M001246 for use with your
|
6
|
+
ActiveRecord models.
|
7
|
+
|
8
|
+
|
9
|
+
Utility scopes was originally announced "here":http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know and has expanded in scope and functionality since then thanks to user contributions. See the
|
10
|
+
"CHANGELOG":http://github.com/yfactorial/utility_scopes/tree/master/CHANGELOG for contribution details.
|
11
|
+
|
12
|
+
Utility scopes has the following dependencies:
|
13
|
+
|
14
|
+
* activerecord >= 2.1.0
|
15
|
+
* rspec >= 1.1.4 (for specs only, not runtime)
|
16
|
+
|
17
|
+
h2. Installation
|
18
|
+
|
19
|
+
To install the utility_scopes gem run the following:
|
20
|
+
|
21
|
+
sudo gem install yfactorial-utility_scopes
|
22
|
+
|
23
|
+
And to enable the scopes in your project just require @utility_scopes@:
|
24
|
+
|
25
|
+
require 'utility_scopes'
|
26
|
+
|
27
|
+
h3. Rails
|
28
|
+
|
29
|
+
You can also specify the gem dependency if you're running Rails 2.1 in your @config/environment.rb@ file:
|
30
|
+
|
31
|
+
Rails::Initializer.run do |config|
|
32
|
+
# ...
|
33
|
+
config.gem "yfactorial-utility_scopes", :lib => 'utility_scopes',
|
34
|
+
:source => 'http://gems.github.com/'
|
35
|
+
end
|
36
|
+
|
37
|
+
You don't need to @require 'utility_scopes'@ in this case as Rails will automatically require it.
|
38
|
+
|
39
|
+
h2. Scopes
|
40
|
+
|
41
|
+
Most examples assume the following @Article@ class:
|
42
|
+
|
43
|
+
class Article < ActiveRecord::Base
|
44
|
+
has_many :comments # (assume each comment also has a :user)
|
45
|
+
has_many :contributors
|
46
|
+
belongs_to :author, :class_name => 'User'
|
47
|
+
end
|
48
|
+
|
49
|
+
Named scopes are chainable by nature, meaning that the following is possible:
|
50
|
+
|
51
|
+
Article.with(:comments).except(1, 2, 3).ordered.limited(5)
|
52
|
+
|
53
|
+
Any exceptions to chainable scopes will be specified in their section below.
|
54
|
+
|
55
|
+
h3. With (eager-loading)
|
56
|
+
|
57
|
+
The @with@ scope let's you eager load your model associations. So instead of having to invoke @find(:all, :include => [:association1, :association2])@ just pass these association names into
|
58
|
+
the @with@ named scope:
|
59
|
+
|
60
|
+
# Get all articles and eager-load their comments, each comments' user, article contributors
|
61
|
+
# and the article author.
|
62
|
+
Article.with({ :comments => :user }, :contributors, :author)
|
63
|
+
|
64
|
+
# Get all articles and eager-load their comments
|
65
|
+
Article.with(:comments)
|
66
|
+
|
67
|
+
Again, just pass in the same arguments into @eager@ that you would pass in as the @:include@ value to "ActiveRecord::Base#find":http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001298
|
68
|
+
|
69
|
+
h3. Except
|
70
|
+
|
71
|
+
_contributed by danielmorrison_
|
72
|
+
|
73
|
+
@except@ excludes the given records from the result set:
|
74
|
+
|
75
|
+
Article.except(1, 2, 3) # Get all articles whose id is NOT 1, 2 or 3
|
76
|
+
Article.except(@article) # Get all articles except the given one
|
77
|
+
Article.except(@new_articles) # Get all non-new articles
|
78
|
+
|
79
|
+
h3. Limited
|
80
|
+
|
81
|
+
@limited@ lets you place a limit on the number of results returned. By default
|
82
|
+
the scope will limit the result set to 10 results if no argument is passed in:
|
83
|
+
|
84
|
+
Article.limited # Get the first 10 articles
|
85
|
+
Article.except(1).limited(5) # Get the first 5 articles where id != 1
|
86
|
+
|
87
|
+
If you're using "will_paginate":http://github.com/mislav/will_paginate/tree/master and don't
|
88
|
+
pass an argument to the scope then the @per_page@ value that is used by will_paginate
|
89
|
+
will be used:
|
90
|
+
|
91
|
+
Article.per_page #=> 20
|
92
|
+
Article.limited # Get the first 20 articles
|
93
|
+
|
94
|
+
If you would like to specify a different default value you can do so on a per class basis
|
95
|
+
using @default_limit@:
|
96
|
+
|
97
|
+
# Set the default limit to be 15
|
98
|
+
class Article < ActiveRecord::Base
|
99
|
+
default_limit 15
|
100
|
+
end
|
101
|
+
|
102
|
+
Article.limited # Get the first 15 articles
|
103
|
+
|
104
|
+
h3. Ordered
|
105
|
+
|
106
|
+
_Note: the @ordered@ scope cannot be chained with any other @ordered@ clauses_
|
107
|
+
|
108
|
+
@ordered@ lets you dynamically specify the ordering of your result set. If no
|
109
|
+
arguments are given it will default to @created_at DESC@:
|
110
|
+
|
111
|
+
Article.ordered # Get all articles ordered by "created_at DESC"
|
112
|
+
Article.ordered(:id) # Get all articles ordered by "id"
|
113
|
+
Article.ordered("rank ASC") # Get all articles ordered by "rank ASC"
|
114
|
+
|
115
|
+
If you would like to specify a different default sort order you can do so on a per class basis
|
116
|
+
using @ordered_by@:
|
117
|
+
|
118
|
+
# Set the default order to be "published_at DESC"
|
119
|
+
class Article < ActiveRecord::Base
|
120
|
+
ordered_by 'published_at DESC'
|
121
|
+
end
|
122
|
+
|
123
|
+
Article.ordered # Get all articles ordered by "published_at DESC"
|
124
|
+
Article.ordered("rank ASC") # Get all articles ordered by "rank ASC"
|
125
|
+
|
126
|
+
The current default ordering for a class can always be accessed via @default_ordering@:
|
127
|
+
|
128
|
+
Article.default_ordering #=> "published_at DESC"
|
data/spec/ordered_spec.rb
CHANGED
@@ -25,4 +25,8 @@ describe "Ordered scope" do
|
|
25
25
|
Article.ordered_by 'published_at DESC'
|
26
26
|
Article.ordered('popularity ASC').proxy_options.should == {:order => 'popularity ASC'}
|
27
27
|
end
|
28
|
+
|
29
|
+
it "should be able to handle symbol order criteria" do
|
30
|
+
Article.ordered(:id).proxy_options.should == { :order => :id }
|
31
|
+
end
|
28
32
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yfactorial-utility_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Daigle
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -28,13 +28,15 @@ executables: []
|
|
28
28
|
extensions: []
|
29
29
|
|
30
30
|
extra_rdoc_files:
|
31
|
-
- README.
|
31
|
+
- README.textile
|
32
32
|
- Rakefile
|
33
33
|
- LICENSE
|
34
|
+
- CHANGELOG
|
34
35
|
files:
|
35
|
-
- README.
|
36
|
+
- README.textile
|
36
37
|
- Rakefile
|
37
38
|
- LICENSE
|
39
|
+
- CHANGELOG
|
38
40
|
- init.rb
|
39
41
|
- lib
|
40
42
|
- lib/utility_scopes
|
@@ -57,7 +59,7 @@ homepage: http://github.com/yfactorial/utility_scopes
|
|
57
59
|
post_install_message:
|
58
60
|
rdoc_options:
|
59
61
|
- --main
|
60
|
-
- README.
|
62
|
+
- README.textile
|
61
63
|
- --inline-source
|
62
64
|
- --charset=UTF-8
|
63
65
|
require_paths:
|
data/README.rdoc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know
|