motion-prime/models/association.rb in motion-prime-0.2.1 vs motion-prime/models/association.rb in motion-prime-0.3.0

- old
+ new

@@ -16,21 +16,19 @@ # Defines bag associated with model, creates accessor for bag # # @param [String] name - the name of bag # @return [Nil] def bag(name) - klass = self - define_method(name) do |*args, &block| return _bags[name] if _bags[name] - bag_key = self.info[name] - if bag_key.nil? + if bag_key.present? + bag = self.class.store.bagsWithKeysInArray([bag_key]).first + end + unless bag bag = Bag.bag self.info[name] = bag.key - else - bag = self.class.store.bagsWithKeysInArray([bag_key]).first end _bags[name] = bag end @@ -107,15 +105,30 @@ end define_method("#{association_name}=") do |value| self.send(bag_name).clear self.send(:"#{bag_name}=", value) end - define_method("#{association_name}") do |options = {}| + define_method("#{association_name}") do |*args| bag = self.send(:"#{bag_name}") collection_options = { - association_name: association_name + association_name: association_name, + inverse_relation: { + type: :has_one, + name: self.class_name_without_kvo.demodulize.underscore, + instance: self + } } - AssociationCollection.new(bag, collection_options, options) + AssociationCollection.new(bag, collection_options, *args) end + end + + def belongs_to(association_name, options = {}) + self._associations ||= {} + self._associations[association_name] = { + type: :belongs_to_one, + class_name: association_name.classify + }.merge(options) + + self.send(:attr_accessor, association_name) end end end \ No newline at end of file