Last active
August 29, 2015 14:18
-
-
Save demental/b9eb16ce681744fa7313 to your computer and use it in GitHub Desktop.
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 EventCanvas::Base | |
class << self | |
attr_reader :matrix | |
def context *context_classes | |
@context_classes = context_classes | |
end | |
def subscribers *subscribers_classes | |
@context_classes.each do |kls| | |
(@matrix ||= {})[kls] = subscribers_classes | |
end | |
end | |
def [](context) | |
(@instances || {})[context] ||= new(context) | |
end | |
end | |
def initialize(context) | |
@context_tree = [ *(context.is_a?(Class) ? context : context.class).ancestors ] & self.class.matrix.keys | |
end | |
def << (publisher) | |
@context_tree.each do |class_name| | |
self.class.matrix[class_name].each do |listener| | |
publisher.subscribe listener.new | |
end | |
end | |
end | |
end |
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 ReviewsController < ApplicationController | |
def edit | |
@form = submitter.form | |
@review = submitter.model | |
end | |
def update | |
Application::EventCanvas[self] << submitter | |
@form = submitter.form | |
@review = submitter.model | |
submitter.on(:success) { |review| redirect_to success_reviews_path } | |
submitter.on(:failure) { |review| render action: :edit } | |
submitter.call | |
end | |
def success | |
@page = Page.identified_by('review-success') | |
end | |
private | |
def submitter | |
@submitter ||= Reviews::Submitter.new(params) | |
end | |
end |
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 Application::EventCanvas < EventCanvas::Base | |
context ReviewsController, Rake::Task | |
subscribers Reviews::EmailSubscriber | |
context Payment::BaseController, Consultations::Reminder | |
subscribers Sms::Subscriber, Email::Subscriber | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment