Skip to content

Instantly share code, notes, and snippets.

@nas
Created December 14, 2011 07:20
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 nas/1475598 to your computer and use it in GitHub Desktop.
Save nas/1475598 to your computer and use it in GitHub Desktop.
Before Filter
class A
def self.before_filters
@before_filters ||= []
end
def self.before_filter(callback, params={})
before_filters << params.merge(:callback => callback)
end
def self.inherited(child_class)
before_filters.each { |f| child_class.before_filters.unshift(f) }
end
def execute(action)
matched_filters = self.class.before_filters.select do |f|
f[:only].nil? || f[:only].include?(action)
end
matched_filters.each { |f| send f[:callback] }
send(action)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment