...or how to separate your application 
from Rails, live longer and prosper

by Hubert Łępicki


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 < ActiveSomething
helper_method :intranet
def intranet @intranet ||= end

One root to rule them all

class DashboardController  helper_method :dashboard  def dashboard    @dashboard ||= intranet.dashboard  end
def show @visits = dashboard.visits.paginate ... endend

Multiple layers of objects

 class Intranet   def dashboard; @dashboard ||= end
class Dashboard def stats @stats ||= end end

Nobody cares what classes your objects are

class FilesController < ApplicationController  def new    @form_model = intranet.files.form  endend

Assemble custom @app for the user

class Intranet  def initialize(user)    @employee = user  end
def files @files ||= if user.is_a?(Admin) else end endend

Authorization without authorization

class FilesViewer  def files_list  endend 
class FilesBrowser < FilesViewer def upload_file(file) end
def 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
Made with