Skip to content

Instantly share code, notes, and snippets.

View sobstel's full-sized avatar

Przemek Sobstel sobstel

View GitHub Profile
@sobstel
sobstel / EnumType.php
Created December 13, 2010 11:31
Doctrine2 EnumType
<?php
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* My custom datatype.
*/
class EnumType extends Type
{
const ENUM = 'enum';
@sobstel
sobstel / Doctrine_Hydrator_FlatArray.class.php
Created December 14, 2010 10:05
Doctrine_Hydrator_FlatArray
<?php
/**
* Doctrine::HYDRATE_SCALAR without component alias at the beginning.
* For key names it takes columns and aliases from SELECT statement.
* First column/alias from statement is used as key for whole row.
*/
class Doctrine_Hydrator_FlatArray extends Doctrine_Hydrator_ScalarDriver {
public function hydrateResultSet($stmt)
{
@sobstel
sobstel / jQuery.fn.whenLoaded.js
Created January 21, 2011 12:29
jQuery onload event for images
jQuery.fn.whenLoaded = function(fn){
return this.each(function(){
// if already loaded call callback
if (this.complete || this.readyState == 'complete'){
fn.call(this);
} else { // otherwise bind onload event
$(this).load(fn);
}
});
}
@sobstel
sobstel / will_paginate_in_rails3.example.rb
Created January 21, 2011 16:27
will_paginate query with per_page working in rails3
@articles = Article.
select('articles.*, (SUM(votes.vote = 1)/COUNT(votes.vote)) votes_ratio, COUNT(votes.vote) votes_num').
joins(:votes).
group('articles.id').
having('votes_num >= 5 AND votes_ratio >= 0.8').
order('votes_ratio DESC, votes_num DESC').
paginate(:page => params[:page], :per_page => Article.per_page)
@sobstel
sobstel / function_in_method.php
Created January 28, 2011 22:05
PHP: function in method
<?php
class FooClass {
public function foo_method() {
function foo_func() {
echo "it works, really";
}
}
}
$foo = new FooClass;
@sobstel
sobstel / lazy_assignment.as
Created February 2, 2011 09:25
Lazy assignment pattern
var i:int = 0, l:int = points.length;
while (i < l) {
i+=1;
}
@sobstel
sobstel / hirb&wirble.rb
Created February 10, 2011 20:34
hirb and wirble configuration
# Source: http://sinatra.inf.ug.edu.pl/rails4/konfiguracja
# File: ~/.irbc
require 'rubygems'
require 'wirble'
require 'hirb'
Wirble.init
Wirble.colorize
Hirb.enable
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
@sobstel
sobstel / interpret_status.rails3.rb
Created February 19, 2011 21:29
interpret_status in rails3
def interpret_status(status)
Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
end
@sobstel
sobstel / config
Created March 4, 2011 20:17
~/.mplayer/config (polish font in mplayer)
fontconfig=yes
subcp=cp1250
subfont-text-scale=4
@sobstel
sobstel / comment.rb
Created March 11, 2011 16:54
mongoid: embedded document without object_id
class Comment
include Mongoid::Document
field :name
# do not generate object_id
# for mongoid <=2.0rc7 (in newer mongoid use Mongoid.embedded_object_id = false)
def identify
end