Skip to content

Instantly share code, notes, and snippets.

@ryankelley
Created December 21, 2013 17:45
Show Gist options
  • Save ryankelley/8072506 to your computer and use it in GitHub Desktop.
Save ryankelley/8072506 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Linq;
using AutoMapper;
using FieldBook.Core.Persistence;
using FieldBook.Core.UI;
using FieldBook.Interface.Conventions;
using FubuCore;
using FubuMVC.Core.Registration;
using FubuMVC.Core.Registration.Nodes;
using FubuMVC.Core.Resources.Conneg;
using FubuMVC.Core.View.Rendering;
namespace FieldBook.Interface.Behaviors.VariableOutput
{
public class VariableOutputConvention : IConfigurationAction
{
public void Configure(BehaviorGraph graph)
{
graph.Actions().Where(x => (x.OutputType().CanBeCastTo<IEnumerable>() && !x.OutputType().IsString()) || x.OutputType().CanBeCastTo<AjaxResponse>())
.Each(x =>
{
OutputNode output = null;
var modelType = x.OutputType();
var view = getRenderViewNode(x);
var json = new BasicRenderJsonNode(modelType);
var variableOut = new VariableOutputNode();
if(x.HasAnyOutputBehavior())
{
output = view ?? x.OfType<OutputNode>().FirstOrDefault();
}
if (output != null)
output.ReplaceWith(variableOut);
else
{
x.AddToEnd(variableOut);
}
if (modelType.CanBeCastTo<PagedListBase>())
{
variableOut.AddOutput(a => a.RenderFormat == "paged", new PagedJsonRenderNode(modelType));
}
variableOut.AddOutput(a => a.RenderFormat == "json", json);
variableOut.AddOutput(a => a.AcceptsFormat("application/json"), json);
variableOut.AddOutput(a => a.AcceptsFormat("text/javascript"), json);
if (view != null)
{
variableOut.AddOutput(a => a.AcceptsFormat("text/html"), view);
variableOut.AddOutput(a => a.AcceptsFormat("*/*"), view);
}
// graph.Observer.RecordCallStatus(x, "Adding variable output behavior");
});
graph.Actions().Where(x => x.OutputType().CanBeCastTo<IReturnJson>())
.Each(x =>
{
OutputNode output = null;
var view = getRenderViewNode(x);
if (x.HasAnyOutputBehavior())
{
output = view ?? x.OfType<OutputNode>().FirstOrDefault();
}
var modelType = x.OutputType();
var json = new CustomRenderJsonNode(modelType);
var variableOut = new VariableOutputNode();
if (output != null)
output.ReplaceWith(variableOut);
else
{
x.AddToEnd(variableOut);
}
variableOut.AddOutput(a => a.RenderFormat == "json", json);
variableOut.AddOutput(a => a.AcceptsFormat("application/json"), json);
variableOut.AddOutput(a => a.AcceptsFormat("text/javascript"), json);
if (view != null)
{
variableOut.AddOutput(a => a.AcceptsFormat("text/html"), view);
variableOut.AddOutput(a => a.AcceptsFormat("*/*"), view);
}
else
{
variableOut.AddOutput(a => a.AcceptsFormat("*/*"), json);
}
// graph.Observer.RecordCallStatus(x, "Adding variable output behavior");
});
}
private static OutputNode getRenderViewNode(ActionCall x)
{
return x.OfType<OutputNode>().FirstOrDefault(y => y.BehaviorType.CanBeCastTo<RenderAction>());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment