Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ActiveRecord pattern for observers
Define own subclass of ActiveModel::Observer that takes care of hooking into models and inheritance. As well as being a seemingly cleaner separation of concerns, this allows us to handle single-columnfamily inheritance properly in the context of observers. Gets rid of Cequel::Model::Observing mixin -- functionality is defined instead on Cequel::Model::Observer.
- Loading branch information
Showing
10 changed files
with
79 additions
and
78 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Cequel | ||
|
||
module Model | ||
|
||
# | ||
# This is ripped directly off of ActiveRecord::Observer | ||
# | ||
class Observer < ActiveModel::Observer | ||
|
||
protected | ||
|
||
def observed_classes | ||
klasses = super | ||
klasses + klasses.map { |klass| klass.descendants }.flatten | ||
end | ||
|
||
def add_observer!(klass) | ||
super | ||
define_callbacks klass | ||
end | ||
|
||
def define_callbacks(klass) | ||
observer = self | ||
observer_name = observer.class.name.underscore.gsub('/', '__') | ||
|
||
Cequel::Model::Callbacks::CALLBACKS.each do |callback| | ||
next unless respond_to?(callback) | ||
callback_meth = :"_notify_#{observer_name}_for_#{callback}" | ||
unless klass.respond_to?(callback_meth) | ||
klass.send(:define_method, callback_meth) do |&block| | ||
observer.update(callback, self, &block) | ||
end | ||
klass.send(callback, callback_meth) | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
class Asset | ||
|
||
include Cequel::Model | ||
|
||
key :id, :integer | ||
column :class_name, :ascii | ||
column :label, :varchar | ||
|
||
def observed!(callback) | ||
@observed ||= [] | ||
@observed << callback | ||
end | ||
|
||
def has_observed?(callback) | ||
@observed.include?(callback) | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AssetObserver < Cequel::Model::Observer | ||
def before_save(asset) | ||
asset.observed!(:before_save) | ||
end | ||
end |
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