internuity-quick_scopes 0.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/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ A Rails plugin to automatically add some quick named_scopes to your models.
2
+
3
+ These are especially useful for quick modifications to a query on an association.
4
+
5
+ ==The named_scopes
6
+ The named scopes added are:
7
+ * <tt>order</tt>
8
+ * <tt>limit</tt>
9
+ * <tt>with</tt>
10
+
11
+ ==Examples
12
+ With a User model having many Posts
13
+ # Standard association method to retrieve all posts
14
+ user.posts
15
+
16
+ # Order the posts
17
+ user.posts.order('created_at')
18
+
19
+ # Limit the results to a specific number
20
+ user.posts.limit(5)
21
+
22
+ # Include other associated models
23
+ user.posts.with(:comments)
24
+ user.posts.with(:comments, :author)
25
+ user.posts.with({:comments => :author}, :author)
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => [:clean, :test]
8
+
9
+ desc 'Run tests'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/test_*.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'doc'
19
+ rdoc.title = 'Quick Scopes'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README*')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ desc 'Clean up files.'
26
+ task :clean do |t|
27
+ FileUtils.rm_rf "doc"
28
+ FileUtils.rm_rf "tmp"
29
+ FileUtils.rm_rf "pkg"
30
+ end
31
+
32
+ spec = Gem::Specification.new do |s|
33
+ s.name = "quick_scopes"
34
+ s.version = "0.1"
35
+ s.author = "Andrew Timberlake"
36
+ s.email = "andrew@andrewtimberlake.com"
37
+ s.homepage = "http://www.internuity.net/projects/quick_scopes"
38
+ s.platform = Gem::Platform::RUBY
39
+ s.summary = "Rails plugin to add some useful named_scopes to your models"
40
+ s.files = FileList["README*",
41
+ "LICENSE",
42
+ "Rakefile",
43
+ "{lib,test}/**/*"].to_a
44
+ s.require_path = "lib"
45
+ s.test_files = FileList["test/**/test_*.rb"].to_a
46
+ s.has_rdoc = true
47
+ s.extra_rdoc_files = FileList["README*"].to_a
48
+ s.rdoc_options << '--line-numbers' << '--inline-source'
49
+ s.add_development_dependency 'thoughtbot-shoulda'
50
+ end
51
+
52
+ desc "Generate a gemspec file for GitHub"
53
+ task :gemspec do
54
+ File.open("#{spec.name}.gemspec", 'w') do |f|
55
+ f.write spec.to_ruby
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ class ActiveRecord::Base
2
+ named_scope :order, lambda { |order|
3
+ { :order => order }
4
+ }
5
+
6
+ named_scope :limit, lambda { |limit|
7
+ { :limit => limit }
8
+ }
9
+
10
+ named_scope :with, lambda { |*include|
11
+ { :include => include.size == 1 ? include[0] : include }
12
+ }
13
+
14
+ named_scope :where, lambda { |*conditions|
15
+ { :conditions => conditions.size == 1 ? conditions[0] : conditions }
16
+ }
17
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+ require 'active_record'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'quick_scopes')
9
+ require File.join(File.dirname(__FILE__), 'models')
10
+
11
+ TEST_DB = File.join(File.dirname(__FILE__), 'test.sqlite3')
12
+
13
+ def setup_database
14
+ File.unlink(TEST_DB) if File.exist?(TEST_DB)
15
+ ActiveRecord::Base.establish_connection(
16
+ 'adapter' => 'sqlite3', 'timeout' => 5000, 'database' => TEST_DB
17
+ )
18
+ create_tables
19
+ load_fixtures
20
+ end
21
+
22
+ def create_tables
23
+ c = ActiveRecord::Base.connection
24
+
25
+ c.create_table :authors, :force => true do |t|
26
+ t.string :name
27
+ t.timestamps
28
+ end
29
+
30
+ c.create_table :posts, :force => true do |t|
31
+ t.string :title
32
+ t.integer :author_id
33
+ t.timestamps
34
+ end
35
+
36
+ c.create_table :comments, :force => true do |t|
37
+ t.string :comment
38
+ t.integer :author_id
39
+ t.integer :post_id
40
+ t.timestamps
41
+ end
42
+ end
43
+
44
+ def load_fixtures
45
+ john = Author.create(:name => 'John')
46
+ jane = Author.create(:name => 'Jane')
47
+ bruce = Author.create(:name => 'Bruce')
48
+ shiela = Author.create(:name => 'Shiela')
49
+
50
+ john_post1 = Post.create(:title => 'Post 1', :author => john, :created_at => Time.now - 3.days)
51
+ john_post2 = Post.create(:title => 'Post 2', :author => john, :created_at => Time.now - 1.day)
52
+
53
+ jane_post1 = Post.create(:title => 'Post 3', :author => jane, :created_at => Time.now - 3.days)
54
+ jane_post2 = Post.create(:title => 'Post 4', :author => jane, :created_at => Time.now - 1.day)
55
+
56
+ comment1 = john_post1.comments.create(:comment => 'A comment on post 1 by jane', :author => jane)
57
+ comment2 = john_post1.comments.create(:comment => 'A comment on post 1 by bruce', :author => bruce)
58
+ comment3 = john_post1.comments.create(:comment => 'A comment on post 1 by shiela', :author => shiela)
59
+
60
+ comment4 = john_post2.comments.create(:comment => 'A comment on post 2 by jane', :author => jane)
61
+ comment5 = john_post2.comments.create(:comment => 'A comment on post 2 by bruce', :author => bruce)
62
+ comment6 = john_post2.comments.create(:comment => 'A comment on post 2 by shiela', :author => shiela)
63
+
64
+ comment7 = jane_post1.comments.create(:comment => 'A comment on post 3 by john', :author => john)
65
+ comment8 = jane_post1.comments.create(:comment => 'A comment on post 3 by bruce', :author => bruce)
66
+ comment9 = jane_post1.comments.create(:comment => 'A comment on post 3 by shiela', :author => shiela)
67
+
68
+ comment10 = jane_post2.comments.create(:comment => 'A comment on post 4 by john', :author => john)
69
+ comment11 = jane_post2.comments.create(:comment => 'A comment on post 4 by bruce', :author => bruce)
70
+ comment12 = jane_post2.comments.create(:comment => 'A comment on post 4 by shiela', :author => shiela)
71
+ end
72
+
73
+ setup_database
data/test/models.rb ADDED
@@ -0,0 +1,14 @@
1
+ class Author < ActiveRecord::Base
2
+ has_many :posts
3
+ has_many :comments
4
+ end
5
+
6
+ class Post < ActiveRecord::Base
7
+ has_many :comments
8
+ belongs_to :author
9
+ end
10
+
11
+ class Comment < ActiveRecord::Base
12
+ belongs_to :post
13
+ belongs_to :author
14
+ end
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), 'helper.rb')
2
+
3
+ class QuickScopesTest < Test::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ [Post, Author, Comment].each do |model|
8
+ context "The #{model} model" do
9
+ should "have an order scope" do
10
+ assert Post.scopes.include?(:order)
11
+ end
12
+
13
+ should "have a limit scope" do
14
+ assert Post.scopes.include?(:limit)
15
+ end
16
+
17
+ should "have a with scope" do
18
+ assert Post.scopes.include?(:with)
19
+ end
20
+ end
21
+ end
22
+
23
+ context "using the order named_scope" do
24
+ setup {
25
+ john = Author.find_by_name('John')
26
+ @posts = john.posts.order('created_at desc')
27
+ }
28
+
29
+ should "return results in the correct order" do
30
+ assert_equal 'Post 2', @posts[0].title
31
+ end
32
+ end
33
+
34
+ context "using the limit named_scope" do
35
+ setup {
36
+ @post = Post.first
37
+ }
38
+
39
+ should "return 3 comments when unlimited" do
40
+ assert_equal 3, @post.comments.size
41
+ end
42
+
43
+ should "return 2 comments when limited to 2" do
44
+ #Not sure why I have to add to_a?
45
+ assert_equal 2, @post.comments.limit(2).to_a.size
46
+ end
47
+ end
48
+
49
+ context "using the where named_scope" do
50
+ setup {
51
+ @post = Post.first
52
+ }
53
+
54
+ should "restrict the results" do
55
+ assert_equal 1, @post.comments.where(:author_id => Author.find_by_name('Jane').id).to_a.size
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: internuity-quick_scopes
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Timberlake
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-27 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description:
25
+ email: andrew@andrewtimberlake.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ files:
33
+ - README.rdoc
34
+ - LICENSE
35
+ - Rakefile
36
+ - lib/quick_scopes.rb
37
+ - test/helper.rb
38
+ - test/models.rb
39
+ - test/test.sqlite3
40
+ - test/test_quick_scopes.rb
41
+ has_rdoc: true
42
+ homepage: http://www.internuity.net/projects/quick_scopes
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Rails plugin to add some useful named_scopes to your models
68
+ test_files:
69
+ - test/test_quick_scopes.rb