Skip to content

Instantly share code, notes, and snippets.

View fabiosussetto's full-sized avatar

Fabio Sussetto fabiosussetto

View GitHub Profile
@fabiosussetto
fabiosussetto / CakePHP Partial mock for a model
Created December 17, 2010 17:44
With SimpleTest, when creating a partial mock, the real object constructor is NOT called. This make impossible to mock model in tests, because things like Behaviors, etc. will not be initialized. I (probably) solved with this:
Mock::generatePartial('Article','MockArticle', array('testCall'));
// then inside the test case class:
public function startTest($method) {
parent::startTest($method);
$this->Article = new MockArticle($this);
$this->Article->alias = 'Article';
$reflectionMethod = new ReflectionMethod('Article', '__construct');
$reflectionMethod->invoke($this->Article);
{"comment"=>{"author"=>"test",
"content"=>"test"},
"commit"=>"Create Comment",
"authenticity_token"=>"wGpdIZVeimdsUCdiu0WeeCDoXiclqK0IcGUjwIvGTfQ=",
"utf8"=>"✓",
"announce_id"=>"1-dummy-title"}
#show.js.erb
$('#comments').html('<%= escape_javascript render(@comments) %>');
$('#paginator').html('<%= escape_javascript(paginate(@comments, :remote => true).to_s) %>');
#announces_controller.rb
def show
@announce = Announce.find(params[:id])
@comments = Comment.order("created_at desc").where(:announce_id => @announce.id).includes(:attachments).page(params[:page]).per(10)
@comment = @announce.comments.build
@comment.attachments.build
#publications_controller
def show
@comments = @publication.comments.includes(:author => :user).order('created_at desc').page(params[:page]).per(10)
@comment = @publication.comments.build
end
#comments_controller
def create
@publication = Publication.find(params[:publication_id])
class Teacher < User
has_many :grades, :through => :teachings, :uniq => true
has_many :teachings
has_many :subjects, :through => :teachings, :uniq => true
def teaching_grades(organization)
grades.where('teachings.grade_id' => Grade.where(:organization_id => organization.id))
end
@fabiosussetto
fabiosussetto / gist:1571632
Created January 6, 2012 17:48
Ajax file upload
/*
* Ajax file upload experiment
*/
AjaxUploadHandler = function(o){
this._options = {
action: '/upload.php',
onProgress: function(fileName, loaded, total){},
onComplete: function(fileName, response){},
onCancel: function(id, fileName){}
@fabiosussetto
fabiosussetto / gist:2567447
Created May 1, 2012 11:24
Git colors for terminal
######### Show git branch name and info in prompt ###############
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
@fabiosussetto
fabiosussetto / gist:2917158
Created June 12, 2012 12:09
Patched webapp2 file to be used on GAE with WebOb 0.9
# -*- coding: utf-8 -*-
"""
webapp2
=======
Taking Google App Engine's webapp to the next level!
:copyright: 2011 by tipfy.org.
:license: Apache Sotware License, see LICENSE for details.
"""
@fabiosussetto
fabiosussetto / gist:3017426
Created June 29, 2012 11:23
Appengine remote API into script
#!/usr/bin/env python
#
import os
import sys
# Hardwire in appengine modules to PYTHONPATH
# or use wrapper to do it more elegantly
appengine_dirs = ['/Applications/blah/blah/google_appengine'...]
sys.path.extend(appengine_dirs)
# Add your models to path
#!/bin/bash
function actual_path() {
if [ [ -z "$1" ] -a [ -d $1 ] ]; then
echo $(cd $1 && test `pwd` = `pwd -P`)
return 0
else
return 1
fi
}