Method: EnoughFields.enable=

Defined in:
lib/enough_fields.rb

.enable=(enable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/enough_fields.rb', line 22

def enable=(enable)
  @enable = enable
  if enable?
    Mongoid::Cursor.class_eval do
      alias_method :origin_each, :each

      def each
        if @cursor.count > 1
          @cursor.each do |document|
            attributes = Attributes.build(@klass, document)
            yield Mongoid::Factory.build(@klass, attributes)
          end
        else
          :origin_each
        end
      end
    end

    Mongoid::Factory.class_eval do
      class <<self
        def build(klass, attributes)
          type = attributes["_type"]
          type ? type.constantize.instantiate(attributes) : klass.instantiate(attributes)
        end
      end
    end
  end
end