johnsbrn-classy-inheritance 0.6.5 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +2 -0
- data/lib/classy-inheritance.rb +43 -5
- data/test/test_has_dependency.rb +79 -0
- metadata +1 -1
data/History.txt
CHANGED
data/lib/classy-inheritance.rb
CHANGED
@@ -1,3 +1,40 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Associations
|
3
|
+
class HasOneAssociation
|
4
|
+
|
5
|
+
def set_belongs_to_association_for(record)
|
6
|
+
if @reflection.options[:as]
|
7
|
+
record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
|
8
|
+
record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
|
9
|
+
else
|
10
|
+
record[@reflection.primary_key_name] = @reflection.options.has_key?(:primary_key) ? @owner.send(@reflection.options[:primary_key]) : @owner.id unless @owner.new_record?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def new_record(replace_existing)
|
16
|
+
# Make sure we load the target first, if we plan on replacing the existing
|
17
|
+
# instance. Otherwise, if the target has not previously been loaded
|
18
|
+
# elsewhere, the instance we create will get orphaned.
|
19
|
+
load_target if replace_existing
|
20
|
+
record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) do
|
21
|
+
yield @reflection
|
22
|
+
end
|
23
|
+
|
24
|
+
if replace_existing
|
25
|
+
replace(record, true)
|
26
|
+
else
|
27
|
+
puts "test"
|
28
|
+
record[@reflection.primary_key_name] = @reflection.options.has_key?(:primary_key) ? @owner.send(@reflection.options[:primary_key]) : @owner.id unless @owner.new_record?
|
29
|
+
self.target = record
|
30
|
+
end
|
31
|
+
|
32
|
+
record
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
1
38
|
module Stonean
|
2
39
|
module ClassyInheritance
|
3
40
|
VERSION = '0.6.5'
|
@@ -5,7 +42,7 @@ module Stonean
|
|
5
42
|
def self.version
|
6
43
|
VERSION
|
7
44
|
end
|
8
|
-
|
45
|
+
|
9
46
|
module ClassMethods
|
10
47
|
|
11
48
|
# fix active record has_one primary key bug - http://rails.lighthouseapp.com/projects/8994/tickets/1756-has_one-with-foreign_key-primary_key-bug
|
@@ -21,8 +58,9 @@ module Stonean
|
|
21
58
|
method_name = "has_one_after_save_for_#{reflection.name}".to_sym
|
22
59
|
define_method(method_name) do
|
23
60
|
association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
|
24
|
-
|
25
|
-
|
61
|
+
|
62
|
+
primary_key = reflection.options[:primary_key] || :id
|
63
|
+
if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != send(primary_key))
|
26
64
|
association[reflection.primary_key_name] = send(primary_key)
|
27
65
|
association.save(true)
|
28
66
|
end
|
@@ -230,9 +268,9 @@ if Object.const_defined?("ActiveRecord") && ActiveRecord.const_defined?("Base")
|
|
230
268
|
end
|
231
269
|
end
|
232
270
|
end
|
233
|
-
|
234
|
-
|
271
|
+
|
235
272
|
ActiveRecord::Base.class_eval do
|
236
273
|
extend Stonean::ClassyInheritance::ClassMethods
|
237
274
|
end
|
275
|
+
|
238
276
|
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
Account.has_dependency :account_login, :attrs => [:login, :password], :foreign_key => :account_email, :primary_key => :email
|
4
|
+
|
5
|
+
class TestHasDependency < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@account = Account.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_active_record_should_respond_to_depends_on
|
12
|
+
assert ActiveRecord::Base.respond_to?(:has_dependency)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_account_should_respond_to_find_with_account_login
|
16
|
+
assert Account.respond_to?(:find_with_account_login)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_account_should_respond_to_login
|
20
|
+
assert @account.respond_to?(:login)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_account_should_respond_to_login=
|
24
|
+
assert @account.respond_to?(:login=)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_account_should_respond_to_password
|
28
|
+
assert @account.respond_to?(:password)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_account_should_respond_to_password=
|
32
|
+
assert @account.respond_to?(:password=)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_account_should_create_account_login_record
|
36
|
+
@account.login = 'joe'
|
37
|
+
@account.password = 'password'
|
38
|
+
@account.first_name = 'joe'
|
39
|
+
@account.last_name = 'bloggs'
|
40
|
+
@account.email = 'joe@bloggs.co.uk'
|
41
|
+
|
42
|
+
@account.save
|
43
|
+
|
44
|
+
@account_login = AccountLogin.find(:first, :conditions => {:account_email => @account.email})
|
45
|
+
|
46
|
+
assert_equal @account_login, @account.account_login
|
47
|
+
|
48
|
+
assert_equal @account_login.login, @account.login
|
49
|
+
assert_equal @account_login.password, @account.password
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_account_should_be_invalid_without_account_login_attributes
|
53
|
+
@account.first_name = 'joe'
|
54
|
+
@account.last_name = 'bloggs'
|
55
|
+
@account.email = 'joe@bloggs.co.uk'
|
56
|
+
assert !@account.valid?
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_account_should_be_valid_with_account_login_attributes
|
60
|
+
@account.login = 'joe'
|
61
|
+
@account.password = 'password'
|
62
|
+
@account.first_name = 'joe'
|
63
|
+
@account.last_name = 'bloggs'
|
64
|
+
@account.email = 'joe@bloggs.co.uk'
|
65
|
+
|
66
|
+
assert @account.valid?
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_account_should_have_nice_error_message
|
70
|
+
@account = Account.new(:login => "joe")
|
71
|
+
@account.valid?
|
72
|
+
|
73
|
+
assert @account.errors.full_messages.include?("Last name can't be blank")
|
74
|
+
assert @account.errors.full_messages.include?("Email can't be blank")
|
75
|
+
assert @account.errors.full_messages.include?("First name can't be blank")
|
76
|
+
assert @account.errors.full_messages.include?("Password can't be blank")
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|