Skip to content
  • azul's avatar
    fix castle gates reloading in development · 043c351c
    azul authored
    Here's what was going on...
    
    When loading a page after changing a file in development the server
    would crash indicating that config/permissions.rb was loaded twice.
    Behind the scenes rails was trying to load the constant Group inside
    the ContextPagesController. So it first looked up
      ContextPagesController::Group
    and then Group itself. Both lookups fired the after_reload callbacks.
    This way the callbacks for Group were run twice leading to errors because
    duplicate gates were defined.
    
    What are we doing now?...
    
    Now we load config/permissions.rb inside a config.to_prepare block.
    Here's what the documentation says about that hook:
    ```
      to_prepare: Run after the initializers are run for all Railties
      (including the application itself), but before eager loading and
      the middleware stack is built.
      More importantly, will run upon every request in development, but
      only once (during boot-up) in production and test.
    ```
    
    We also use require_dependency instead of plain require. This way the file
    will actually be reloaded when models are reloaded.
    043c351c