Forked from ryanb/tasks_controller_refactoring.rb
Created
November 12, 2012 20:17
Star
You must be signed in to star a gist
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
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 TasksController < ApplicationController | |
def update | |
if @task.update_attributes(params[:task]) | |
tracker = PostSaveTaskTracker.new(@task) | |
TaskPusher.new(tracker, socket_id).push_changes | |
TaskMailSender.new(tracker, current_user).deliver_email | |
# success response | |
else | |
# failure respond | |
end | |
end | |
end | |
class TaskTracker < Struct.new(:task) | |
def changed?(attribute) | |
if changes.has_key?(attribute) | |
prev_attr, curr_attr = changes[attribute] | |
prev_attr != curr_attr | |
end | |
end | |
def changes | |
task.changed_attributes | |
end | |
end | |
class PostSaveTaskTracker < TaskTracker | |
def changes | |
task.previous_changes | |
end | |
end | |
class TaskPusher < Struct.new(:tracker, :socket_id) | |
def push_changes | |
if tracker.changed?("assignee") | |
# push assignee changes | |
end | |
if tracker.changed?("project_id") | |
# push project changes | |
end | |
end | |
end | |
class TaskMailSender < Struct.new(:tracker, :recipient) | |
def deliver_email | |
if tracker.changed?("status") | |
# email status change | |
end | |
if tracker.changed?("assignee") | |
# email assignee change | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment