johnsbrn-classy-inheritance 0.6.8.2 → 0.6.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/classy-inheritance.rb +91 -70
  2. metadata +3 -2
@@ -1,51 +1,87 @@
1
1
  require 'active_record/version'
2
2
  module ActiveRecord
3
- module Associations
4
- class HasOneAssociation
5
-
6
- # this is fixed in 2.3, but it doesn't hurt to leave it here
7
- def set_belongs_to_association_for(record)
8
- if @reflection.options[:as]
9
- record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
10
- record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
3
+
4
+ # 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
5
+ if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 2
6
+ module ClassMethods
7
+ def has_one(association_id, options = {})
8
+ if options[:through]
9
+ reflection = create_has_one_through_reflection(association_id, options)
10
+ association_accessor_methods(reflection, ActiveRecord::Associations::HasOneThroughAssociation)
11
11
  else
12
- unless @owner.new_record?
13
- primary_key = @reflection.options[:primary_key] || :id
14
- record[@reflection.primary_key_name] = @owner.send(primary_key)
12
+ reflection = create_has_one_reflection(association_id, options)
13
+
14
+ ivar = "@#{reflection.name}"
15
+
16
+ method_name = "has_one_after_save_for_#{reflection.name}".to_sym
17
+ define_method(method_name) do
18
+ association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
19
+
20
+ primary_key = reflection.options[:primary_key] || :id
21
+ if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != send(primary_key))
22
+ association[reflection.primary_key_name] = send(primary_key)
23
+ association.save(true)
24
+ end
15
25
  end
26
+ after_save method_name
27
+
28
+ add_single_associated_validation_callbacks(reflection.name) if options[:validate] == true
29
+ association_accessor_methods(reflection, ActiveRecord::Associations::HasOneAssociation)
30
+ association_constructor_method(:build, reflection, ActiveRecord::Associations::HasOneAssociation)
31
+ association_constructor_method(:create, reflection, ActiveRecord::Associations::HasOneAssociation)
32
+
33
+ configure_dependency_for_has_one(reflection)
16
34
  end
17
35
  end
36
+ end
37
+ end
38
+
39
+ if ActiveRecord::VERSION::MAJOR == 2
40
+ module Associations
41
+ class HasOneAssociation
18
42
 
19
- private
20
- #this is still not fixed in 2.3
21
- def new_record(replace_existing)
22
- # Make sure we load the target first, if we plan on replacing the existing
23
- # instance. Otherwise, if the target has not previously been loaded
24
- # elsewhere, the instance we create will get orphaned.
25
- load_target if replace_existing
26
- record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) do
27
- yield @reflection
43
+ # this is fixed in 2.3, but it doesn't hurt to leave it here
44
+ def set_belongs_to_association_for(record)
45
+ if @reflection.options[:as]
46
+ record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
47
+ record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
48
+ else
49
+ unless @owner.new_record?
50
+ primary_key = @reflection.options[:primary_key] || :id
51
+ record[@reflection.primary_key_name] = @owner.send(primary_key)
52
+ end
53
+ end
28
54
  end
55
+
56
+ private
57
+ #this is still not fixed in 2.3
58
+ def new_record(replace_existing)
59
+ # Make sure we load the target first, if we plan on replacing the existing
60
+ # instance. Otherwise, if the target has not previously been loaded
61
+ # elsewhere, the instance we create will get orphaned.
62
+ load_target if replace_existing
63
+ record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) do
64
+ yield @reflection
65
+ end
29
66
 
30
- if replace_existing
31
- replace(record, true)
32
- else
33
- unless @owner.new_record?
34
- primary_key = @reflection.options[:primary_key] || :id
35
- record[@reflection.primary_key_name] = @owner.send(primary_key)
67
+ if replace_existing
68
+ replace(record, true)
69
+ else
70
+ unless @owner.new_record?
71
+ primary_key = @reflection.options[:primary_key] || :id
72
+ record[@reflection.primary_key_name] = @owner.send(primary_key)
73
+ end
74
+ self.target = record
36
75
  end
37
- self.target = record
38
- end
39
76
 
40
- record
77
+ record
78
+ end
41
79
  end
42
80
  end
43
81
  end
44
82
 
45
83
  if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 3
46
-
47
84
  module AutosaveAssociation
48
-
49
85
  # 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
86
  def save_has_one_association(reflection)
51
87
  if (association = association_instance_get(reflection.name)) && !association.target.nil?
@@ -58,57 +94,42 @@ module ActiveRecord
58
94
  end
59
95
  end
60
96
  end
61
- end
97
+
98
+ # this is fixed in 2.3.3
99
+ def save_belongs_to_association(reflection)
100
+ if association = association_instance_get(reflection.name)
101
+ autosave = reflection.options[:autosave]
62
102
 
103
+ if autosave && association.marked_for_destruction?
104
+ association.destroy
105
+ else
106
+ association.save(!autosave) if association.new_record? || autosave
107
+
108
+ if association.updated?
109
+ association_id = association.send(reflection.options[:primary_key] || :id)
110
+ self[reflection.primary_key_name] = association_id
111
+ # TODO: Removing this code doesn't seem to matter…
112
+ if reflection.options[:polymorphic]
113
+ self[reflection.options[:foreign_type]] = association.class.base_class.name.to_s
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
63
120
  end
64
121
  end
65
122
 
66
123
  module Stonean
67
124
  module ClassyInheritance
68
- VERSION = '0.6.8.2'
125
+ VERSION = '0.6.8.3'
69
126
 
70
127
  def self.version
71
128
  VERSION
72
129
  end
73
130
 
74
131
  module ClassMethods
75
-
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)
86
-
87
- ivar = "@#{reflection.name}"
88
-
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)
92
132
 
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
98
- end
99
- after_save method_name
100
-
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)
105
-
106
- configure_dependency_for_has_one(reflection)
107
- end
108
- end
109
-
110
- end
111
-
112
133
  def depends_on(model_sym, options = {})
113
134
  define_relationship(model_sym,options)
114
135
 
@@ -130,7 +151,7 @@ module Stonean
130
151
  end
131
152
 
132
153
  # Before save functionality to create/update the requisite object
133
- define_save_method(model_sym, options[:as])
154
+ define_save_method(model_sym, options[:as]) unless options[:autosave]
134
155
 
135
156
  # Adds a find_with_<model_sym> class method
136
157
  define_find_with_method(model_sym)
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.2
4
+ version: 0.6.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Stone
@@ -49,6 +49,7 @@ files:
49
49
  - test/test_with_class_name_ending_with_ss.rb
50
50
  has_rdoc: true
51
51
  homepage: http://stonean.com/wiki/classy-inheritance
52
+ licenses:
52
53
  post_install_message:
53
54
  rdoc_options:
54
55
  - --main
@@ -70,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
71
  requirements: []
71
72
 
72
73
  rubyforge_project: classyinherit
73
- rubygems_version: 1.2.0
74
+ rubygems_version: 1.3.5
74
75
  signing_key:
75
76
  specification_version: 2
76
77
  summary: Classy Inheritance adds a depends_on class method to your ActiveRecord model so that you can define requisite objects