Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@blowmage blowmage commented Nov 12, 2012

@elight

This comment has been minimized.

Copy link

@elight elight commented Nov 12, 2012

Nice.

@ryanb

This comment has been minimized.

Copy link

@ryanb 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