Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blowmage/4060224 to your computer and use it in GitHub Desktop.
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.
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
@blowmage
Copy link
Author

@elight
Copy link

elight commented Nov 12, 2012

Nice.

@ryanb
Copy link

ryanb commented Nov 12, 2012

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