Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails execute code per-environment

Tags:

grails

Is there a way to do what theConfig.groovy file does, but during the code execution...
Something like:

class AController{
    def method(){
        withEnvironments{
            development{
               println 'This is execute just on development'
            }
            production {
               log.debug 'This is execute just on production'
            }
         }
     }
}

I know that I can achieve the same effect using if (Environment.current == 'development'), but is there something with that sintax???

like image 208
rascio Avatar asked Sep 28 '12 12:09

rascio


1 Answers

Found this blog post which shows one possible solution using Environment.executeForCurrentEnvironment:

import grails.util.Environment

class AController {
  def method() { 
    Environment.executeForCurrentEnvironment {
      development {
        println 'This is execute just on development'
      }
      production {
        log.debug 'This is execute just on production'
      }
    }
  }
}
like image 81
tim_yates Avatar answered Sep 28 '22 16:09

tim_yates