Permalink
Browse files

Added support for both pascal casing and underscored action method na…

…mes.
  • Loading branch information...
1 parent 2ac5678 commit 57cfa96ae7ce03c55e25eeb74a20d1500760b414 @casualjim casualjim committed Mar 22, 2009
@@ -53,12 +53,12 @@ private void PopulateLookupTables(IEnumerable<string> methodNames)
{
var methodAliases = (Hash)_rubyEngine.CallMethod(ControllerClass, "name_selectors");
AliasedMethods = methodAliases.Map(pair => KeyValuePairFor(pair));
- NonAliasedMethods = methodNames.Where(method => AliasedMethods.DoesNotContain(pair => String.Equals(pair.Key, method, StringComparison.OrdinalIgnoreCase)));
+ NonAliasedMethods = methodNames.Where(method => AliasedMethods.DoesNotContain(pair => String.Equals(pair.Key, method.Underscore(), StringComparison.OrdinalIgnoreCase) || String.Equals(pair.Key, method.Pascalize(), StringComparison.OrdinalIgnoreCase)));
}
- private KeyValuePair<string, PredicateList> KeyValuePairFor(KeyValuePair<object, object> pair)
+ private static KeyValuePair<string, PredicateList> KeyValuePairFor(KeyValuePair<object, object> pair)
{
- return new KeyValuePair<string, PredicateList>(pair.Key.ToString(), new PredicateList(_rubyEngine, (RubyArray) pair.Value));
+ return new KeyValuePair<string, PredicateList>(pair.Key.ToString(), new PredicateList((RubyArray) pair.Value));
}
private static AmbiguousMatchException CreateAmbiguousMatchException(string actionName)
@@ -70,7 +70,7 @@ public string FindActionMethod(ControllerContext controllerContext, string actio
{
PopulateLookupTables(controllerContext); // dynamic languages can add methods at runtime
var methodsMatchingName = GetMatchingAliasedMethods(controllerContext, actionName);
- methodsMatchingName.AddRange(NonAliasedMethods.Where(name => String.Equals(name, actionName, StringComparison.OrdinalIgnoreCase)));
+ methodsMatchingName.AddRange(NonAliasedMethods.Where(name => String.Equals(name, actionName.Underscore(), StringComparison.OrdinalIgnoreCase) || String.Equals(name, actionName.Pascalize(), StringComparison.OrdinalIgnoreCase)));
var finalMethods = RunSelectionFilters(controllerContext, methodsMatchingName);
switch (finalMethods.Count)
@@ -104,7 +104,7 @@ private List<string> GetMatchingAliasedMethods(ControllerContext controllerConte
private List<string> RunSelectionFilters(ControllerContext controllerContext, IEnumerable<string> matchingMethods)
{
var filtersDescriptions = (Hash) _rubyEngine.CallMethod(ControllerClass, "method_selectors");
- var filters = filtersDescriptions.Where(pair => matchingMethods.Contains(pair.Key.ToString())).Map(pair => KeyValuePairFor(pair));
+ var filters = filtersDescriptions.Where(pair => matchingMethods.Contains(pair.Key.ToString().Underscore()) || matchingMethods.Contains(pair.Key.ToString().Pascalize())).Map(pair => KeyValuePairFor(pair));
return filters.Count() == 0
? new List<string>(matchingMethods)
@@ -120,11 +120,9 @@ public class PredicateList : IEnumerable<Func<ControllerContext, string, bool>>
{
private readonly RubyArray _items;
private readonly List<Func<ControllerContext, string, bool>> _predicates = new List<Func<ControllerContext, string, bool>>();
- private readonly IRubyEngine _rubyEngine;
- public PredicateList(IRubyEngine rubyEngine, RubyArray items)
+ public PredicateList(RubyArray items)
{
- _rubyEngine = rubyEngine;
_items = items;
Populate();
}
@@ -136,7 +134,7 @@ private void Populate()
private void Add(Proc proc)
{
- _predicates.Add(_rubyEngine.ConvertProcToFunc<bool>(proc));
+ _predicates.Add(ConvertProcToFunc<bool>(proc));
}
public bool IsValid(ControllerContext context, string name)
@@ -146,7 +144,12 @@ public bool IsValid(ControllerContext context, string name)
public bool IsValidForAction(ControllerContext context, string name)
{
- return _predicates.Any(predicate => predicate(context, name));
+ return _predicates.Any(predicate => predicate(context, name.Underscore())|| predicate(context, name.Pascalize()));
+ }
+
+ public Func<ControllerContext, string, TResult> ConvertProcToFunc<TResult>(Proc proc)
+ {
+ return (context, name) => (TResult)proc.Call(context, name);
}
#region Implementation of IEnumerable
@@ -49,7 +49,6 @@ private void PopulateParams()
_params =
new Dictionary<object, object>(ControllerContext.RouteData.Values.Count +
request.QueryString.Count + request.Form.Count);
-
PopulateParamsWithRouteData();
PopulateParamsWithQueryStringData(request);
@@ -146,12 +145,12 @@ public new ViewResult View(string viewName, string masterName, object model)
vdd["__scriptRuntime"] = ((RubyEngine) _engine).Runtime;
_engine.CallMethod(this, "fill_view_data");
-
foreach (var entry in _viewData)
vdd[Convert.ToString(entry.Key, CultureInfo.InvariantCulture)] = entry.Value;
-
+
var hash = model as Hash;
vdd.Model = (hash != null) ? new HashWrapper(hash) : model;
+ ModelState.ForEach(pair => vdd.ModelState.Add(pair.Key.ToString(), pair.Value));
return new ViewResult {ViewName = viewName, MasterName = masterName, ViewData = vdd, TempData = TempData};
}
@@ -230,6 +230,10 @@ class Controller < System::Web::Mvc::IronRuby::Controllers::RubyController
def fill_view_data
instance_variables.each { |varname| view_data.add(varname[1..-1], instance_variable_get(varname.to_sym)) }
end
+
+ def post?
+ controller_context.http_context.request.http_method.to_s.downcase.to_sym == :post
+ end
end
@@ -30,14 +30,6 @@ public interface IRubyEngine
RubyController ConfigureController(RubyClass rubyClass, RequestContext requestContext);
/// <summary>
- /// Converts a proc object to a .NET Func.
- /// </summary>
- /// <typeparam name="TResult">The type of the result.</typeparam>
- /// <param name="proc">The proc.</param>
- /// <returns></returns>
- Func<ControllerContext, string, TResult> ConvertProcToFunc<TResult>(Proc proc);
-
- /// <summary>
/// Calls the method.
/// </summary>
/// <param name="receiver">The receiver.</param>
@@ -9,6 +9,7 @@
using System.Web.Mvc.IronRuby.Extensions;
using System.Web.Mvc.IronRuby.ViewEngine;
using System.Web.Routing;
+using System.Web.Security;
using IronRuby;
using IronRuby.Builtins;
using IronRuby.Runtime;
@@ -126,17 +127,6 @@ public RubyController ConfigureController(RubyClass rubyClass, RequestContext re
return controller;
}
- /// <summary>
- /// Converts a proc object to a .NET Func.
- /// </summary>
- /// <typeparam name="TResult">The type of the result.</typeparam>
- /// <param name="proc">The proc.</param>
- /// <returns></returns>
- public Func<ControllerContext, string, TResult> ConvertProcToFunc<TResult>(Proc proc)
- {
- return (context, name) => (TResult) proc.Call(context, name);
- }
-
public string GetMethodName(object receiver, string message)
{
var methodNames = Operations.GetMemberNames(receiver);
@@ -156,7 +146,7 @@ public string GetMethodName(object receiver, string message)
/// <returns></returns>
public object CallMethod(object receiver, string message, params object[] args)
{
- return Operations.InvokeMember(receiver, GetMethodName(receiver, message), args);
+ return Operations.InvokeMember(receiver, GetMethodName(receiver, message));
}
/// <summary>
@@ -271,7 +261,7 @@ private void Initialize()
Context = Ruby.GetExecutionContext(Engine);
CurrentScope = Engine.CreateScope();
Operations = Engine.CreateOperations();
- LoadAssemblies(typeof (object), typeof (Uri), typeof (HttpResponseBase), typeof (RouteTable), typeof (Controller), typeof (RubyController));
+ LoadAssemblies(typeof (object), typeof (Uri), typeof (HttpResponseBase), typeof(MembershipCreateStatus), typeof (RouteTable), typeof (Controller), typeof (RubyController));
AddLoadPaths();
DefineReadOnlyGlobalVariable(Constants.SCRIPT_RUNTIME_VARIABLE, Engine);
RequireControllerFile();
@@ -20,7 +20,7 @@
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
- <OutputPath>..\IronRubyMvcLibrarySpecs\bin\</OutputPath>
+ <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 57cfa96

Please sign in to comment.