Permalink
Browse files

Return an empty array if the @_tenacity_associations instance variabl…

…e is nil
  • Loading branch information...
1 parent ebcd2e3 commit 0bb76d02f745f1a2b91588c00bc779633a586eaf @jwood committed Jun 6, 2011
Showing with 14 additions and 17 deletions.
  1. +4 −1 lib/tenacity/class_methods.rb
  2. +10 −16 lib/tenacity/instance_methods.rb
@@ -141,7 +141,6 @@ module Tenacity
# project.milestones # uses the milestone cache
#
module ClassMethods
- attr_reader :_tenacity_associations
# Specifies a one-to-one association with another class. This method should only be used
# if the other class contains the foreign key. If the current class contains the foreign key,
@@ -425,6 +424,10 @@ def _t_serialize(object_id, association=nil) #:nodoc:
end
end
+ def _tenacity_associations
+ @_tenacity_associations || []
+ end
+
private
def _t_create_association(type, name, options) #:nococ:
@@ -7,17 +7,15 @@ def _t_ivar_name(association)
def _t_save_autosave_associations
associations = self.class._tenacity_associations
- unless associations.nil?
- self.class._tenacity_associations.select { |a| a.autosave == true }.each do |association|
- if association.type == :t_has_one || association.type == :t_belongs_to
- associate = instance_variable_get(_t_ivar_name(association))
- autosave_save_or_destroy(associate) unless associate.nil?
- elsif association.type == :t_has_many
- associates = instance_variable_get(_t_ivar_name(association))
- unless associates.nil?
- associates.each { |associate| autosave_save_or_destroy(associate) }
- instance_variable_set(_t_ivar_name(association), associates.reject { |associate| associate.marked_for_destruction? })
- end
+ self.class._tenacity_associations.select { |a| a.autosave == true }.each do |association|
+ if association.type == :t_has_one || association.type == :t_belongs_to
+ associate = instance_variable_get(_t_ivar_name(association))
+ autosave_save_or_destroy(associate) unless associate.nil?
+ elsif association.type == :t_has_many
+ associates = instance_variable_get(_t_ivar_name(association))
+ unless associates.nil?
+ associates.each { |associate| autosave_save_or_destroy(associate) }
+ instance_variable_set(_t_ivar_name(association), associates.reject { |associate| associate.marked_for_destruction? })
end
end
end
@@ -96,11 +94,7 @@ def _t_serialize(object, association=nil)
def associations_requiring_associate_validation
associations = self.class._tenacity_associations
- unless associations.nil?
- associations.select { |a| a.foreign_key_constraints_enabled? && a.type == :t_belongs_to }
- else
- []
- end
+ associations.select { |a| a.foreign_key_constraints_enabled? && a.type == :t_belongs_to }
end
end

0 comments on commit 0bb76d0

Please sign in to comment.