In our settings (), there is a code block that allows us to share the code for all the actions of our application. When an action includes the Web::Action module, that block code is yielded within the context of that class. This is heavily inspired by Ruby Module and its included hook.

    Imagine we want to check if the current request comes from an authenticated user.

    We craft a module in apps/web/controllers/authentication.rb.

    It will be really tedious to include this module for all the actions of our app. We can use controller.prepare for the scope.

    Code included via prepare is available for ALL the actions of an application.

    Let’s say we have included globally, but want to skip the execution of its callback for certain resources. A typical use case is to redirect unauthenticated requests to our sign in form.

    The action will still try to invoke :authenticate!, because, technically speaking, callbacks execution can’t be skipped. But if we override that method with an empty implementation, it does nothing and our non-signedin users can reach the wanted resource.

    Imagine we have a RESTful resource named books. There are several actions (show, edit, and destroy) which need to find a specific book to perform their job.

    What if we want to DRY the code of all these actions? Ruby comes to our rescue.