shubhamjuneja
shubhamjuneja

Reputation: 387

Skip callbacks in amoeba gem while cloning

Consider a basic structure with class A has many B. Now while cloning A object, I want to skip callbacks of object B. How to do that? We generally do this using attr_accessor but I unable to do that too.

https://github.com/amoeba-rb/amoeba/issues/17

This issue is opened from a long time.

 class File < ApplicationRecord
   amoeba do
     enable
     include_association :attachments
   end
   has_many :attachments
 end

 class Attachment < ApplicationRecord

   attr_accessor :skip_processing

   amoeba do
     enable
     # This is wrong
     set :skip_processing => true
   end

   belongs_to :file

   after_commit :process_attachment, on: :create, unless: :skip_processing
 end

There was some error while using attr_accessor in amoeba block, I think we can use only DB values. Is there any solution for this?

Upvotes: 0

Views: 179

Answers (1)

shubhamjuneja
shubhamjuneja

Reputation: 387

Amoeba gem provide different preprocessor and one of it is customize which I used here. You can pass a lambda function or array of lambda function in which you can call methods or set attributes for the cloned object. I used it to set the attr_accessor as follow -

class Attachment < ApplicationRecord

   attr_accessor :skip_processing

   amoeba do
     enable
     customize (lambda { |original, cloned|
       # Set attr_accessor here
       cloned.skip_processing = true
     })
   end

   belongs_to :file

   after_commit :process_attachment, on: :create, unless: :skip_processing
 end

Upvotes: 1

Related Questions