Skip to content

Instantly share code, notes, and snippets.

@jtushman
Created June 10, 2010 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtushman/433337 to your computer and use it in GitHub Desktop.
Save jtushman/433337 to your computer and use it in GitHub Desktop.
# 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