Skip to content

Instantly share code, notes, and snippets.

View gabrieljoelc's full-sized avatar

Gabriel Chaney gabrieljoelc

View GitHub Profile
@gabrieljoelc
gabrieljoelc / PascalCaseToDisplayNameFilter.cs
Created November 8, 2013 17:09
For defaulting view model property display names to space delimited versions of the property name. From http://trycatchfail.com/blog/post/Building-Nice-Display-Names-From-Pascal-Case-View-Model-Names-in-ASPNET-MVC-3.aspx. See https://gist.github.com/gabrieljoelc/7374242 for the string::ToStringWithSpaces() method.
public class PascalCaseToDisplayNameFilter : IModelMetadataFilter
{
public void TransformMetadata(System.Web.Mvc.ModelMetadata metadata, IEnumerable<Attribute> attributes)
{
if (!string.IsNullOrEmpty(metadata.PropertyName) && !attributes.OfType<DisplayNameAttribute>().Any())
{
metadata.DisplayName = metadata.PropertyName.ToStringWithSpaces();
}
}
}
using System;
using System.Data.Common;
using System.Data.Entity.Migrations;
using TestingEntityFramework.Core.Extensions;
using TestingEntityFramework.Data;
using TestingEntityFramework.Data.Migrations;
namespace TestingEntityFramework.Tests.Helpers
{
public class TestDataContextFactory
@gabrieljoelc
gabrieljoelc / FilterAttributeSpecificGlobalActionFilter.cs
Last active December 29, 2015 15:59
Constructor injected global action filter example that can be used for specific actions. The original idea came from this SO answer: http://stackoverflow.com/a/4169916/34315. The alternative is implementing a custom container-specific action invoker (http://lostechies.com/jimmybogard/2010/05/03/dependency-injection-in-asp-net-mvc-filters/, http:…
using System.Linq;
using System.Web.Mvc;
namespace Gabe.Web.Common
{
/// <summary>
/// Use this for getting constructor injected dependencies on IActionFilters.
/// </summary>
/// <typeparam name="TFilterAttr"></typeparam>
/// <remarks>
@gabrieljoelc
gabrieljoelc / SemanticBlockElementsToCenterVertially.css
Last active December 30, 2015 04:39
Pretty solid way to center vertically with HTML/CSS. From http://css-tricks.com/centering-in-the-unknown/
.something-semantic {
display: table;
width: 100%;
}
.something-else-semantic {
display: table-cell;
text-align: center;
vertical-align: middle;
}
@gabrieljoelc
gabrieljoelc / HandleDomainEventsScanner.cs
Last active January 3, 2016 09:29
Structure Map scanner convention for registering concrete types that implement multiple of the same interfaces but with varying generic parameter.
public class HandleDomainEventsScanner : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
foreach(var interfaceType in type.GetInterfaces()
// see https://gist.github.com/gabrieljoelc/5706069 for IsGenericTypeAssignableFrom() extension method
.Where(t => typeof(IHandleDomainEvents<>).IsGenericTypeAssignableFrom(t)))
registry.AddType(interfaceType, type));
}
}
@gabrieljoelc
gabrieljoelc / gist:8564008
Created January 22, 2014 18:07
Testable DateTime.Now using IDisposable pattern http://www.limilabs.com/blog/testing-datetime-now
[Test]
public void CreateName_AddsCurrentTimeAtEnd()
{
using (Clock.NowIs(new DateTime(2010, 12, 31, 23, 59, 00)))
{
string name = new ReportNameService().CreateName(...);
Assert.AreEqual("name 2010-12-31 23:59:00", name);
}
}
@gabrieljoelc
gabrieljoelc / gist:11386605
Last active August 29, 2015 14:00
Sneakers gem handler error and timeout pseudo code
# if message doesn't contain max retry info then
# publish to delay (dead-letter) queue with first step
# elsif message contains retry info that's not max then
# update retry step and publish to delay dead-letter queue
# else
# send to error (dead-letter) queue
@gabrieljoelc
gabrieljoelc / partial_stubbing.rb
Created May 8, 2014 00:38
From http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9: "Partial stubbing is when you want use a “real object” in your tests but want to stub some of the methods of that object, for example to avoid hitting the network or to freeze the time. These kinds of stubs are easily added in ruby thanks to it's dynamic workings. Le…
describe Book do
it "glorifies published at" do
book = Book.new
def book.published_at
Time.new(2012, 1, 2)
end
book.glorified_published_at.must_equal "The most awesome and first Monday of the glorious year of 2012"
end
@gabrieljoelc
gabrieljoelc / acts_as_owned.rb
Last active August 29, 2015 14:02
Overkill but interesting learning experience for me about metaprogramming in Ruby.
module ActsAsOwned
def acts_as_owned(options = {})
raise 'Must define owner lambda' unless options[:owner] && options[:owner].class == Proc
define_method(:owner, &options[:owner])
end
end
ActiveRecord::Base.send :extend, ActsAsOwned
@gabrieljoelc
gabrieljoelc / .bash_profile
Created July 10, 2014 04:02
My OS X .bash_profile
export PATH=/usr/local/bin:$PATH
export PATH="$HOME/.rbenv/bin:$PATH"
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
export PATH="$HOME/bin:$PATH"
export PATH="/Applications/Postgres.app/Contents/Versions/9.3/bin:$PATH"
source ~/.git-prompt.sh