Last active
February 1, 2018 10:15
-
-
Save vsavkin/5555897 to your computer and use it in GitHub Desktop.
Example of using EDR
This file contains hidden or 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
#somewhere in a controller | |
CreateOrder.new(OrderRepository).create current_user, params | |
# where | |
class CreateOrder < UseCaseService | |
def initialize order_repo | |
@order_repo = order_repo | |
end | |
def create_order user, attrs | |
order = Order.new attrs[:order_attrs] | |
attrs[:items].each do |item_attrs| | |
order.build_item item_attrs | |
end | |
@order_repo.create order | |
end | |
end | |
class Order | |
include Edr::Model | |
fields :id, :amount, :deliver_at | |
attr_accessible :items | |
def build_item attrs | |
items ||= [] | |
items << Item.new attrs | |
end | |
end | |
class OrderRepository | |
class ItemRepository #this is private repo | |
end | |
def self.create order | |
with_transaction do | |
persist order | |
order.items do |item| | |
ItemRepository.persist item | |
end | |
end | |
end | |
def self.read order_id | |
order = where(id: order_id).first | |
order.items = ItemRepository.for_order(order_id) | |
order | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment