Skip to content

Instantly share code, notes, and snippets.

@joecorcoran
Created March 6, 2017 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joecorcoran/63605bde9e3c88ff59f401a02e2651d1 to your computer and use it in GitHub Desktop.
Save joecorcoran/63605bde9e3c88ff59f401a02e2651d1 to your computer and use it in GitHub Desktop.
# Simple user class
# We're pretending this is an ActiveRecord::Base model
class User
attr_reader :name
def initialize(name)
@name = name
end
end
# Controller class
# We're pretending this is an ActionController::Base
class Controller
def self.before_action(lmb = ->{})
@@before_actions ||= []
@@before_actions << lmb
end
def run_before_actions!(context)
@@before_actions.each do |a|
context.instance_exec(&a)
end
end
end
# Our own controller
# This is like when we make a new controller in a Rails app
class UsersController < Controller
before_action -> { puts @user.name }
def create
@user = User.new('Joe')
run_before_actions!(self)
end
end
# We make a new instance of our controller and call the #create action
controller = UsersController.new
controller.create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment