lib/api_resource/associations.rb in api_resource-0.2.11 vs lib/api_resource/associations.rb in api_resource-0.3.0

- old
+ new

@@ -18,12 +18,12 @@ extend ActiveSupport::Concern included do raise "Cannot include Associations without first including AssociationActivation" unless self.ancestors.include?(ApiResource::AssociationActivation) - class_inheritable_accessor :related_objects - + class_attribute :related_objects + # Hash to hold onto the definitions of the related objects self.related_objects = RelatedObjectHash.new self.association_types.keys.each do |type| self.related_objects[type] = RelatedObjectHash.new({}) end @@ -55,11 +55,12 @@ raise "Invalid arguments to #{assoc}" unless options.blank? || args.length == 1 args.each do |arg| klass_name = (options[:class_name] ? options[:class_name].to_s.classify : arg.to_s.classify) # add this to any descendants - the other methods etc are handled by inheritance ([self] + self.descendants).each do |klass| - klass.related_objects[:#{assoc}][arg.to_sym] = klass_name + #We need to merge upon itself to generate a new object since the children all share their related objects with each other + klass.related_objects = klass.related_objects.merge(:#{assoc} => klass.related_objects[:#{assoc}].merge(arg.to_sym => klass_name)) end # We need to define reader and writer methods here define_association_as_attribute(:#{assoc}, arg) end end @@ -152,22 +153,20 @@ return klass end end - module InstanceMethods - def association?(assoc) - self.class.association?(assoc) - end - - def association_class_name(assoc) - self.class.association_class_name(assoc) - end - - # list of all association names - def association_names - self.class.association_names - end + def association?(assoc) + self.class.association?(assoc) + end + + def association_class_name(assoc) + self.class.association_class_name(assoc) + end + + # list of all association names + def association_names + self.class.association_names end end end \ No newline at end of file