-
-
Save blowmage/4060224 to your computer and use it in GitHub Desktop.
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks or reimplementing dirty tracking.
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
class TasksController < ApplicationController | |
def update | |
if @task.update_attributes(params[:task]) | |
TaskPusher.new(@task, socket_id).push_changes | |
TaskMailSender.new(@task, current_user).deliver_email | |
# success response | |
else | |
# failure respond | |
end | |
end | |
end | |
class TaskPusher < Struct.new(:task, :socket_id) | |
def push_changes | |
if changed?("assignee") | |
# push assignee changes | |
end | |
if changed?("project_id") | |
# push project changes | |
end | |
end | |
def changed?(attribute) | |
if changes = task.previous_changes[attribute] | |
changes.first != changes.last | |
end | |
end | |
end | |
class TaskMailSender < Struct.new(:task, :recipient) | |
def deliver_email | |
if changed?("status") | |
# email status change | |
end | |
if changed?("assignee") | |
# email assignee change | |
end | |
end | |
def changed?(attribute) | |
if changes = task.previous_changes[attribute] | |
changes.first != changes.last | |
end | |
end | |
end |
Nice.
Just realized a big issue with using previous_changes
. What if the TaskPusher ends up saving the task again? Then the TaskMailSender would not detect the earlier changes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And keeping the tracker object.