Created
June 10, 2010 17:37
-
-
Save jtushman/433337 to your computer and use it in GitHub Desktop.
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
# In my controller | |
monitor :only => [:create,:destroy], | |
:alias => {:create => :voted}, | |
:target => :voteable | |
# Calls this method in my ActivityLogger module | |
def monitor(options = {}) | |
if !options.has_key?(:only) && !options.has_key?(:except) | |
options[:only] = [:show,:create,:update,:destroy] | |
end | |
noun = self.name.sub(/(s)Controller$/,"") | |
Activity.targets[noun] = options[:target] if options[:target] | |
Activity.add_action_aliases(noun,options[:alias]) | |
after_filter(options.slice(:only,:except)) do |c| | |
c.log_action(options.except(:only,:except)) | |
end | |
end | |
# And in my Activity class | |
def self.action_aliases | |
@@action_aliases ||= {} | |
end | |
def self.targets | |
@@targets ||= {} | |
end | |
def self.add_action_aliases(noun,aliases) | |
base_aliases = default_aliases.clone | |
(aliases || []).each do |key,value| | |
base_aliases.delete(key) | |
base_aliases[key] = value | |
end | |
base_aliases.each do |key,value| | |
action_aliases.delete(noun.to_s + "/" + key.to_s) | |
action_aliases[noun.to_s + "/" + key.to_s] = value | |
end | |
end | |
private | |
def self.default_aliases | |
{ | |
:create => :created, | |
:show => :viewed, | |
:destroy => :deleted, | |
:update => :updated | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment