johnsbrn-classy-inheritance 0.6.8 → 0.6.8.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/History.txt +2 -0
- data/lib/classy-inheritance.rb +51 -23
- data/test/test_classy-inheritance.rb +1 -2
- data/test/test_has_dependency.rb +14 -2
- data/test/test_helper.rb +21 -0
- data/test/test_with_optional_dependency.rb +9 -8
- metadata +3 -2
data/History.txt
CHANGED
data/lib/classy-inheritance.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
require 'active_record/version'
|
1
2
|
module ActiveRecord
|
2
3
|
module Associations
|
3
4
|
class HasOneAssociation
|
4
5
|
|
6
|
+
# this is fixed in 2.3, but it doesn't hurt to leave it here
|
5
7
|
def set_belongs_to_association_for(record)
|
6
8
|
if @reflection.options[:as]
|
7
9
|
record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
|
@@ -15,6 +17,7 @@ module ActiveRecord
|
|
15
17
|
end
|
16
18
|
|
17
19
|
private
|
20
|
+
#this is still not fixed in 2.3
|
18
21
|
def new_record(replace_existing)
|
19
22
|
# Make sure we load the target first, if we plan on replacing the existing
|
20
23
|
# instance. Otherwise, if the target has not previously been loaded
|
@@ -38,11 +41,31 @@ module ActiveRecord
|
|
38
41
|
end
|
39
42
|
end
|
40
43
|
end
|
44
|
+
|
45
|
+
if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 3
|
46
|
+
|
47
|
+
module AutosaveAssociation
|
48
|
+
|
49
|
+
# fix active record has_one primary key bug rails 2.3 - http://rails.lighthouseapp.com/projects/8994/tickets/1756-has_one-with-foreign_key-primary_key-bug
|
50
|
+
def save_has_one_association(reflection)
|
51
|
+
if (association = association_instance_get(reflection.name)) && !association.target.nil?
|
52
|
+
primary_key = reflection.options[:primary_key] || :id
|
53
|
+
if reflection.options[:autosave] && association.marked_for_destruction?
|
54
|
+
association.destroy
|
55
|
+
elsif new_record? || association.new_record? || association[reflection.primary_key_name] != send(primary_key) || reflection.options[:autosave]
|
56
|
+
association[reflection.primary_key_name] = send(primary_key)
|
57
|
+
association.save(false)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
41
64
|
end
|
42
65
|
|
43
66
|
module Stonean
|
44
67
|
module ClassyInheritance
|
45
|
-
VERSION = '0.6.8'
|
68
|
+
VERSION = '0.6.8.1'
|
46
69
|
|
47
70
|
def self.version
|
48
71
|
VERSION
|
@@ -50,35 +73,40 @@ module Stonean
|
|
50
73
|
|
51
74
|
module ClassMethods
|
52
75
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
76
|
+
|
77
|
+
# fix active record has_one primary key bug rails 2.2.2 - http://rails.lighthouseapp.com/projects/8994/tickets/1756-has_one-with-foreign_key-primary_key-bug
|
78
|
+
if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 2
|
79
|
+
|
80
|
+
def has_one(association_id, options = {})
|
81
|
+
if options[:through]
|
82
|
+
reflection = create_has_one_through_reflection(association_id, options)
|
83
|
+
association_accessor_methods(reflection, ActiveRecord::Associations::HasOneThroughAssociation)
|
84
|
+
else
|
85
|
+
reflection = create_has_one_reflection(association_id, options)
|
60
86
|
|
61
|
-
|
87
|
+
ivar = "@#{reflection.name}"
|
62
88
|
|
63
|
-
|
64
|
-
|
65
|
-
|
89
|
+
method_name = "has_one_after_save_for_#{reflection.name}".to_sym
|
90
|
+
define_method(method_name) do
|
91
|
+
association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
|
66
92
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
93
|
+
primary_key = reflection.options[:primary_key] || :id
|
94
|
+
if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != send(primary_key))
|
95
|
+
association[reflection.primary_key_name] = send(primary_key)
|
96
|
+
association.save(true)
|
97
|
+
end
|
71
98
|
end
|
72
|
-
|
73
|
-
after_save method_name
|
99
|
+
after_save method_name
|
74
100
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
101
|
+
add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
|
102
|
+
association_accessor_methods(reflection, ActiveRecord::Associations::HasOneAssociation)
|
103
|
+
association_constructor_method(:build, reflection, ActiveRecord::Associations::HasOneAssociation)
|
104
|
+
association_constructor_method(:create, reflection, ActiveRecord::Associations::HasOneAssociation)
|
79
105
|
|
80
|
-
|
106
|
+
configure_dependency_for_has_one(reflection)
|
107
|
+
end
|
81
108
|
end
|
109
|
+
|
82
110
|
end
|
83
111
|
|
84
112
|
def depends_on(model_sym, options = {})
|
@@ -1,10 +1,9 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
|
-
User.depends_on :profile, :attrs => [:first_name, :last_name, :email]
|
4
|
-
|
5
3
|
class TestClassyInheritance < Test::Unit::TestCase
|
6
4
|
|
7
5
|
def setup
|
6
|
+
User.depends_on :profile, :attrs => [:first_name, :last_name, :email]
|
8
7
|
@user = User.new
|
9
8
|
end
|
10
9
|
|
data/test/test_has_dependency.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
|
-
Account.has_dependency :account_login, :attrs => [:login, :password], :foreign_key => :account_email, :primary_key => :email
|
4
|
-
|
5
3
|
class TestHasDependency < Test::Unit::TestCase
|
6
4
|
|
7
5
|
def setup
|
6
|
+
Account.has_dependency :account_login, :attrs => [:login, :password], :foreign_key => :account_email, :primary_key => :email
|
7
|
+
Database.has_dependency :database_login, :attrs => [:login]
|
8
8
|
@account = Account.new
|
9
|
+
@database = Database.new
|
9
10
|
end
|
10
11
|
|
11
12
|
def test_active_record_should_respond_to_depends_on
|
@@ -32,6 +33,17 @@ class TestHasDependency < Test::Unit::TestCase
|
|
32
33
|
assert @account.respond_to?(:password=)
|
33
34
|
end
|
34
35
|
|
36
|
+
def test_profile_should_create_author_record
|
37
|
+
@database.name = 'page'
|
38
|
+
@database.login = 'joe'
|
39
|
+
|
40
|
+
@database.save!
|
41
|
+
|
42
|
+
@database_login = DatabaseLogin.find(:first, :conditions => {:database_id => @database.id})
|
43
|
+
|
44
|
+
assert_equal 'joe', @database_login.login
|
45
|
+
end
|
46
|
+
|
35
47
|
def test_account_should_create_account_login_record
|
36
48
|
@account.login = 'joe'
|
37
49
|
@account.password = 'password'
|
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'activerecord'
|
3
3
|
|
4
|
+
ActiveRecord.load_all!
|
5
|
+
|
4
6
|
require 'test/unit'
|
5
7
|
require File.dirname(__FILE__) + '/../lib/classy-inheritance'
|
6
8
|
|
@@ -56,6 +58,17 @@ class SetupTestTables < ActiveRecord::Migration
|
|
56
58
|
t.timestamps
|
57
59
|
end
|
58
60
|
|
61
|
+
create_table :databases, :force => true do |t|
|
62
|
+
t.string :name
|
63
|
+
t.timestamps
|
64
|
+
end
|
65
|
+
|
66
|
+
create_table :database_logins, :force => true do |t|
|
67
|
+
t.string :login
|
68
|
+
t.integer :database_id
|
69
|
+
t.timestamps
|
70
|
+
end
|
71
|
+
|
59
72
|
create_table :addresses, :force => true do |t|
|
60
73
|
t.string :line_one
|
61
74
|
t.string :line_two
|
@@ -118,6 +131,14 @@ class Profile < ActiveRecord::Base
|
|
118
131
|
validates_presence_of :first_name, :last_name, :email
|
119
132
|
end
|
120
133
|
|
134
|
+
class Database < ActiveRecord::Base
|
135
|
+
validates_presence_of :name
|
136
|
+
end
|
137
|
+
|
138
|
+
class DatabaseLogin < ActiveRecord::Base
|
139
|
+
validates_presence_of :login
|
140
|
+
end
|
141
|
+
|
121
142
|
class Account < ActiveRecord::Base
|
122
143
|
validates_presence_of :first_name, :last_name, :email
|
123
144
|
end
|
@@ -1,16 +1,17 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
2
|
|
3
3
|
class TestWithOptionalDependency < Test::Unit::TestCase
|
4
|
-
# Turn off the validates_presence_of call
|
5
|
-
Author.depends_on :profile, :validates_presence_if => false,
|
6
|
-
:attrs => [:first_name, :last_name, :email]
|
7
|
-
|
8
|
-
# Turn off the validates_presence_of and the validates_associated calls
|
9
|
-
Artist.depends_on :profile, :validates_presence_if => false,
|
10
|
-
:validates_associated_if => false,
|
11
|
-
:attrs => [:first_name, :last_name, :email]
|
12
4
|
|
13
5
|
def setup
|
6
|
+
# Turn off the validates_presence_of call
|
7
|
+
Author.depends_on :profile, :validates_presence_if => false,
|
8
|
+
:attrs => [:first_name, :last_name, :email]
|
9
|
+
|
10
|
+
# Turn off the validates_presence_of and the validates_associated calls
|
11
|
+
Artist.depends_on :profile, :validates_presence_if => false,
|
12
|
+
:validates_associated_if => false,
|
13
|
+
:attrs => [:first_name, :last_name, :email]
|
14
|
+
|
14
15
|
@author = Author.new
|
15
16
|
@artist = Artist.new
|
16
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: johnsbrn-classy-inheritance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.8
|
4
|
+
version: 0.6.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Stone
|
@@ -9,11 +9,12 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-19 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bones
|
17
|
+
type: :development
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|