scoped_associations 0.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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +12 -0
- data/Appraisals +8 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +14 -0
- data/gemfiles/activerecord3.gemfile +8 -0
- data/gemfiles/activerecord4.gemfile +8 -0
- data/lib/scoped_associations/activerecord3/has_many.rb +63 -0
- data/lib/scoped_associations/activerecord3/has_one.rb +64 -0
- data/lib/scoped_associations/activerecord4/has_many.rb +58 -0
- data/lib/scoped_associations/activerecord4/has_one.rb +58 -0
- data/lib/scoped_associations/version.rb +4 -0
- data/lib/scoped_associations.rb +10 -0
- data/scoped_associations.gemspec +31 -0
- data/spec/fixtures/image.png +0 -0
- data/spec/image_spec.rb +144 -0
- data/spec/spec_helper.rb +84 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a8d5ec22b7e01d1b6038c99e16c54f9b6d8ca0d
|
4
|
+
data.tar.gz: da77f72500f98858cf76d2828b698a5c0e573db3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 307392fd32c64f5e392f3617bb01dc1b821f49c8763dc3a95185c76328f4ee7d822c631ae9bfaf4bbbcb9065a9f6e6c3dc5a1f61af9fed20d0bd3c2920864c20
|
7
|
+
data.tar.gz: 9fe8e023f57dfe2bec1779d94f35eb4f50e895a8515643fe669cd168c81d7390a27f1aa73a6ac85d138f020f027075e5ea4ef1df329bd6f00091e63fa1c6959d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Stefano Verna
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# ScopedAssociations
|
2
|
+
|
3
|
+
[](https://travis-ci.org/stefanoverna/scoped_associations)
|
4
|
+
|
5
|
+
ScopedAssociations is able to create multiple `has_to` and `has_many`
|
6
|
+
associations between two ActiveRecord models.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'scoped_associations'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install scoped_associations
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
TODO: Write usage instructions here
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
33
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'appraisal'
|
4
|
+
|
5
|
+
desc 'Default: run specs'
|
6
|
+
task default: :spec
|
7
|
+
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.pattern = "spec/**/*_spec.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
Bundler::GemHelper.install_tasks
|
14
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ScopedAssociations
|
4
|
+
module ActiveRecord3
|
5
|
+
module HasMany
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do |klass|
|
9
|
+
klass.valid_options += [:scoped]
|
10
|
+
end
|
11
|
+
|
12
|
+
def build
|
13
|
+
reflection = super
|
14
|
+
extend_reflection(reflection)
|
15
|
+
reflection
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def extend_reflection(reflection)
|
21
|
+
if scoped?
|
22
|
+
reflection.extend ReflectionExtension
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def scoped?
|
27
|
+
options[:scoped]
|
28
|
+
end
|
29
|
+
|
30
|
+
module ReflectionExtension
|
31
|
+
def foreign_scope
|
32
|
+
if options[:as]
|
33
|
+
"#{options[:as]}_scope"
|
34
|
+
else
|
35
|
+
name = active_record.name
|
36
|
+
name.underscore.demodulize + "_scope"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def association_class
|
41
|
+
ScopedHasManyAssociation
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class ScopedHasManyAssociation < ActiveRecord::Associations::HasManyAssociation
|
46
|
+
def creation_attributes
|
47
|
+
attributes = super
|
48
|
+
attributes[reflection.foreign_scope] = reflection.name.to_s
|
49
|
+
attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
def association_scope
|
53
|
+
super.where(reflection.foreign_scope => reflection.name.to_s)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
ActiveRecord::Associations::Builder::HasMany.send :include, ScopedAssociations::ActiveRecord3::HasMany
|
62
|
+
|
63
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ScopedAssociations
|
4
|
+
module ActiveRecord3
|
5
|
+
module HasOne
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do |klass|
|
9
|
+
klass.valid_options += [:scoped]
|
10
|
+
end
|
11
|
+
|
12
|
+
def build
|
13
|
+
reflection = super
|
14
|
+
extend_reflection(reflection)
|
15
|
+
reflection
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def extend_reflection(reflection)
|
21
|
+
if scoped?
|
22
|
+
reflection.extend ReflectionExtension
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def scoped?
|
27
|
+
options[:scoped]
|
28
|
+
end
|
29
|
+
|
30
|
+
module ReflectionExtension
|
31
|
+
def foreign_scope
|
32
|
+
if options[:as]
|
33
|
+
"#{options[:as]}_scope"
|
34
|
+
else
|
35
|
+
name = active_record.name
|
36
|
+
name.underscore.demodulize + "_scope"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def association_class
|
41
|
+
ScopedHasOneAssociation
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class ScopedHasOneAssociation < ActiveRecord::Associations::HasOneAssociation
|
46
|
+
def creation_attributes
|
47
|
+
attributes = super
|
48
|
+
attributes[reflection.foreign_scope] = reflection.name.to_s
|
49
|
+
attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
def association_scope
|
53
|
+
super.where(reflection.foreign_scope => reflection.name.to_s)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
ActiveRecord::Associations::Builder::HasOne.send :include, ScopedAssociations::ActiveRecord3::HasOne
|
62
|
+
|
63
|
+
|
64
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ScopedAssociations
|
2
|
+
module ActiveRecord4
|
3
|
+
module HasMany
|
4
|
+
|
5
|
+
def valid_options
|
6
|
+
super + [:scoped]
|
7
|
+
end
|
8
|
+
|
9
|
+
def build
|
10
|
+
reflection = super
|
11
|
+
extend_reflection(reflection)
|
12
|
+
reflection
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def extend_reflection(reflection)
|
18
|
+
if scoped?
|
19
|
+
reflection.extend ReflectionExtension
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def scoped?
|
24
|
+
options[:scoped]
|
25
|
+
end
|
26
|
+
|
27
|
+
module ReflectionExtension
|
28
|
+
def foreign_scope
|
29
|
+
if options[:as]
|
30
|
+
"#{options[:as]}_scope"
|
31
|
+
else
|
32
|
+
name = active_record.name
|
33
|
+
name.underscore.demodulize + "_scope"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def association_class
|
38
|
+
ScopedHasManyAssociation
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ScopedHasManyAssociation < ActiveRecord::Associations::HasManyAssociation
|
43
|
+
def creation_attributes
|
44
|
+
attributes = super
|
45
|
+
attributes[reflection.foreign_scope] = reflection.name.to_s
|
46
|
+
attributes
|
47
|
+
end
|
48
|
+
|
49
|
+
def association_scope
|
50
|
+
super.where(reflection.foreign_scope => reflection.name.to_s)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveRecord::Associations::Builder::HasMany.send :include, ScopedAssociations::ActiveRecord4::HasMany
|
58
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ScopedAssociations
|
2
|
+
module ActiveRecord4
|
3
|
+
module HasOne
|
4
|
+
|
5
|
+
def valid_options
|
6
|
+
super + [:scoped]
|
7
|
+
end
|
8
|
+
|
9
|
+
def build
|
10
|
+
reflection = super
|
11
|
+
extend_reflection(reflection)
|
12
|
+
reflection
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def extend_reflection(reflection)
|
18
|
+
if scoped?
|
19
|
+
reflection.extend ReflectionExtension
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def scoped?
|
24
|
+
options[:scoped]
|
25
|
+
end
|
26
|
+
|
27
|
+
module ReflectionExtension
|
28
|
+
def foreign_scope
|
29
|
+
if options[:as]
|
30
|
+
"#{options[:as]}_scope"
|
31
|
+
else
|
32
|
+
name = active_record.name
|
33
|
+
name.underscore.demodulize + "_scope"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def association_class
|
38
|
+
ScopedHasOneAssociation
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ScopedHasOneAssociation < ActiveRecord::Associations::HasOneAssociation
|
43
|
+
def creation_attributes
|
44
|
+
attributes = super
|
45
|
+
attributes[reflection.foreign_scope] = reflection.name.to_s
|
46
|
+
attributes
|
47
|
+
end
|
48
|
+
|
49
|
+
def association_scope
|
50
|
+
super.where(reflection.foreign_scope => reflection.name.to_s)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
ActiveRecord::Associations::Builder::HasOne.send :include, ScopedAssociations::ActiveRecord4::HasOne
|
58
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
if defined?(ActiveRecord::Base)
|
2
|
+
if ActiveRecord::VERSION::MAJOR == 4
|
3
|
+
require 'scoped_associations/activerecord4/has_many'
|
4
|
+
require 'scoped_associations/activerecord4/has_one'
|
5
|
+
elsif ActiveRecord::VERSION::MAJOR == 3
|
6
|
+
require 'scoped_associations/activerecord3/has_many'
|
7
|
+
require 'scoped_associations/activerecord3/has_one'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scoped_associations/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'scoped_associations'
|
8
|
+
spec.version = ScopedAssociations::VERSION
|
9
|
+
spec.authors = ['Stefano Verna']
|
10
|
+
spec.email = ['stefano.verna@gmail.com']
|
11
|
+
spec.description = %q{ScopedAssociations lets you create multiple `has_to` and `has_many` associations between two ActiveRecord models.}
|
12
|
+
spec.summary = %q{Create multiple `has_to` and `has_many` associations between two ActiveRecord models}
|
13
|
+
spec.homepage = 'https://github.com/stefanoverna/scoped_associations'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'sqlite3'
|
25
|
+
|
26
|
+
spec.required_ruby_version = ">= 1.9.3"
|
27
|
+
|
28
|
+
spec.add_dependency 'activerecord', ['>= 3.2', '<= 4.0']
|
29
|
+
spec.add_dependency 'activesupport'
|
30
|
+
end
|
31
|
+
|
Binary file
|
data/spec/image_spec.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'ScopedAssociations' do
|
4
|
+
let(:post) { Post.new }
|
5
|
+
|
6
|
+
context "non polymorphic" do
|
7
|
+
describe "has_one" do
|
8
|
+
it "works with direct assignment" do
|
9
|
+
post.secondary_comment = Comment.new(comment: "foo")
|
10
|
+
post.save!
|
11
|
+
post.reload
|
12
|
+
|
13
|
+
expect(post.secondary_comment).to be_present
|
14
|
+
expect(post.primary_comment).to be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "works with nested attributes" do
|
18
|
+
post = Post.new(primary_comment_attributes: { comment: "foo" })
|
19
|
+
post.save!
|
20
|
+
post.reload
|
21
|
+
|
22
|
+
expect(post.primary_comment).to be_present
|
23
|
+
expect(post.secondary_comment).to be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "works with .build" do
|
27
|
+
post.build_secondary_comment(comment: "foo")
|
28
|
+
post.save!
|
29
|
+
post.reload
|
30
|
+
|
31
|
+
expect(post.secondary_comment).to be_present
|
32
|
+
expect(post.primary_comment).to be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "has_many" do
|
37
|
+
it "works with direct assignment" do
|
38
|
+
post.secondary_comments = [ Comment.new(comment: "foo") ]
|
39
|
+
post.save!
|
40
|
+
|
41
|
+
post.reload
|
42
|
+
expect(post.secondary_comments.size).to eq 1
|
43
|
+
expect(post.primary_comments.size).to eq 0
|
44
|
+
end
|
45
|
+
|
46
|
+
it "works with <<" do
|
47
|
+
post.save
|
48
|
+
post.secondary_comments << Comment.new(comment: "foo")
|
49
|
+
|
50
|
+
post.reload
|
51
|
+
expect(post.secondary_comments.size).to eq 1
|
52
|
+
expect(post.primary_comments.size).to eq 0
|
53
|
+
end
|
54
|
+
|
55
|
+
it "works with .build" do
|
56
|
+
post.secondary_comments.build(comment: "foo")
|
57
|
+
post.save
|
58
|
+
|
59
|
+
post.reload
|
60
|
+
expect(post.secondary_comments.size).to eq 1
|
61
|
+
expect(post.primary_comments.size).to eq 0
|
62
|
+
end
|
63
|
+
|
64
|
+
it "works with nested attributes" do
|
65
|
+
post = Post.new(primary_comments_attributes: [ { comment: "foo" } ])
|
66
|
+
post.save!
|
67
|
+
post.reload
|
68
|
+
|
69
|
+
expect(post.primary_comments.size).to eq 1
|
70
|
+
expect(post.secondary_comments.size).to eq 0
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "polymorphic" do
|
76
|
+
describe "has_one" do
|
77
|
+
it "works with direct assignment" do
|
78
|
+
post.secondary_tag = Tag.new(name: "foo")
|
79
|
+
post.save!
|
80
|
+
post.reload
|
81
|
+
|
82
|
+
expect(post.secondary_tag).to be_present
|
83
|
+
expect(post.primary_tag).to be_nil
|
84
|
+
end
|
85
|
+
|
86
|
+
it "works with nested attributes" do
|
87
|
+
post = Post.new(primary_tag_attributes: { name: "foo" })
|
88
|
+
post.save!
|
89
|
+
post.reload
|
90
|
+
|
91
|
+
expect(post.primary_tag).to be_present
|
92
|
+
expect(post.secondary_tag).to be_nil
|
93
|
+
end
|
94
|
+
|
95
|
+
it "works with .build" do
|
96
|
+
post.build_secondary_tag(name: "foo")
|
97
|
+
post.save!
|
98
|
+
post.reload
|
99
|
+
|
100
|
+
expect(post.secondary_tag).to be_present
|
101
|
+
expect(post.primary_tag).to be_nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "has_many" do
|
106
|
+
it "works with direct assignment" do
|
107
|
+
post.secondary_tags = [ Tag.new(name: "foo") ]
|
108
|
+
post.save!
|
109
|
+
|
110
|
+
post.reload
|
111
|
+
expect(post.secondary_tags.size).to eq 1
|
112
|
+
expect(post.primary_tags.size).to eq 0
|
113
|
+
end
|
114
|
+
|
115
|
+
it "works with <<" do
|
116
|
+
post.save
|
117
|
+
post.secondary_tags << Tag.new(name: "foo")
|
118
|
+
|
119
|
+
post.reload
|
120
|
+
expect(post.secondary_tags.size).to eq 1
|
121
|
+
expect(post.primary_tags.size).to eq 0
|
122
|
+
end
|
123
|
+
|
124
|
+
it "works with .build" do
|
125
|
+
post.secondary_tags.build(name: "foo")
|
126
|
+
post.save
|
127
|
+
|
128
|
+
post.reload
|
129
|
+
expect(post.secondary_tags.size).to eq 1
|
130
|
+
expect(post.primary_tags.size).to eq 0
|
131
|
+
end
|
132
|
+
|
133
|
+
it "works with nested attributes" do
|
134
|
+
post = Post.new(primary_tags_attributes: [ { name: "foo" } ])
|
135
|
+
post.save!
|
136
|
+
post.reload
|
137
|
+
|
138
|
+
expect(post.primary_tags.size).to eq 1
|
139
|
+
expect(post.secondary_tags.size).to eq 0
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'active_record'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
ROOT = File.expand_path('../', __FILE__)
|
7
|
+
|
8
|
+
require 'scoped_associations'
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(
|
11
|
+
adapter: "sqlite3",
|
12
|
+
database: ":memory:"
|
13
|
+
)
|
14
|
+
|
15
|
+
ActiveRecord::Migration.verbose = false
|
16
|
+
|
17
|
+
ActiveRecord::Schema.define do
|
18
|
+
create_table :tags do |t|
|
19
|
+
t.integer :owner_id, null: false
|
20
|
+
t.string :owner_type, null: false
|
21
|
+
t.string :owner_scope, null: false
|
22
|
+
t.string :name, null: false
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :posts do |t|
|
26
|
+
t.string :title
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table :comments do |t|
|
30
|
+
t.integer :post_id, null: false
|
31
|
+
t.string :post_scope, null: false
|
32
|
+
t.string :comment, null: false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# ActiveRecord::Base.logger = Logger.new(STDOUT)
|
37
|
+
|
38
|
+
class Comment < ActiveRecord::Base
|
39
|
+
belongs_to :post
|
40
|
+
end
|
41
|
+
|
42
|
+
class Tag < ActiveRecord::Base
|
43
|
+
belongs_to :owner, polymorphic: true
|
44
|
+
end
|
45
|
+
|
46
|
+
class Post < ActiveRecord::Base
|
47
|
+
has_one :primary_tag, as: :owner,
|
48
|
+
class_name: "Tag",
|
49
|
+
scoped: true
|
50
|
+
|
51
|
+
accepts_nested_attributes_for :primary_tag
|
52
|
+
|
53
|
+
has_one :secondary_tag, as: :owner,
|
54
|
+
class_name: "Tag",
|
55
|
+
scoped: true
|
56
|
+
|
57
|
+
has_many :primary_tags, as: :owner,
|
58
|
+
class_name: "Tag",
|
59
|
+
scoped: true
|
60
|
+
|
61
|
+
accepts_nested_attributes_for :primary_tags
|
62
|
+
|
63
|
+
has_many :secondary_tags, as: :owner,
|
64
|
+
class_name: "Tag",
|
65
|
+
scoped: true
|
66
|
+
|
67
|
+
|
68
|
+
has_one :primary_comment, class_name: "Comment",
|
69
|
+
scoped: true
|
70
|
+
|
71
|
+
accepts_nested_attributes_for :primary_comment
|
72
|
+
|
73
|
+
has_one :secondary_comment, class_name: "Comment",
|
74
|
+
scoped: true
|
75
|
+
|
76
|
+
has_many :primary_comments, class_name: "Comment",
|
77
|
+
scoped: true
|
78
|
+
|
79
|
+
accepts_nested_attributes_for :primary_comments
|
80
|
+
|
81
|
+
has_many :secondary_comments, class_name: "Comment",
|
82
|
+
scoped: true
|
83
|
+
end
|
84
|
+
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scoped_associations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefano Verna
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.2'
|
76
|
+
- - <=
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '4.0'
|
79
|
+
type: :runtime
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.2'
|
86
|
+
- - <=
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '4.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: activesupport
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :runtime
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
description: ScopedAssociations lets you create multiple `has_to` and `has_many` associations
|
104
|
+
between two ActiveRecord models.
|
105
|
+
email:
|
106
|
+
- stefano.verna@gmail.com
|
107
|
+
executables: []
|
108
|
+
extensions: []
|
109
|
+
extra_rdoc_files: []
|
110
|
+
files:
|
111
|
+
- .gitignore
|
112
|
+
- .travis.yml
|
113
|
+
- Appraisals
|
114
|
+
- Gemfile
|
115
|
+
- LICENSE.txt
|
116
|
+
- README.md
|
117
|
+
- Rakefile
|
118
|
+
- gemfiles/activerecord3.gemfile
|
119
|
+
- gemfiles/activerecord4.gemfile
|
120
|
+
- lib/scoped_associations.rb
|
121
|
+
- lib/scoped_associations/activerecord3/has_many.rb
|
122
|
+
- lib/scoped_associations/activerecord3/has_one.rb
|
123
|
+
- lib/scoped_associations/activerecord4/has_many.rb
|
124
|
+
- lib/scoped_associations/activerecord4/has_one.rb
|
125
|
+
- lib/scoped_associations/version.rb
|
126
|
+
- scoped_associations.gemspec
|
127
|
+
- spec/fixtures/image.png
|
128
|
+
- spec/image_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
homepage: https://github.com/stefanoverna/scoped_associations
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 1.9.3
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 2.0.0
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Create multiple `has_to` and `has_many` associations between two ActiveRecord
|
154
|
+
models
|
155
|
+
test_files:
|
156
|
+
- spec/fixtures/image.png
|
157
|
+
- spec/image_spec.rb
|
158
|
+
- spec/spec_helper.rb
|