active_record_inherit_assoc 0.0.6
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/.gitignore +6 -0
- data/.travis.yml +7 -0
- data/Appraisals +7 -0
- data/Gemfile +15 -0
- data/MIT-LICENSE +20 -0
- data/README.md +26 -0
- data/Rakefile +14 -0
- data/active_record_inherit_assoc.gemspec +13 -0
- data/gemfiles/rails2.3.gemfile +17 -0
- data/gemfiles/rails2.3.gemfile.lock +80 -0
- data/gemfiles/rails3.0.gemfile +17 -0
- data/gemfiles/rails3.0.gemfile.lock +125 -0
- data/lib/active_record_inherit_assoc.rb +103 -0
- data/test/helper.rb +14 -0
- data/test/schema.rb +28 -0
- data/test/test_belongs_to_association.rb +48 -0
- data/test/test_inherit_assoc.rb +99 -0
- metadata +85 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem 'appraisal'
|
6
|
+
gem 'rake'
|
7
|
+
gem 'rails'
|
8
|
+
gem 'bundler'
|
9
|
+
gem 'sqlite3'
|
10
|
+
gem 'shoulda'
|
11
|
+
gem 'jeweler'
|
12
|
+
gem 'ruby-debug', :platforms => :ruby_18
|
13
|
+
gem 'debugger', :platforms => :ruby_19
|
14
|
+
gem 'test-unit', '>=2.5.1'
|
15
|
+
gem 'activerecord', :require => 'active_record'
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Zendesk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# ActiveRecord association inheritance
|
2
|
+
|
3
|
+
Makes the model inherit the specified attribute from a named association.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
gem install active_record_inherit_assoc
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
# parent_name - The Symbol name of the parent association.
|
12
|
+
# options - The Hash options to use:
|
13
|
+
# :attr - A Symbol or an Array of Symbol names of the attributes
|
14
|
+
# that should be inherited from the parent association.
|
15
|
+
#
|
16
|
+
class Post < ActiveRecord::Base
|
17
|
+
belongs_to :category
|
18
|
+
inherits_from :category, :attr => :account
|
19
|
+
end
|
20
|
+
|
21
|
+
## Copyright
|
22
|
+
|
23
|
+
Copyright (c) 2011 Zendesk. See MIT-LICENSE for details.
|
24
|
+
|
25
|
+
## Author
|
26
|
+
Ben Osheroff <ben@gimbo.net>
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'appraisal'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |test|
|
7
|
+
test.libs << 'lib' << 'test'
|
8
|
+
test.pattern = 'test/test*.rb'
|
9
|
+
test.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default do
|
13
|
+
sh "bundle exec rake appraisal:install && bundle exec rake appraisal test"
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
2
|
+
name = "active_record_inherit_assoc"
|
3
|
+
|
4
|
+
Gem::Specification.new name, "0.0.6" do |s|
|
5
|
+
s.summary = "Attribute inheritance for AR associations"
|
6
|
+
s.authors = ["Ben Osheroff"]
|
7
|
+
s.email = ["ben@gimbo.net"]
|
8
|
+
s.files = `git ls-files`.split("\n")
|
9
|
+
s.license = "MIT"
|
10
|
+
s.homepage = "http://github.com/zendesk/#{name}"
|
11
|
+
s.add_runtime_dependency "activerecord"
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source :rubygems
|
4
|
+
|
5
|
+
gem "appraisal"
|
6
|
+
gem "rake"
|
7
|
+
gem "rails"
|
8
|
+
gem "bundler"
|
9
|
+
gem "sqlite3"
|
10
|
+
gem "shoulda"
|
11
|
+
gem "jeweler"
|
12
|
+
gem "ruby-debug", :platforms=>:ruby_18
|
13
|
+
gem "debugger", :platforms=>:ruby_19
|
14
|
+
gem "test-unit", ">=2.5.1"
|
15
|
+
gem "activerecord", "2.3.14", :require=>"active_record"
|
16
|
+
|
17
|
+
gemspec :path=>"../"
|
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/active_record_inherit_assoc
|
3
|
+
specs:
|
4
|
+
active_record_inherit_assoc (0.0.5)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (2.3.14)
|
11
|
+
actionpack (= 2.3.14)
|
12
|
+
actionpack (2.3.14)
|
13
|
+
activesupport (= 2.3.14)
|
14
|
+
rack (~> 1.1.0)
|
15
|
+
activerecord (2.3.14)
|
16
|
+
activesupport (= 2.3.14)
|
17
|
+
activeresource (2.3.14)
|
18
|
+
activesupport (= 2.3.14)
|
19
|
+
activesupport (2.3.14)
|
20
|
+
appraisal (0.4.1)
|
21
|
+
bundler
|
22
|
+
rake
|
23
|
+
columnize (0.3.6)
|
24
|
+
debugger (1.1.4)
|
25
|
+
columnize (>= 0.3.1)
|
26
|
+
debugger-linecache (~> 1.1.1)
|
27
|
+
debugger-ruby_core_source (~> 1.1.3)
|
28
|
+
debugger-linecache (1.1.2)
|
29
|
+
debugger-ruby_core_source (>= 1.1.1)
|
30
|
+
debugger-ruby_core_source (1.1.3)
|
31
|
+
git (1.2.5)
|
32
|
+
jeweler (1.8.4)
|
33
|
+
bundler (~> 1.0)
|
34
|
+
git (>= 1.2.5)
|
35
|
+
rake
|
36
|
+
rdoc
|
37
|
+
json (1.7.3)
|
38
|
+
linecache (0.46)
|
39
|
+
rbx-require-relative (> 0.0.4)
|
40
|
+
rack (1.1.3)
|
41
|
+
rails (2.3.14)
|
42
|
+
actionmailer (= 2.3.14)
|
43
|
+
actionpack (= 2.3.14)
|
44
|
+
activerecord (= 2.3.14)
|
45
|
+
activeresource (= 2.3.14)
|
46
|
+
activesupport (= 2.3.14)
|
47
|
+
rake (>= 0.8.3)
|
48
|
+
rake (0.9.2.2)
|
49
|
+
rbx-require-relative (0.0.9)
|
50
|
+
rdoc (3.12)
|
51
|
+
json (~> 1.4)
|
52
|
+
ruby-debug (0.10.4)
|
53
|
+
columnize (>= 0.1)
|
54
|
+
ruby-debug-base (~> 0.10.4.0)
|
55
|
+
ruby-debug-base (0.10.4)
|
56
|
+
linecache (>= 0.3)
|
57
|
+
shoulda (3.0.1)
|
58
|
+
shoulda-context (~> 1.0.0)
|
59
|
+
shoulda-matchers (~> 1.0.0)
|
60
|
+
shoulda-context (1.0.0)
|
61
|
+
shoulda-matchers (1.0.0)
|
62
|
+
sqlite3 (1.3.6)
|
63
|
+
test-unit (2.5.1)
|
64
|
+
|
65
|
+
PLATFORMS
|
66
|
+
ruby
|
67
|
+
|
68
|
+
DEPENDENCIES
|
69
|
+
active_record_inherit_assoc!
|
70
|
+
activerecord (= 2.3.14)
|
71
|
+
appraisal
|
72
|
+
bundler
|
73
|
+
debugger
|
74
|
+
jeweler
|
75
|
+
rails
|
76
|
+
rake
|
77
|
+
ruby-debug
|
78
|
+
shoulda
|
79
|
+
sqlite3
|
80
|
+
test-unit (>= 2.5.1)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source :rubygems
|
4
|
+
|
5
|
+
gem "appraisal"
|
6
|
+
gem "rake"
|
7
|
+
gem "rails"
|
8
|
+
gem "bundler"
|
9
|
+
gem "sqlite3"
|
10
|
+
gem "shoulda"
|
11
|
+
gem "jeweler"
|
12
|
+
gem "ruby-debug", :platforms=>:ruby_18
|
13
|
+
gem "debugger", :platforms=>:ruby_19
|
14
|
+
gem "test-unit", ">=2.5.1"
|
15
|
+
gem "activerecord", "3.0.15", :require=>"active_record"
|
16
|
+
|
17
|
+
gemspec :path=>"../"
|
@@ -0,0 +1,125 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/active_record_inherit_assoc
|
3
|
+
specs:
|
4
|
+
active_record_inherit_assoc (0.0.5)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
abstract (1.0.0)
|
11
|
+
actionmailer (3.0.15)
|
12
|
+
actionpack (= 3.0.15)
|
13
|
+
mail (~> 2.2.19)
|
14
|
+
actionpack (3.0.15)
|
15
|
+
activemodel (= 3.0.15)
|
16
|
+
activesupport (= 3.0.15)
|
17
|
+
builder (~> 2.1.2)
|
18
|
+
erubis (~> 2.6.6)
|
19
|
+
i18n (~> 0.5.0)
|
20
|
+
rack (~> 1.2.5)
|
21
|
+
rack-mount (~> 0.6.14)
|
22
|
+
rack-test (~> 0.5.7)
|
23
|
+
tzinfo (~> 0.3.23)
|
24
|
+
activemodel (3.0.15)
|
25
|
+
activesupport (= 3.0.15)
|
26
|
+
builder (~> 2.1.2)
|
27
|
+
i18n (~> 0.5.0)
|
28
|
+
activerecord (3.0.15)
|
29
|
+
activemodel (= 3.0.15)
|
30
|
+
activesupport (= 3.0.15)
|
31
|
+
arel (~> 2.0.10)
|
32
|
+
tzinfo (~> 0.3.23)
|
33
|
+
activeresource (3.0.15)
|
34
|
+
activemodel (= 3.0.15)
|
35
|
+
activesupport (= 3.0.15)
|
36
|
+
activesupport (3.0.15)
|
37
|
+
appraisal (0.4.1)
|
38
|
+
bundler
|
39
|
+
rake
|
40
|
+
arel (2.0.10)
|
41
|
+
builder (2.1.2)
|
42
|
+
columnize (0.3.6)
|
43
|
+
debugger (1.1.4)
|
44
|
+
columnize (>= 0.3.1)
|
45
|
+
debugger-linecache (~> 1.1.1)
|
46
|
+
debugger-ruby_core_source (~> 1.1.3)
|
47
|
+
debugger-linecache (1.1.2)
|
48
|
+
debugger-ruby_core_source (>= 1.1.1)
|
49
|
+
debugger-ruby_core_source (1.1.3)
|
50
|
+
erubis (2.6.6)
|
51
|
+
abstract (>= 1.0.0)
|
52
|
+
git (1.2.5)
|
53
|
+
i18n (0.5.0)
|
54
|
+
jeweler (1.8.4)
|
55
|
+
bundler (~> 1.0)
|
56
|
+
git (>= 1.2.5)
|
57
|
+
rake
|
58
|
+
rdoc
|
59
|
+
json (1.7.3)
|
60
|
+
linecache (0.46)
|
61
|
+
rbx-require-relative (> 0.0.4)
|
62
|
+
mail (2.2.19)
|
63
|
+
activesupport (>= 2.3.6)
|
64
|
+
i18n (>= 0.4.0)
|
65
|
+
mime-types (~> 1.16)
|
66
|
+
treetop (~> 1.4.8)
|
67
|
+
mime-types (1.19)
|
68
|
+
polyglot (0.3.3)
|
69
|
+
rack (1.2.5)
|
70
|
+
rack-mount (0.6.14)
|
71
|
+
rack (>= 1.0.0)
|
72
|
+
rack-test (0.5.7)
|
73
|
+
rack (>= 1.0)
|
74
|
+
rails (3.0.15)
|
75
|
+
actionmailer (= 3.0.15)
|
76
|
+
actionpack (= 3.0.15)
|
77
|
+
activerecord (= 3.0.15)
|
78
|
+
activeresource (= 3.0.15)
|
79
|
+
activesupport (= 3.0.15)
|
80
|
+
bundler (~> 1.0)
|
81
|
+
railties (= 3.0.15)
|
82
|
+
railties (3.0.15)
|
83
|
+
actionpack (= 3.0.15)
|
84
|
+
activesupport (= 3.0.15)
|
85
|
+
rake (>= 0.8.7)
|
86
|
+
rdoc (~> 3.4)
|
87
|
+
thor (~> 0.14.4)
|
88
|
+
rake (0.9.2.2)
|
89
|
+
rbx-require-relative (0.0.9)
|
90
|
+
rdoc (3.12)
|
91
|
+
json (~> 1.4)
|
92
|
+
ruby-debug (0.10.4)
|
93
|
+
columnize (>= 0.1)
|
94
|
+
ruby-debug-base (~> 0.10.4.0)
|
95
|
+
ruby-debug-base (0.10.4)
|
96
|
+
linecache (>= 0.3)
|
97
|
+
shoulda (3.0.1)
|
98
|
+
shoulda-context (~> 1.0.0)
|
99
|
+
shoulda-matchers (~> 1.0.0)
|
100
|
+
shoulda-context (1.0.0)
|
101
|
+
shoulda-matchers (1.0.0)
|
102
|
+
sqlite3 (1.3.6)
|
103
|
+
test-unit (2.5.1)
|
104
|
+
thor (0.14.6)
|
105
|
+
treetop (1.4.10)
|
106
|
+
polyglot
|
107
|
+
polyglot (>= 0.3.1)
|
108
|
+
tzinfo (0.3.33)
|
109
|
+
|
110
|
+
PLATFORMS
|
111
|
+
ruby
|
112
|
+
|
113
|
+
DEPENDENCIES
|
114
|
+
active_record_inherit_assoc!
|
115
|
+
activerecord (= 3.0.15)
|
116
|
+
appraisal
|
117
|
+
bundler
|
118
|
+
debugger
|
119
|
+
jeweler
|
120
|
+
rails
|
121
|
+
rake
|
122
|
+
ruby-debug
|
123
|
+
shoulda
|
124
|
+
sqlite3
|
125
|
+
test-unit (>= 2.5.1)
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_record/base'
|
3
|
+
require 'active_record/reflection'
|
4
|
+
require 'active_record/associations/association_proxy'
|
5
|
+
require 'active_record/associations/association_collection'
|
6
|
+
|
7
|
+
ActiveRecord::Base.valid_keys_for_has_many_association << :inherit
|
8
|
+
ActiveRecord::Base.valid_keys_for_has_one_association << :inherit
|
9
|
+
ActiveRecord::Base.valid_keys_for_belongs_to_association << :inherit
|
10
|
+
|
11
|
+
class ActiveRecord::Base
|
12
|
+
# Makes the model inherit the specified attribute from a named association.
|
13
|
+
#
|
14
|
+
# parent_name - The Symbol name of the parent association.
|
15
|
+
# options - The Hash options to use:
|
16
|
+
# :attr - A Symbol or an Array of Symbol names of the attributes
|
17
|
+
# that should be inherited from the parent association.
|
18
|
+
#
|
19
|
+
# Examples
|
20
|
+
#
|
21
|
+
# class Post < ActiveRecord::Base
|
22
|
+
# belongs_to :category
|
23
|
+
# inherits_from :category, :attr => :account
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
def self.inherits_from(parent_name, options = {})
|
27
|
+
attrs = Array.wrap(options.fetch(:attr))
|
28
|
+
|
29
|
+
before_validation do |model|
|
30
|
+
parent = model.send(parent_name)
|
31
|
+
|
32
|
+
attrs.each do |attr|
|
33
|
+
model[attr] = parent[attr] if parent.present?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module ActiveRecord
|
40
|
+
module Associations
|
41
|
+
AssociationProxy.class_eval do
|
42
|
+
def conditions_with_value_inheritance
|
43
|
+
return conditions_without_value_inheritance unless @reflection.klass.respond_to?(:sanitize_sql) # ActiveHash TODO test this!
|
44
|
+
copied_merge_conditions(attribute_inheritance_hash, conditions_without_value_inheritance)
|
45
|
+
end
|
46
|
+
|
47
|
+
alias_method_chain :conditions, :value_inheritance
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
# copied from activerecord 2.3 to fix compatability with 3.0
|
52
|
+
# Merges conditions so that the result is a valid +condition+
|
53
|
+
def copied_merge_conditions(*conditions)
|
54
|
+
segments = []
|
55
|
+
|
56
|
+
conditions.each do |condition|
|
57
|
+
unless condition.blank?
|
58
|
+
sql = sanitize_sql(condition)
|
59
|
+
segments << sql unless sql.blank?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
"(#{segments.join(') AND (')})" unless segments.empty?
|
64
|
+
end
|
65
|
+
|
66
|
+
def attribute_inheritance_hash
|
67
|
+
return {} unless @reflection.options[:inherit]
|
68
|
+
Array(@reflection.options[:inherit]).inject({}) { |hash, obj| hash[obj] = @owner.send(obj) ; hash }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
AssociationCollection.class_eval do
|
73
|
+
# this is *maybe* not the correct place to patch in, but it covers all the cases
|
74
|
+
# without having to patch build, create, create!, etc
|
75
|
+
def add_record_to_target_with_callbacks_with_value_inheritance(record, &block)
|
76
|
+
attribute_inheritance_hash.each do |k, v|
|
77
|
+
record[k] = v
|
78
|
+
end
|
79
|
+
add_record_to_target_with_callbacks_without_value_inheritance(record, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
alias_method_chain :add_record_to_target_with_callbacks, :value_inheritance
|
83
|
+
end
|
84
|
+
|
85
|
+
HasOneAssociation.class_eval do
|
86
|
+
def create_with_value_inheritance(attrs = {}, replace_existing = true)
|
87
|
+
attrs ||= {}
|
88
|
+
create_without_value_inheritance(attribute_inheritance_hash.merge(attrs), replace_existing)
|
89
|
+
end
|
90
|
+
|
91
|
+
def create_with_value_inheritance!(attrs = {}, replace_existing = true)
|
92
|
+
attrs ||= {}
|
93
|
+
create_without_value_inheritance!(attribute_inheritance_hash.merge(attrs), replace_existing)
|
94
|
+
end
|
95
|
+
|
96
|
+
def build_with_value_inheritance(attrs = {}, replace_existing = true)
|
97
|
+
attrs ||= {}
|
98
|
+
build_without_value_inheritance(attribute_inheritance_hash.merge(attrs), replace_existing)
|
99
|
+
end
|
100
|
+
[:create, :create!, :build].each { |sym| alias_method_chain sym, :value_inheritance }
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.setup
|
3
|
+
Bundler.require
|
4
|
+
|
5
|
+
require 'active_support/test_case'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
:adapter => "sqlite3",
|
9
|
+
:database => ":memory:"
|
10
|
+
)
|
11
|
+
require File.expand_path("../schema", __FILE__)
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
14
|
+
require 'active_record_inherit_assoc'
|
data/test/schema.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
drop_table(:mains) rescue nil
|
3
|
+
create_table "mains" do |t|
|
4
|
+
t.integer :account_id
|
5
|
+
t.integer :blah_id
|
6
|
+
t.string "val"
|
7
|
+
end
|
8
|
+
|
9
|
+
drop_table(:others) rescue nil
|
10
|
+
create_table "others" do |t|
|
11
|
+
t.integer :main_id
|
12
|
+
t.integer :account_id
|
13
|
+
t.string :val
|
14
|
+
end
|
15
|
+
|
16
|
+
drop_table(:thirds) rescue nil
|
17
|
+
create_table "thirds" do |t|
|
18
|
+
t.integer :main_id
|
19
|
+
t.integer :account_id
|
20
|
+
end
|
21
|
+
|
22
|
+
drop_table(:fourths) rescue nil
|
23
|
+
create_table "fourths" do |t|
|
24
|
+
t.integer :main_id
|
25
|
+
t.integer :account_id
|
26
|
+
t.integer :blah_id
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path '../helper', __FILE__
|
2
|
+
|
3
|
+
class TestBelongsToAssociation < ActiveSupport::TestCase
|
4
|
+
class Main < ActiveRecord::Base
|
5
|
+
end
|
6
|
+
|
7
|
+
class Other < ActiveRecord::Base
|
8
|
+
belongs_to :main
|
9
|
+
inherits_from :main, :attr => [:account_id, :val]
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_value_is_inherited_from_parent
|
13
|
+
@main = Main.create!(:account_id => 42)
|
14
|
+
@other = Other.create!(:main => @main)
|
15
|
+
|
16
|
+
assert_equal 42, @other.account_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_multiple_values_are_inherited_from_parent
|
20
|
+
@main = Main.create!(:account_id => 42, :val => "The Answer")
|
21
|
+
@other = Other.create!(:main => @main)
|
22
|
+
|
23
|
+
assert_equal 42, @other.account_id
|
24
|
+
assert_equal "The Answer", @other.val
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_does_not_inherit_when_parent_not_present
|
28
|
+
@other = Other.create!
|
29
|
+
assert_nil @other.account_id
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_overwrites_value_on_child
|
33
|
+
@main = Main.create!(:account_id => 42)
|
34
|
+
@other = Other.create!(:main => @main, :account_id => 1337)
|
35
|
+
|
36
|
+
assert_equal 42, @other.account_id
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_allows_setting_the_value_after_instantiation
|
40
|
+
@main = Main.create!
|
41
|
+
@other = Other.new(:main => @main, :account_id => 1337)
|
42
|
+
|
43
|
+
@main.account_id = 42
|
44
|
+
@other.save
|
45
|
+
|
46
|
+
assert_equal 42, @other.account_id
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.expand_path '../helper', __FILE__
|
2
|
+
|
3
|
+
class TestInheritAssoc < ActiveSupport::TestCase
|
4
|
+
class Main < ActiveRecord::Base
|
5
|
+
has_many :others, :inherit => :account_id
|
6
|
+
has_one :third, :inherit => :account_id
|
7
|
+
has_many :fourths, :inherit => [:account_id, :blah_id]
|
8
|
+
has_many :conditional_others, :inherit => :account_id, :conditions => {:val => "foo"}, :class_name => "Other"
|
9
|
+
end
|
10
|
+
|
11
|
+
class Other < ActiveRecord::Base
|
12
|
+
belongs_to :main
|
13
|
+
end
|
14
|
+
|
15
|
+
class Third < ActiveRecord::Base
|
16
|
+
belongs_to :main
|
17
|
+
end
|
18
|
+
|
19
|
+
class Fourth < ActiveRecord::Base
|
20
|
+
belongs_to :main
|
21
|
+
end
|
22
|
+
|
23
|
+
context "Main, with some others, scoped by account_id" do
|
24
|
+
setup do
|
25
|
+
@main = Main.create! :account_id => 1
|
26
|
+
Other.create! :main_id => @main.id, :account_id => 1
|
27
|
+
Other.create! :main_id => @main.id, :account_id => 2
|
28
|
+
Other.create! :main_id => @main.id, :account_id => 1, :val => "foo"
|
29
|
+
end
|
30
|
+
|
31
|
+
should "set conditions on simple access" do
|
32
|
+
assert_equal 2, @main.others.size
|
33
|
+
end
|
34
|
+
|
35
|
+
should "set conditions on find" do
|
36
|
+
assert_equal 2, @main.others.find(:all).size
|
37
|
+
end
|
38
|
+
|
39
|
+
should "merge conditions on find" do
|
40
|
+
assert_equal 1, @main.others.all(:conditions => "val = 'foo'").size
|
41
|
+
end
|
42
|
+
|
43
|
+
should "merge conditions" do
|
44
|
+
assert_equal 1, @main.conditional_others.size
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_has_one_should_set_conditions_on_fetch
|
49
|
+
main = Main.create! :account_id => 1
|
50
|
+
third_1 = Third.create! :main_id => main.id, :account_id => 2
|
51
|
+
third_2 = Third.create! :main_id => main.id, :account_id => 1
|
52
|
+
assert_equal third_2, main.third
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_has_many_should_set_conditions_for_multiple_inherits
|
56
|
+
main = Main.create! :account_id => 1, :blah_id => 10
|
57
|
+
# these two should match
|
58
|
+
Fourth.create! :main_id => main.id, :account_id => 1, :blah_id => 10
|
59
|
+
Fourth.create! :main_id => main.id, :account_id => 1, :blah_id => 10
|
60
|
+
|
61
|
+
# nope.
|
62
|
+
Fourth.create! :main_id => main.id, :account_id => 1, :blah_id => 5
|
63
|
+
Fourth.create! :main_id => main.id, :account_id => 1, :blah_id => 12
|
64
|
+
Fourth.create! :main_id => 99999, :account_id => 1, :blah_id => 12
|
65
|
+
|
66
|
+
assert_equal(2, main.fourths.size)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_has_many_should_setup_attributes_when_building
|
70
|
+
main = Main.create! :account_id => 1, :blah_id => 10
|
71
|
+
|
72
|
+
other = main.others.build
|
73
|
+
assert_equal main.id, other.main_id
|
74
|
+
assert_equal main.account_id, other.account_id
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_has_many_should_setup_attributes_when_creating
|
78
|
+
main = Main.create! :account_id => 1, :blah_id => 10
|
79
|
+
|
80
|
+
other = main.others.create!
|
81
|
+
assert_equal main.id, other.main_id
|
82
|
+
assert_equal main.account_id, other.account_id
|
83
|
+
|
84
|
+
other = main.others.create
|
85
|
+
assert_equal main.id, other.main_id
|
86
|
+
assert_equal main.account_id, other.account_id
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_has_one_should_setup_attributes_when_building
|
90
|
+
main = Main.create! :account_id => 1, :blah_id => 10
|
91
|
+
|
92
|
+
other = main.build_third
|
93
|
+
assert_equal main.account_id, other.account_id
|
94
|
+
|
95
|
+
other = main.create_third
|
96
|
+
assert_equal main.account_id, other.account_id
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_inherit_assoc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Osheroff
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description:
|
31
|
+
email:
|
32
|
+
- ben@gimbo.net
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .travis.yml
|
39
|
+
- Appraisals
|
40
|
+
- Gemfile
|
41
|
+
- MIT-LICENSE
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- active_record_inherit_assoc.gemspec
|
45
|
+
- gemfiles/rails2.3.gemfile
|
46
|
+
- gemfiles/rails2.3.gemfile.lock
|
47
|
+
- gemfiles/rails3.0.gemfile
|
48
|
+
- gemfiles/rails3.0.gemfile.lock
|
49
|
+
- lib/active_record_inherit_assoc.rb
|
50
|
+
- test/helper.rb
|
51
|
+
- test/schema.rb
|
52
|
+
- test/test_belongs_to_association.rb
|
53
|
+
- test/test_inherit_assoc.rb
|
54
|
+
homepage: http://github.com/zendesk/active_record_inherit_assoc
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
hash: -1206521462731846587
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: -1206521462731846587
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.24
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Attribute inheritance for AR associations
|
85
|
+
test_files: []
|