Search code examples
ruby-on-railsspree

Definition of :duplicate_extra


In the Spree codebase, the class ProductDuplicator has a duplicate method:

def duplicate
  new_product = duplicate_product

  # don't dup the actual variants, just the characterising types
  new_product.option_types = product.option_types if product.has_variants?

  # allow site to do some customization
  new_product.send(:duplicate_extra, product) if new_product.respond_to?(:duplicate_extra)
  new_product.save!
  new_product
end

I am trying to figure out where :duplicate_extra is defined and how it works. The documentation isn't clear.


Solution

  • The duplicate_extra method is not defined by default. The documentation says:

    define "duplicate_extra" for site-specific actions, eg for additional fields

    It's telling you to define it on your own if you want more duplication logic, like duplicating additional fields.

    This line of code along with the comment above it:

      # allow site to do some customization
      new_product.send(:duplicate_extra, product) if new_product.respond_to?(:duplicate_extra)
    

    is clearly checking if the product responds to a method duplicate_extra which takes another product as an argument.

    The duplicate_extra method should be defined as an instance method of Product (or if you are subclassing it, you could define it at the subclass) with one argument, like this:

    class Product
      def duplicate_extra(original_product)
        # copy custom stuff from the original product to self
      end
    end
    

    You could also try asking on Spree slack for more details.