I think Tim's suggestion is spot on - a separate object that manages these 
situations (both the operations on the model, plus what would normally be 
managed by observers or callbacks) is best. I generally see them referred to as 
services. See item #2 on this blog post:
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

-- 
Pat

On 31/10/2012, at 10:35 AM, Tim Uckun wrote:

> You could try a state machine approach but frankly I would probably
> wrap all that in a separate business process class so the code is all
> in one place.
> 
> 
> 
> On Wed, Oct 31, 2012 at 11:55 AM, Sebastian Porto <[email protected]> 
> wrote:
>> Hi All
>> 
>> Someone please point me in the right direction with this.
>> 
>> I would like to implement an event / listener pattern in parts of my
>> application. But I don't quite like the Observer pattern as it is as it
>> hides the flow, I will like something explicit, where you trigger events
>> manually. See made up code below:
>> 
>>    class User < ActiveRecord::Base
>>        def something
>>            trigger :on_something, name
>>        end
>>    end
>> 
>>    class UserObserver < ActiveRecord::Observer
>>        def on_something(name)
>>            ...
>>        end
>>    end
>> 
>> I have been looking at
>> 
>> custom callbacks
>> http://api.rubyonrails.org/classes/ActiveSupport/Callbacks/ClassMethods.html#method-i-define_callbacks
>> 
>> Observers
>> http://api.rubyonrails.org/classes/ActiveRecord/Observer.html
>> 
>> But nothing quite does what I want, or I don't know how to hook it together.
>> Any suggestions / gems on this will be appreciated.
>> 
>> Thanks,
>> Sebastian
>> 
>> 
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby or Rails Oceania" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/rails-oceania?hl=en.
>> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.
> 



-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to