internuity-quick_scopes 0.1 → 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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Internuity Ltd, Andrew Timberlake <andrew@andrewtimberlake.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc CHANGED
@@ -6,7 +6,9 @@ These are especially useful for quick modifications to a query on an association
6
6
  The named scopes added are:
7
7
  * <tt>order</tt>
8
8
  * <tt>limit</tt>
9
- * <tt>with</tt>
9
+ * <tt>offset</tt>
10
+ * <tt>with</tt> - alias for :include
11
+ * <tt>where</tt> - alias for :conditions
10
12
 
11
13
  ==Examples
12
14
  With a User model having many Posts
@@ -18,8 +20,17 @@ With a User model having many Posts
18
20
 
19
21
  # Limit the results to a specific number
20
22
  user.posts.limit(5)
23
+
24
+ # Offset the results
25
+ user.posts.offset(5)
21
26
 
22
27
  # Include other associated models
23
28
  user.posts.with(:comments)
24
29
  user.posts.with(:comments, :author)
25
30
  user.posts.with({:comments => :author}, :author)
31
+
32
+ # Limit your results with conditions
33
+ user.posts.where(:published => true)
34
+
35
+ ==Note
36
+ Some of these don't promote best practices for your code but can be very useful when working with the console.
data/Rakefile CHANGED
@@ -31,7 +31,7 @@ end
31
31
 
32
32
  spec = Gem::Specification.new do |s|
33
33
  s.name = "quick_scopes"
34
- s.version = "0.1"
34
+ s.version = "0.2"
35
35
  s.author = "Andrew Timberlake"
36
36
  s.email = "andrew@andrewtimberlake.com"
37
37
  s.homepage = "http://www.internuity.net/projects/quick_scopes"
data/lib/quick_scopes.rb CHANGED
@@ -14,4 +14,8 @@ class ActiveRecord::Base
14
14
  named_scope :where, lambda { |*conditions|
15
15
  { :conditions => conditions.size == 1 ? conditions[0] : conditions }
16
16
  }
17
+
18
+ named_scope :offset, lambda { |offset|
19
+ { :offset => offset }
20
+ }
17
21
  end
@@ -17,6 +17,14 @@ class QuickScopesTest < Test::Unit::TestCase
17
17
  should "have a with scope" do
18
18
  assert Post.scopes.include?(:with)
19
19
  end
20
+
21
+ should "have a where scope" do
22
+ assert Post.scopes.include?(:where)
23
+ end
24
+
25
+ should "have a offset scope" do
26
+ assert Post.scopes.include?(:offset)
27
+ end
20
28
  end
21
29
  end
22
30
 
@@ -46,6 +54,21 @@ class QuickScopesTest < Test::Unit::TestCase
46
54
  end
47
55
  end
48
56
 
57
+ context "using the offset named_scope" do
58
+ setup {
59
+ @post = Post.first
60
+ }
61
+
62
+ should "return 2 comments when offset by 1" do
63
+ assert_equal 2, @post.comments.limit(3).offset(1).to_a.size
64
+ end
65
+
66
+ should "return 1 comments when offset by 2" do
67
+ #Not sure why I have to add to_a?
68
+ assert_equal 1, @post.comments.limit(3).offset(2).to_a.size
69
+ end
70
+ end
71
+
49
72
  context "using the where named_scope" do
50
73
  setup {
51
74
  @post = Post.first
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: internuity-quick_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Timberlake