Skip to content

Instantly share code, notes, and snippets.

View berkes's full-sized avatar

Bèr Kessels berkes

View GitHub Profile
@berkes
berkes / product_scopes_decorator.rb
Created February 28, 2012 10:36
Spree Decorator to add a custom ordering scope. Placed in "app/models/spree/product/"
Spree::Product.class_eval do
def self.simple_scopes
[
:ascend_by_random,
:ascend_by_updated_at,
:descend_by_updated_at,
:ascend_by_name,
:descend_by_name,
# Need to have master price scopes here
# This makes them appear in admin/product_groups/edit
6ff235b69503dc6c7b1df425c003413c
@berkes
berkes / typecast.php
Created June 23, 2012 22:03
gotcha with PHP typecasting
<?php
$amount = 17.50;
print (int) $amount * 100; # => 1700 # My implementation. I was wrong.
print (int) ($amount * 100); # => 1750 # After bugfix. PHP first casts, then multiplies.
php> $are_you_insane_php = (int) 'hello world'
php> var_dump($are_you_insane_php) #=> int(0)
@berkes
berkes / gotcha_isset.php
Created September 20, 2012 08:03
Isset acts weird in PHP.
<?php
function foo($bar = NULL) {
if (isset($bar)) {
print "bar is set\n";
var_dump($bar);
}
else {
print "bar is not set\n";
var_dump($bar);//If not set should throw at least a NOTICE.
@berkes
berkes / String.rb
Created October 29, 2012 10:02
String.tenderloverized
class String
# http://confreaks.com/videos/1228-aloharuby2012-keynote-rails-4-and-the-future-of-web
def t13d
if length > 2
return "#{self[0,1]}#{(length - 2)}#{self[-1,1]}"
else
return self
end
end
end
@berkes
berkes / pass_by_ref_WTF.php
Created October 29, 2012 13:46
another WTF in PHP. Makes sense, technically, but as a use it makes no sense.
<?php
function foo($bar) {
var_dump($bar);
}
foo(array()); # => array(0) {}
## Works as expected. But now...
function foo(&$bar) {
var_dump($bar);
@berkes
berkes / smartcropper_document.rb
Created November 28, 2012 14:58
smartcropper document generator
require 'RMagick'
require 'smartcropper'
class DemoChopper
def initialize width, height, filename
@width, @height, @filename = width, height, filename
@files = [{
image: ::Magick::ImageList.new(filename).resize_to_fit!(600, 1000),
<?php
class View{
var $attributes = array();
var $object;
public function __construct($object) {
$this->object = $object;
$reflection = new ReflectionClass($object);
$props = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $prop) {
@berkes
berkes / 478-backup-arbitrary-variables.patch
Created August 31, 2013 10:40
Add chapter about how to pass in arbitrary variables. This closes #478
diff --git a/Performing-Backups.md b/Performing-Backups.md
index d5e1511..472e9a2 100644
--- a/Performing-Backups.md
+++ b/Performing-Backups.md
@@ -191,6 +191,24 @@ The `backup perform` command will exit with the following status codes:
**2**: All triggers were _processed_, but some failed.
**3**: A fatal error caused Backup to exit. Some triggers may not have been processed.
+Passing Arbitrary Variables
+---------------------------