Skip to content

Instantly share code, notes, and snippets.

@justinwiley
Created January 26, 2012 19:46
Show Gist options
  • Save justinwiley/1684659 to your computer and use it in GitHub Desktop.
Save justinwiley/1684659 to your computer and use it in GitHub Desktop.
DCI + Rails new and create
# ---role
module BookSeller
def create_book(params)
self.books.create! params
end
end
# ---context
class AddingBookContext
attr_accessor :user, :book
def initialize(user_id)
@user = User.find(user_id)
@book = Book.new(:user => @user)
@user.extend BookSeller
end
def execute(params)
@user.create_book(params)
end
end
# ---controller
class BookController < ApplicationController
before_filter :create_context, :only => [:new,:create]
def new
@book = @adding_book_context.book
end
def create
@adding_book_context.execute(params)
end
private
def create_context
@adding_book_context = AddingBookContext.new(current_user.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment