yfactorial-utility_scopes 0.2.0 → 0.2.1
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 +5 -0
- data/README.textile +7 -1
- data/lib/utility_scopes/except.rb +24 -4
- data/lib/utility_scopes/ordered.rb +1 -0
- data/spec/except_spec.rb +4 -4
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.textile
CHANGED
@@ -57,12 +57,14 @@ h3. With (eager-loading)
|
|
57
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
58
|
the @with@ named scope:
|
59
59
|
|
60
|
+
<pre><code>
|
60
61
|
# Get all articles and eager-load their comments, each comments' user, article contributors
|
61
62
|
# and the article author.
|
62
63
|
Article.with({ :comments => :user }, :contributors, :author)
|
63
64
|
|
64
65
|
# Get all articles and eager-load their comments
|
65
66
|
Article.with(:comments)
|
67
|
+
</code></pre>
|
66
68
|
|
67
69
|
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
70
|
|
@@ -94,16 +96,18 @@ will be used:
|
|
94
96
|
If you would like to specify a different default value you can do so on a per class basis
|
95
97
|
using @default_limit@:
|
96
98
|
|
99
|
+
<pre><code>
|
97
100
|
# Set the default limit to be 15
|
98
101
|
class Article < ActiveRecord::Base
|
99
102
|
default_limit 15
|
100
103
|
end
|
101
104
|
|
102
105
|
Article.limited # Get the first 15 articles
|
106
|
+
</code></pre>
|
103
107
|
|
104
108
|
h3. Ordered
|
105
109
|
|
106
|
-
_Note: the @ordered@ scope cannot be chained with any other @
|
110
|
+
_Note: the @ordered@ scope cannot be chained with any other @order@ clauses_
|
107
111
|
|
108
112
|
@ordered@ lets you dynamically specify the ordering of your result set. If no
|
109
113
|
arguments are given it will default to @created_at DESC@:
|
@@ -115,6 +119,7 @@ arguments are given it will default to @created_at DESC@:
|
|
115
119
|
If you would like to specify a different default sort order you can do so on a per class basis
|
116
120
|
using @ordered_by@:
|
117
121
|
|
122
|
+
<pre><code>
|
118
123
|
# Set the default order to be "published_at DESC"
|
119
124
|
class Article < ActiveRecord::Base
|
120
125
|
ordered_by 'published_at DESC'
|
@@ -122,6 +127,7 @@ using @ordered_by@:
|
|
122
127
|
|
123
128
|
Article.ordered # Get all articles ordered by "published_at DESC"
|
124
129
|
Article.ordered("rank ASC") # Get all articles ordered by "rank ASC"
|
130
|
+
</code></pre>
|
125
131
|
|
126
132
|
The current default ordering for a class can always be accessed via @default_ordering@:
|
127
133
|
|
@@ -1,9 +1,30 @@
|
|
1
1
|
module UtilityScopes
|
2
2
|
module Except
|
3
3
|
|
4
|
-
def self.included(within)
|
5
|
-
|
4
|
+
def self.included(within)
|
6
5
|
within.class_eval do
|
6
|
+
|
7
|
+
# Give every class the ability to add to itself the except named
|
8
|
+
# scope
|
9
|
+
extend ClassMethods
|
10
|
+
|
11
|
+
# And automatically do so for all its subclasses
|
12
|
+
def self.inherited_with_except_scope_hook(child)
|
13
|
+
inherited_without_except_scope_hook(child)
|
14
|
+
child.attach_except_utility_scope
|
15
|
+
end
|
16
|
+
class << self
|
17
|
+
alias_method_chain :inherited, :except_scope_hook
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
|
24
|
+
# Must be a class method called directly on AR::Base subclasses
|
25
|
+
# so named_scope knows who its base class, and thus, table_name
|
26
|
+
# and primary_key are.
|
27
|
+
def attach_except_utility_scope
|
7
28
|
|
8
29
|
# Allow easy rejection of items.
|
9
30
|
# Can take an a single id or ActiveRecord object, or an array of them
|
@@ -14,8 +35,7 @@ module UtilityScopes
|
|
14
35
|
# nil or empty array causes issues here with mysql
|
15
36
|
item_or_list.blank? ? {} : {:conditions => ["#{quoted_table_name}.#{primary_key} NOT IN (?)", item_or_list]}
|
16
37
|
}
|
17
|
-
|
18
|
-
end
|
38
|
+
end
|
19
39
|
end
|
20
40
|
end
|
21
41
|
end
|
data/spec/except_spec.rb
CHANGED
@@ -5,8 +5,8 @@ describe "except scope" do
|
|
5
5
|
before do
|
6
6
|
uses_fixture(:article)
|
7
7
|
# Stub these so it doesn't try to hit the (non-existant) database
|
8
|
-
|
9
|
-
|
8
|
+
Article.stub!(:quoted_table_name).and_return("'articles'")
|
9
|
+
Article.stub!(:primary_key).and_return('id')
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should not change the scope with nil" do
|
@@ -15,11 +15,11 @@ describe "except scope" do
|
|
15
15
|
|
16
16
|
it "should return all items except the one specified" do
|
17
17
|
item = 123
|
18
|
-
Article.except(item).proxy_options.should == {:conditions => ['articles.id NOT IN (?)
|
18
|
+
Article.except(item).proxy_options.should == {:conditions => ["'articles'.id NOT IN (?)", item]}
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should return all items except ones in the list specified" do
|
22
22
|
list = [123, 456, 7]
|
23
|
-
Article.except(list).proxy_options.should == {:conditions => ['articles.id NOT IN (?)
|
23
|
+
Article.except(list).proxy_options.should == {:conditions => ["'articles'.id NOT IN (?)", list]}
|
24
24
|
end
|
25
25
|
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.2.
|
4
|
+
version: 0.2.1
|
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-09-08 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|