ruby on rails - How to edit a method of a gem? -


i'm using gem acts_as_commentable_with_threading, , i'd add destroy method. currently, if delete comment , has replies delete comment , it's replies. i'd keep function root comment, not children. if this

     comment 1      /        \       \      comment 4     comment 2        \         \       comment 3 

where comment 2, 3, , 4 children of 1, 3 child of 2. want make if delete comment 2, comment 3 still there. however, keep if comment 1 deleted of comments under deleted because comment 1 root comment. have edit destroy method in gem allow this. how go doing this? (not asking how logic rather can edit method, i'd appreciate on logic)

you can via monkey patching, involves defining additional aspects of class or module within file. place put this, i've found, in config/initializers.

so, if want overwrite destroy method of class a::b, make file says like:

require 'loads_a_b' module   class b     def destroy_with_child_preservation       # code     end     alias_method_chain :destroy, :child_preservation   end end 

and can refer original method calling destroy_without_child_preservation


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -