Sha256: 6fe255c10d090f9b594ed155166c52091a98d41d4dbc8d11d831df453a126556

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

# encoding: UTF-8
module MarkMapper
  module Plugins
    module Associations
      class ManyAssociation < Base

        def class_name
          @class_name ||= options[:class_name] || name.to_s.singularize.camelize
        end

        # hate this, need to revisit
        def proxy_class
          @proxy_class ||= if klass.embeddable?
            polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
          else
            if polymorphic?
              ManyPolymorphicProxy
            elsif as?
              ManyDocumentsAsProxy
            elsif in_array?
              InArrayProxy
            else
              ManyDocumentsProxy
            end
          end
        end

        def setup(model)
          model.associations_module.module_eval <<-end_eval
            def #{name}
              get_proxy(associations[#{name.inspect}])
            end

            def #{name}=(value)
              get_proxy(associations[#{name.inspect}]).replace(value)
              value
            end
          end_eval

          association = self
          options = self.options

          model.before_destroy do
            if !association.embeddable?
              case options[:dependent]
                when :destroy
                  self.get_proxy(association).destroy_all
                when :delete_all
                  self.get_proxy(association).delete_all
                when :nullify
                  self.get_proxy(association).nullify
              end
            end
          end
        end

        def autosave?
          options.fetch(:autosave, true)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mark_mapper-0.0.6 lib/mark_mapper/plugins/associations/many_association.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/associations/many_association.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/associations/many_association.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/associations/many_association.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/associations/many_association.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/associations/many_association.rb