Skip to content

Instantly share code, notes, and snippets.

@ingeniarius
Created June 13, 2012 15:57
Show Gist options
  • Save ingeniarius/2924959 to your computer and use it in GitHub Desktop.
Save ingeniarius/2924959 to your computer and use it in GitHub Desktop.
DCI yet another approach
class User < ActiveRecord::Base
end
class Book < ActiveRecord::Base
end
module CustomerRole
end
class AddToCartContext
attr_reader :customer, :book
class Customer < User
include CustomerRole
end
def self.call(customer_id, book_id)
AddToCartContext.new(customer_id, book_id).call
end
def initialize(customer_id, book_id)
@customer = Customer.find(customer_id)
@book = Book.find(book_id)
end
def call
@customer.add_to_cart(@book)
end
end
@krisleech
Copy link

Interesting, so you would use Customer.find(id) instead of User.find(id).extend(CustomerRole)?

@ingeniarius
Copy link
Author

Yes, you are right!
From one side we are using light persistence models,
from another side we can collect role's related code to modules,
and combine it at context for interaction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment