Skip to content

Instantly share code, notes, and snippets.

View lkaczanowski's full-sized avatar

Łukasz Kaczanowski lkaczanowski

View GitHub Profile
@lkaczanowski
lkaczanowski / jquery.debounce.js
Created February 3, 2014 07:26
JQuery Debounce
(function ($) {
'use strict';
$.debounce = function (delay, fn) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
@lkaczanowski
lkaczanowski / NotifyPropertyBase.cs
Created January 28, 2014 10:41
INotifyPropertyChanged with property expressions
public class NotifyPropertyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged( Expression<Func<object>> propertyExpression )
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler( this, new PropertyChangedEventArgs( GetPropertyName( propertyExpression ) ) );
}
private string GetPropertyName( Expression<Func<object>> propertyExpression )
@lkaczanowski
lkaczanowski / install.ps1
Created October 18, 2013 07:17
Powershell nuget script to copy log4net.config file to project depending on project type
param($installPath, $toolsPath, $package, $project)
$projectItems = $project.ProjectItems
$isWPFProject = Select-String -Simple "{60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}" $project.FullName #WPF project guid
$isWinFormsProject = Select-String -Simple "FlavorProperties" $project.FullName #WinForms project doesn't support FlavorProperties so should be empty
if(([string]::IsNullOrEmpty($isWPFProject) -eq $false) -or ([string]::IsNullOrEmpty($isWinFormsProject) -eq $true)) {
#terminal app project
Write-Host "Detected terminal application project (WebForms or WPF)"
$projectItems.Item("log4net.config").Delete()
@lkaczanowski
lkaczanowski / Global.asax.cs
Created August 27, 2013 07:14
MVC Global error handling
protected void Application_Error(Object sender, EventArgs e)
{
var httpContext = ((MvcApplication)sender).Context;
var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
var currentController = " ";
var currentAction = " ";
if (currentRouteData != null)
{
/**
* WkHtmlToPdf table splitting hack.
*
* Script to automatically split multiple-pages-spanning HTML tables for PDF
* generation using webkit.
*
* To use, you must adjust pdfPage object's contents to reflect your PDF's
* page format.
* The tables you want to be automatically splitted when the page ends must
* have a class name of "splitForPrint" (can be changed).
@lkaczanowski
lkaczanowski / CustomHandleErrorAttribute.cs
Last active November 14, 2020 00:33
Custom ASP.NET MVC handle error attribute. Handles and logs all errors.
public class CustomHandleErrorAttribute : HandleErrorAttribute
{
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public override void OnException(ExceptionContext filterContext)
{
if (!filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
@lkaczanowski
lkaczanowski / ValidateGlobalAntiForgeryTokenAttribute.cs
Created January 22, 2013 13:20
ValidateGlobalAntiForgeryToken attribute (put it on controller, not separate actions)
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ValidateGlobalAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
private readonly ValidateAntiForgeryTokenAttribute validator;
private readonly AcceptVerbsAttribute acceptedVerbs;
public ValidateGlobalAntiForgeryTokenAttribute()
{
this.validator = new ValidateAntiForgeryTokenAttribute();
@lkaczanowski
lkaczanowski / HtmlHelperTests.cs
Created January 10, 2013 14:42
Creates HtmlHelper mock for testing
[TestFixture]
public class HtmlHelperTests
{
// here put some tests...
public static HtmlHelper<T> CreateHtmlHelper<T>() where T : new ()
{
ViewDataDictionary viewData = new ViewDataDictionary(new T());
var controllerContext = new ControllerContext(
@lkaczanowski
lkaczanowski / HttpContextCurrentTests.cs
Created November 30, 2012 07:23
Creates HttpContext with Session to stub HttpContext.Current
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.SessionState;
using NUnit.Framework;
namespace Mvc.Tests
{
[TestFixture]
@lkaczanowski
lkaczanowski / MvcMockHelpers.cs
Created November 29, 2012 09:56
Mvc Mocks Helper - taken from hanselman.com with added little tweaks
using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
namespace Mvc.Tests
{