Skip to content

Instantly share code, notes, and snippets.

View stuartbates's full-sized avatar

Stuart Bates stuartbates

  • London, United Kingdom
View GitHub Profile
<?php
class m130726_082858_yii_category_point extends CDbMigration
{
public function up()
{
$this->createTable('yii_category_point', array(
'category_id'=>'int NOT NULL',
'point_id'=>'int NOT NULL',
'PRIMARY KEY (`category_id`, `point_id`)'
@stuartbates
stuartbates / barcode.rb
Created January 31, 2014 09:50
Generating random barcodes
class Barcode < ActiveRecord::Base
validates_presence_of :code
validates_uniqueness_of :code
validates_length_of :code, :is => 8
def generate_random_code
"#{self.batch}#{self.letters}#{self.numbers}"
end
@stuartbates
stuartbates / Efficient barcodes
Last active August 29, 2015 13:55
Low level PHP making barcodes
<?php
if ( !isset( $_GET['batch'] ) )
die("YOU MUST SPECIFY A BATCH PARAMETER");
$batch = $_GET['batch'];
$dbc = @mysqli_connect( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME ) or die();
$batch_size = 100000;
@stuartbates
stuartbates / AJAX POST request link
Last active August 29, 2015 13:56
We can't nest HTML forms so instead we can use AJAX to simulate the POST request
link_to 'Add to wishlist', spree.wished_product_path({:id => variant.id}), :remote => true, :method => :put
@stuartbates
stuartbates / Spree Wishlist
Created February 5, 2014 12:43
Decorated version
def create
@wished_product = Spree::WishedProduct.new(params[:wished_product])
@wishlist = spree_current_user.wishlist
if @wishlist.include? params[:wished_product][:variant_id]
@wished_product = @wishlist.wished_products.detect {|wp| wp.variant_id == params[:wished_product][:variant_id].to_i }
else
@wished_product.wishlist = spree_current_user.wishlist
@wished_product.save
end
@stuartbates
stuartbates / Spree Filters
Created March 12, 2014 10:42
Numerous examples of Spree filters
module Spree
module Core
module ProductFilters
# Example: filtering by price
# The named scope just maps incoming labels onto their conditions, and builds the conjunction
# 'price' is in the base scope's context (ie, "select foo from products where ...") so
# we can access the field right away
# The filter identifies which scope to use, then sets the conditions for each price range
#
@stuartbates
stuartbates / PHPass
Created March 20, 2014 13:16
Ruby implementation of PHPass to allow legacy passwords from Wordpress to Devise
class PHPass
def self.itoa64
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
end
def self.crypt_private(password, setting)
output = '*0'
if setting[0,2] == output
def signed_in_root_path(resource_or_scope)
unless resource.has_role?('refinery')
refinery.edit_user_path(resource)
elsif respond_to?(:admin_root_path)
refinery.admin_root_path
else
"/"
end
end
require 'json'
data = {
status: 200, # Maps to HTTP response codes (200 = OK, 404 = not found)
search: {
label: 'Apple',
image: '',
},
data: {
demographics: {
{"status":200,"search":{"label":"Apple","image":""},"data":{"demographics":{"age":{"label":"25 to 35","image":"","zscore":0,"extra":[]},"gender":{"label":"Male","image":"","zscore":0,"extra":[]},"social_grad":{"label":"Middle Class","image":"","zscore":0,"extra":[]},"region":{"label":"London","image":"","zscore":0,"extra":[]},"income":{"label":"Under &pound;5,000","image":"","zscore":0,"extra":[]},"party":{"label":"Labour","image":"https:#images.yougov.com","zscore":0,"extra":[]}},"lifestyle":{"interests":[{"label":"Walking","image":"https:#images.yougov.com","zscore":0,"extra":[],"node_id":1},{"label":"Singing","image":"https:#images.yougov.com","zscore":0,"extra":[],"node_id":1}],"hobbies":[],"sports":[],"dishes":[{"label":"Turkey Twizzlers","image":"","zscore":0,"extra":[{"label":"","image":"","zscore":0}]}],"animals":[]},"personality":{"positive_traits":[],"negative_traits":[],"cat_dog":{"label":"Cat","image":"","zscore":0,"extra":[]},"attitudes":[]},"brands":{"clothing":[],"bank":[{"label":"Natwest","ima