THE @APP OBJECT
...or how to separate your application
from Rails, live longer and prosper
by Hubert Łępicki
@hubertlepicki

Why @app object?
- You want to build your application, not Rails
- You might decide to get rid of Rails at some point
- You want to use the same logic from different places
- You want to make your integration tests easier to write
- You want to hide classes and their implementation
The only rule of @app object
Never call it @app
Give it meaningful name
Example names
- @blog
- @shop
- @intranet
- @cms
- @player
The root object
class ApplicationController < ActiveSomethinghelper_method :intranetdef intranet@intranet ||= Intranet.new(current_user)endend
One root to rule them all
class DashboardControllerhelper_method :dashboarddef dashboard@dashboard ||= intranet.dashboardenddef show@visits = dashboard.visits.paginate ...endend
Multiple layers of objects
class Intranetdef dashboard; @dashboard ||= Dashboard.newendclass Dashboarddef stats@stats ||= DashboardStats.newendend
Nobody cares what classes your objects are
class FilesController < ApplicationControllerdef new@form_model = intranet.files.formendend
Assemble custom @app for the user
class Intranetdef initialize(user)@employee = userenddef files@files ||= if user.is_a?(Admin)FilesManager.newelseFilesViewer.newendendend
Authorization without authorization
class FilesViewerdef files_listendendclass FilesBrowser < FilesViewerdef upload_file(file)enddef remove_file(filename)endend
Easier integration tests
- Testing with real browser is hard and slow
- Write just some end-to-end tests, leave edge cases
for integration tests - Write more integration tests easier
The @app object
By Hubert Łępicki
The @app object
- 1,926