Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blowmage/4061619 to your computer and use it in GitHub Desktop.
Save blowmage/4061619 to your computer and use it in GitHub Desktop.
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
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