Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
carlhoerberg / Rakefile
Created December 1, 2010 22:06
Resque + New Relic
task :environment do
DataMapper.setup(:default, ENV['DATABASE_URL'])
AWS::S3::Base.establish_connection!(
:access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
:secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'])
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Pony.options = {
:via => :smtp,
:via_options => {
@carlhoerberg
carlhoerberg / Test.cs
Created December 22, 2010 15:01
Test IModel in RabbitMQ.Client
[TestFixture]
public class RabbitMQTest
{
[Test]
public void CanMockMQ()
{
// Arrange
var channelMock = new Mock<IModel>();
channelMock
.Setup(m => m.BasicConsume(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<IDictionary>(), It.IsAny<IBasicConsumer>()))
@carlhoerberg
carlhoerberg / AesCryptoProvider.cs
Created January 13, 2011 15:59
Implementation of an encrypted string user type
class AesCryptoProvider : ICryptoProvider
{
private string password;
public AesCryptoProvider(string password)
{
this.password = password;
}
public byte[] Encrypt(string inputText)
@carlhoerberg
carlhoerberg / EncryptedColumnInEntity.cs
Created January 13, 2011 16:14
Make the encryption explicit in the domain model
public class EncryptedColumnInEntity : Entity
{
protected virtual byte[] EncryptedText { get; set; }
/// <summary>
/// Decryptes the text stored in the database
/// </summary>
/// <exception cref="CryptographicException">Throws CryptographicException when the password isn't valid</exception>
/// <param name="password">The password which the text was encrypted with</param>
/// <returns>Decrypted text</returns>
@carlhoerberg
carlhoerberg / global.asax.cs
Created March 15, 2011 10:38
How to get precompiled mvc 3 site working in Mono 2.10
public class MonoWebFormViewEngine : WebFormViewEngine
{
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
return base.FileExists(controllerContext, virtualPath.Replace("~", ""));
}
}
public class MonoRazorViewEngine : RazorViewEngine
{
@carlhoerberg
carlhoerberg / Global.asax.cs
Created April 5, 2011 07:12
Replace log4net replacement for Elmah
protected void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
var log = LogManager.GetLogger(GetType());
var msg = string.Format("\r\nPath: {0}\r\nMethod: {4}\r\nUser: {1}\r\nForm: {2}\r\nReferer: {3}",
Request.Url,
User.Identity.Name,
Request.Form,
Request.UrlReferrer,
Request.HttpMethod);
public class AutocompleteController : Controller
{
private readonly IRepository db;
public AutocompleteController(IRepository db)
{
this.db = db;
}
public JsonResult Users(string term)
{
@carlhoerberg
carlhoerberg / gist:976228
Created May 17, 2011 09:59
How to list DataMapper enum options
class Entity
include DataMapper::Resource
property :enum_property, Enum[:a, :b, :c]
end
Entity.enum_property.options[:flags] #=> [:a, :b, :c]
# or
Entity.enum_property.flag_option.values #=> [:a, :b, :c]
@carlhoerberg
carlhoerberg / Rakefile.rb
Created May 17, 2011 19:33
How to do automatic backup with Heroku PGBackups and Heroku Cron. http://carlhoerberg.com/automatic-backup-of-heroku-database-to-s3
require 'aws/s3'
require 'heroku'
require 'heroku/command'
require 'heroku/command/auth'
require 'heroku/command/pgbackups'
task :cron do
class Heroku::Auth
def self.client
Heroku::Client.new ENV['heroku_login'], ENV['heroku_passwd']
@carlhoerberg
carlhoerberg / gist:1146796
Created August 15, 2011 13:50
Optional validation
[HttpPost]
public ActionResult Create(int id, Projekt model)
{
var p = db.Get<Projekt>(id);
if (p.Typ1Validering)
if (string.IsNullOrEmpty(model.Field))
ModelState.AddModelError("Field", "Fältet får inte vara tomt");
if (ModelState.IsValid)
{