Created
June 13, 2012 15:57
-
-
Save ingeniarius/2924959 to your computer and use it in GitHub Desktop.
DCI yet another approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Interesting, so you would use Customer.find(id) instead of User.find(id).extend(CustomerRole)?