-1

How to override the delete method ?

This construction does not override a method that I'm doing wrong? :

def NOT_TOUCH = ['Assets', 'Dbdoc', 'Login', 'Logout']

grailsApplication.controllerClasses.each {controller ->

            if(NOT_TOUCH.findAll{it == controller.name} == []){

                controller.metaClass.delete = { Map m ->

                     ....
                }
            }
        }
igilfanov
  • 353
  • 1
  • 11
  • It isn't really clear what you are trying to do. One thing that isn't clear is why you want the `delete` method to accept a `Map` argument. Separate from that, one problem is that you probably want to manipulate `controller.clazz.metaClass` instead of `controller.metaClass`. Also, a more efficient way to deal with your `NOT_TOUCH` thing is `if(!(controller.name in NOT_TOUCH)) {...}`. – Jeff Scott Brown Mar 03 '15 at 02:56
  • Also, be aware that your runtime metaprogramming is not going to be visible from Java code or from any Groovy code that is marked with @CompileStatic. Parts of the core framework fall in each of those categories. I suggest that you post a note to https://groups.google.com/forum/#!forum/grails-dev-discuss describing what you are really trying to accomplish. Depending on what you are trying to accomplish, there is almost certainly a better way to go about it than what you describe here. – Jeff Scott Brown Mar 03 '15 at 03:02
  • Why modifier insert / update does not be set when calling instance.save? https://github.com/grails/grails-core/blob/master/grails-plugin-rest/src/main/groovy/grails/rest/RestfulController.groovy – igilfanov Mar 06 '15 at 12:50
  • I can not understand what operation is performed when overriding save "domainClazz.metaClass.save = { Map m -> ...." ? – igilfanov Mar 06 '15 at 12:57
  • This isn't the answer to your question but why is it that you want the save action to accept a `Map` argument? Even if what you are trying to do would work (it won't), I don't know what you are trying to accomplish with the `Map`. I suggest that you post a note to groups.google.com/forum/#!forum/grails-dev-discuss describing what you are really trying to accomplish. Depending on what you are trying to accomplish, there is almost certainly a better way to go about it than what you describe here. – Jeff Scott Brown Mar 09 '15 at 18:14
  • The fact that inside the method beforeInsert impossible to to save to domain other classes. – igilfanov Mar 09 '15 at 18:45
  • Accordingly construction beforeInsert, beforeUpdate, beforeDelete, afterInsert, afterUpdate, afterDelete are not self-sufficient. Can be manipulated only between fields. – igilfanov Mar 09 '15 at 18:51

1 Answers1

-1

Applicable only for redirect and respond:

def NOT_TOUCH = ['Assets', 'Dbdoc', 'Login', 'Logout']

        grailsApplication.controllerClasses.each {controller ->

            if(!(controller.name in NOT_TOUCH)){

                controller.clazz.metaClass.invokeMethod = { String name, args ->

                    ...
                }
            }
        }
igilfanov
  • 353
  • 1
  • 11
  • While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Mar 05 '15 at 18:14