Permalink
Browse files

Added support for supplying strings and symbols for filters too

Signed-off-by: Jimmy Schementi <jschementi@gmail.com>
  • Loading branch information...
1 parent 409735a commit eca88ec1c0708cf2cd6a3342b55143caef2eaa40 @casualjim casualjim committed with Mar 5, 2009
@@ -71,39 +71,49 @@ def on_action_executed(context)
module ClassMethods
- def before_action(name, &b)
- filter(name, RubyProcActionFilter.new(b, nil))
+ def before_action(name, method_name=nil, &b)
+ impl = create_from_name(method_name) if method_name.is_a?(Symbol) or method_name.is_a?(String)
+ filter(name, RubyProcActionFilter.new(impl || b, nil))
end
- def after_action(name, &b)
- filter(name, RubyProcActionFilter.new(nil, b))
+ def after_action(name, method_name=nil, &b)
+ impl = create_from_name(method_name) if method_name.is_a?(Symbol) or method_name.is_a?(String)
+ filter(name, RubyProcActionFilter.new(nil, impl || b))
end
def around_action(name, options={}, &b)
options[:before] ||= b if block_given?
options[:after] ||= b if block_given?
+ options[:before] ||= create_from_name(options[:before]) if options[:before].is_a?(Symbol) or options[:before].is_a?(String)
+ options[:after] ||= create_from_name(options[:after]) if options[:after].is_a?(Symbol) or options[:after].is_a?(String)
filter(name, RubyProcActionFilter.new(options[:before], options[:after]))
end
- def authorized_action(name, &b)
- filter(name, RubyProcAuthorizationFilter.new(&b))
+ def authorized_action(name, method_name=nil, &b)
+ impl = create_from_name(method_name) if method_name.is_a?(Symbol) or method_name.is_a?(String)
+ filter(name, RubyProcAuthorizationFilter.new(impl || b))
end
- def exception_action(name, &b)
- filter(name, RubyProcExceptionFilter.new(&b))
+ def exception_action(name, method_name=nil, &b)
+ impl = create_from_name(method_name) if method_name.is_a?(Symbol) or method_name.is_a?(String)
+ filter(name, RubyProcExceptionFilter.new(b))
end
- def before_result(name, options={}, &b)
- filter(name, RubyProcResultFilter.new(&b))
+ def before_result(name, method_name=nil, options={}, &b)
+ impl = create_from_name(method_name) if method_name.is_a?(Symbol) or method_name.is_a?(String)
+ filter(name, RubyProcResultFilter.new(b))
end
- def after_result(name, options={}, &b)
- filter(name, RubyProcResultFilter.new(nil, &b))
+ def after_result(name, method_name=nil, options={}, &b)
+ impl = create_from_name(method_name) if method_name.is_a?(Symbol) or method_name.is_a?(String)
+ filter(name, RubyProcResultFilter.new(nil, b))
end
- def around_result(name, options={}, &b)
+ def around_result(name, method_name=nil, options={}, &b)
options[:before] ||= b if block_given?
options[:after] ||= b if block_given?
+ options[:before] ||= create_from_name(options[:before]) if options[:before].is_a?(Symbol) or options[:before].is_a?(String)
+ options[:after] ||= create_from_name(options[:after]) if options[:after].is_a?(Symbol) or options[:after].is_a?(String)
filter(name, RubyProcResultFilter.new(options[:before], options[:after]))
end
@@ -112,8 +122,9 @@ def filter(name, options=nil)
klass = nil
klass = name.new if name.is_a? Class
klass = options.new if options.is_a? Class
+ klass = Object.const_get(options.to_s.split('_').map {|word| word = word.capitalize }.join('')) if options.is_a?(Symbol) or options.is_a?(String)
klass ||= options
- name = :controller if name.nil? or name.is_a?(Class)
+ name = :controller if name.nil? or name.is_a?(Class)
@action_filters[name.to_sym] ||= []
@action_filters[name.to_sym] << klass
end
@@ -123,6 +134,11 @@ def action_filters
@action_filters
end
+ private
+ def create_from_name(name)
+ lambda {|context| context.controller.send(name.to_sym, context) }
+ end
+
end
def self.included(base)
@@ -14,11 +14,6 @@ internal class Constants
public const string FILTERS_PASCAL_PATH_FORMAT = @"~\Filters\{0}.rb";
public const string FILTERS_UNDERSCORE_PATH_FORMAT = @"~\Filters\{0}.rb";
- public const string GET_ACTIONMETHOD_SCRIPT =
- @"controller = {0}.new
-controller.Initialize $request_context
-controller.method :{1}";
-
public const string MODELS = "Models";
public const string REQUEST_CONTEXT_VARIABLE = "request_context";
public const string RUBYCONTROLLER_FILE = "System.Web.Mvc.IronRuby.Controllers.controller.rb";
@@ -6,11 +6,4 @@ public enum ReaderType
AssemblyResource
}
- public enum OnExecuting
- {
- BeforeAction,
- AfterAction,
- BeforeResult,
- AfterResult
- }
}
@@ -106,6 +106,7 @@
<Compile Include="Controllers\RubyControllerFactory.cs" />
<Compile Include="Core\RubyEngine.cs" />
<Compile Include="Core\VirtualPathProvider.cs" />
+ <Compile Include="Core\VirtualPathStreamContentProvider.cs" />
<Compile Include="Extensions\FilterInfoExtensions.cs" />
<Compile Include="Extensions\IEnumerableExtensions.cs" />
<Compile Include="Extensions\ObjectExtensions.cs" />
@@ -23,9 +23,15 @@ class HomeController < Controller
context.request_context.http_context.response.write("Hello world<br />")
end
+ before_action :index, :method_filter
+
filter :index, MyFilter
def index
view(nil, 'layout', HomeModel.new)
end
+
+ def method_filter(context)
+ context.request_context.http_context.response.write("From method filter<br />")
+ end
end
View
10 README
@@ -22,6 +22,12 @@ class YourController < Controller
# do some filtering stuff here
end
+ around_result :index do |context|
+ # do some result filtering stuff here
+ end
+
+ before_action :index, :method_filter
+
authorized_action :some_other_action do |context|
# do some authorization checking work here
end
@@ -39,6 +45,10 @@ class YourController < Controller
def some_other_action
# index action
end
+
+ def method_filter
+ # do some filter stuff here
+ end
end

0 comments on commit eca88ec

Please sign in to comment.