Skip to content

Instantly share code, notes, and snippets.

@fxposter
Forked from scaint/product.rb
Last active August 29, 2015 13:58
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 fxposter/9995722 to your computer and use it in GitHub Desktop.
Save fxposter/9995722 to your computer and use it in GitHub Desktop.
class Product < ActiveRecord::Base
end
class ProductController < ApplicationController
# ...
def create
creates_product = CreatesProduct.new(Product, global_or_new_publisher)
product = creates_product.call(product_params)
# ...
end
end
class CreatesProduct
def initialize(factory, events)
@factory = factory
@events = events
end
def call(product_params)
product = @factory.new(product_params)
# do domething
@events.broadcast(:before_save, product)
# do domething
result = product.save
# do domething
@events.broadcast(:after_save, product, result)
# do domething
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment