Sha256: 53129acb9470c6f468188c6180692dbc764d610de1d279056fdbb74eb9c6404a

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

# module Mongoid #:nodoc:
#   module Relations #:nodoc:
#     module Referenced #:nodoc:

#       # This class defines the behaviour for all relations that are a
#       # one-to-many between documents in different collections.
#       class Many < Relations::Many

#         def build_with_custom_fields(attributes = {}, type = nil)
#           if base.respond_to?(:custom_fields_for?) && base.custom_fields_for?(relation_metadata.name)
#             # all the information about how to build the custom class are stored here
#             recipe = base.custom_fields_recipe_for(relation_metadata.name)
#             attributes ||= {}
#             attributes.merge!(custom_fields_recipe: recipe)
#             # build the class with custom_fields for the first time
#             type = relation_metadata.klass.klass_with_custom_fields(recipe)
#           end
#           build_without_custom_fields(attributes, type)
#         end

#         alias_method :build_without_custom_fields, :build
#         alias_method :build, :build_with_custom_fields

#         # new should point to the new build method
#         alias :new :build_with_custom_fields
#       end

#     end
#   end
# end

module CustomFieldsManyExtension
  def build(attributes = {}, type = nil)
    if _base.respond_to?(:custom_fields_for?) && _base.custom_fields_for?(association.name)
      # all the information about how to build the custom class are stored here
      recipe = _base.custom_fields_recipe_for(association.name)
      attributes ||= {}
      attributes.merge!(custom_fields_recipe: recipe)
      # build the class with custom_fields for the first time
      type = association.klass.klass_with_custom_fields(recipe)
    end
    super(attributes, type)
  end
  alias new build
end

::Mongoid::Association::Referenced::HasMany::Proxy.prepend CustomFieldsManyExtension

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
custom_fields-2.14.0.alpha1 lib/custom_fields/extensions/mongoid/association/referenced/has_many.rb
custom_fields-2.13.1 lib/custom_fields/extensions/mongoid/association/referenced/has_many.rb
custom_fields-2.13.0 lib/custom_fields/extensions/mongoid/association/referenced/has_many.rb