scopes_for_associations 0.1.0

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.
File without changes
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .*.sw*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.8.7-p249@scopes_for_associations
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in scopes_for_associations.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ = Scopes for Associations
2
+
3
+ == Description
4
+
5
+ Define very basic, commonly used scopes for ActiveRecord associations.
6
+
7
+ == Description through code
8
+
9
+ class Movie < ActiveRecord::Base
10
+ belongs_to :director
11
+ scopes_for_associations
12
+ end
13
+
14
+ Movie.for_director(Director.first)
15
+ #=> [#<Movie ..., director_id: 1>]
16
+ Movie.for(Director.first)
17
+ #=> [#<Movie ..., director_id: 1>]
18
+
19
+ == Limitations
20
+
21
+ As of now, the gem only defines scopes for non-polymorphic belongs_to associations.
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+
6
+ desc 'Run rspec tests'
7
+ task :test do
8
+ sh 'rspec spec/'
9
+ end
@@ -0,0 +1,24 @@
1
+ module ScopesForAssociations
2
+
3
+ def scopes_for_associations()
4
+ # scopes for belongs_to associations.
5
+ belongs_to_reflections = reflect_on_all_associations(:belongs_to)
6
+ non_polymorphic_belongs_to_reflections = belongs_to_reflections.reject { |reflection| reflection.options[:polymorphic] }
7
+ non_polymorphic_belongs_to_reflections.each do |reflection|
8
+ name = reflection.name
9
+ scope :"for_#{name}", proc { |obj| where(:"#{name}_id" => obj) }
10
+ end
11
+ # scopes for has_many associations.
12
+ #has_many_reflections = reflect_on_all_associations(:has_many)
13
+ #non_through_has_many_reflections = has_many_reflections.reject { |reflection| reflection.options[:through] }
14
+ #non_through_has_many_reflections.each do |reflection|
15
+ #name = reflection.name
16
+ #scope :"for_#{name}", proc { |obj| joins(name).where(name => {:"#{self.class.table_name.singularize}_id" => obj.id}) }
17
+ #end
18
+ # for scope.
19
+ scope :for, proc { |obj| send("for_#{obj.class.name.underscore}", obj) }
20
+ end
21
+
22
+ end
23
+
24
+ ActiveRecord::Base.extend ScopesForAssociations
@@ -0,0 +1,3 @@
1
+ module ScopesForAssociations
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'scopes_for_associations/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'scopes_for_associations'
7
+ s.version = ScopesForAssociations::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Jared Ning']
10
+ s.email = ['jared@redningja.com']
11
+ s.homepage = 'https://github.com/ordinaryzelig/scopes_for_associations'
12
+ s.summary = %q{Define very basic, commonly used scopes for ActiveRecord associations}
13
+ s.description = %q{Define very basic, commonly used scopes for ActiveRecord associations. E.g. if a Post model has an author and a category association, scopes will be defined like Post.for(author) or Post.for(category).}
14
+
15
+ s.rubyforge_project = 'scopes-for-associations'
16
+
17
+ s.files = `git ls-files`.split($/)
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split($/)
19
+ s.executables = `git ls-files -- bin/*`.split($/).map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
+
22
+ s.add_development_dependency 'rspec', '2.5.0'
23
+ s.add_development_dependency 'activerecord', '>= 3.0.0'
24
+ s.add_development_dependency 'sqlite3-ruby', '1.2.5'
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe ScopesForAssociations do
4
+
5
+ let(:director) { Director.create! }
6
+ let(:production_company) { ProductionCompany.create! }
7
+
8
+ it 'should be included in ActiveRecord::Base' do
9
+ ActiveRecord::Base.should respond_to('scopes_for_associations')
10
+ end
11
+
12
+ it 'should define for_x scopes' do
13
+ Movie.should respond_to('for_director')
14
+ Movie.should respond_to('for_production_company')
15
+ end
16
+
17
+ it 'should scope for association as object' do
18
+ Movie.for_director(director).to_sql.should eq(Movie.where(:director_id => director.id).to_sql)
19
+ Movie.for_production_company(production_company).to_sql.should eq(Movie.where(:production_company_id => production_company.id).to_sql)
20
+ end
21
+
22
+ it 'should have a "polymorphic" scope :for' do
23
+ Movie.for(director).to_sql.should eq(Movie.for_director(director).to_sql)
24
+ Movie.for(production_company).to_sql.should eq(Movie.for_production_company(production_company).to_sql)
25
+ end
26
+
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'pathname'
2
+ require 'active_record'
3
+
4
+ require File.join(Pathname(__FILE__).dirname.expand_path, '../lib/scopes_for_associations')
5
+
6
+ # require support .rb files.
7
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f}
8
+
9
+ #RSpec.configure do |config|
10
+ #config.include(Macros)
11
+ #end
@@ -0,0 +1,22 @@
1
+ class Movie < ActiveRecord::Base
2
+
3
+ belongs_to :director
4
+ belongs_to :production_company
5
+ has_one :trailer
6
+
7
+ scopes_for_associations
8
+
9
+ end
10
+
11
+ class Director < ActiveRecord::Base
12
+ end
13
+
14
+ class ProductionCompany < ActiveRecord::Base
15
+ end
16
+
17
+ class Trailer < ActiveRecord::Base
18
+ belongs_to :movie
19
+ belongs_to :director
20
+ belongs_to :production_company
21
+ scopes_for_associations
22
+ end
@@ -0,0 +1,16 @@
1
+ require 'sqlite3'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+
5
+ ActiveRecord::Schema.define do
6
+ create_table :movies, :force => true do |t|
7
+ t.belongs_to :director
8
+ t.belongs_to :production_company
9
+ end
10
+ create_table(:directors, :force => true)# { |t| }
11
+ create_table(:production_companies, :force => true) { |t| }
12
+ create_table :trailers, :force => true do |t|
13
+ t.belongs_to :director
14
+ t.belongs_to :production_company
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scopes_for_associations
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jared Ning
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-08 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 2
32
+ - 5
33
+ - 0
34
+ version: 2.5.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activerecord
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 3
48
+ - 0
49
+ - 0
50
+ version: 3.0.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: sqlite3-ruby
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ hash: 21
62
+ segments:
63
+ - 1
64
+ - 2
65
+ - 5
66
+ version: 1.2.5
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: Define very basic, commonly used scopes for ActiveRecord associations. E.g. if a Post model has an author and a category association, scopes will be defined like Post.for(author) or Post.for(category).
70
+ email:
71
+ - jared@redningja.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gemtest
80
+ - .gitignore
81
+ - .rvmrc
82
+ - Gemfile
83
+ - README.rdoc
84
+ - Rakefile
85
+ - lib/scopes_for_associations.rb
86
+ - lib/scopes_for_associations/version.rb
87
+ - scopes_for_associations.gemspec
88
+ - spec/scopes_for_associations_spec.rb
89
+ - spec/spec_helper.rb
90
+ - spec/support/models.rb
91
+ - spec/support/schema.rb
92
+ has_rdoc: true
93
+ homepage: https://github.com/ordinaryzelig/scopes_for_associations
94
+ licenses: []
95
+
96
+ post_install_message:
97
+ rdoc_options: []
98
+
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: scopes-for-associations
122
+ rubygems_version: 1.5.0
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Define very basic, commonly used scopes for ActiveRecord associations
126
+ test_files:
127
+ - spec/scopes_for_associations_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/models.rb
130
+ - spec/support/schema.rb