Skip to content

Instantly share code, notes, and snippets.

View pnomolos's full-sized avatar

Phil Schalm pnomolos

  • Jane.app
  • British Columbia, BC
  • 10:00 (UTC -12:00)
  • X @pnomolos
View GitHub Profile
@pnomolos
pnomolos / application_controller.rb
Last active December 11, 2015 21:18
Get OAuth-two-legged request working in Rails 3.
# Based on work from http://mifsud.me/simple-two-legged-oauth-provider-in-rails-2
# Note that you need a user with :api_key and :secret fields
require 'oauth/request_proxy/rack_request'
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def run_oauth_check
@pnomolos
pnomolos / gist:4172189
Created November 29, 2012 21:59
ActiveRecord-like .becomes(klass) method for DataMapper
module DataMapper::Resource
def becomes(klass)
became = klass.new
self.send(:properties).each do |prop|
became.instance_variable_set("#{prop.instance_variable_name}", prop.get(self))
end
became.instance_variable_set(:@_persistence_state, @_persistence_state)
became.instance_variable_set(:@errors, errors)
became.type = self.class.name.demodulize
became
@pnomolos
pnomolos / csvsplit.rb
Created October 30, 2012 18:19
Splits a CSV file into multiple, smaller chunks.
#!/usr/bin/env ruby -w
file = "products.csv"
lines_per_file = 20000
header_lines = 1
extension = File.extname(file)
basename = File.basename(file, extension)
File.open(file) do |f|
jQuery.fn.ratingSelector = function(){
return $(this).each(function(){
var stars_element = null,
rating_element = null;
stars_element = $(this).find('span.rating_stars');
rating_element = $(this).find('input');
if (stars_element.size() && rating_element.size())
stars_element.click(function(ev){
var stars_coords = stars_element.offset();
var offset = ev.pageX - stars_coords.left;
@pnomolos
pnomolos / better_ee.php
Created February 24, 2012 05:32
Better Expression Engine - the start of a class to make your life simpler when dealing with EE programmatically.
<?php
if (!class_exists('Better_EE')) {
class Better_EE {
public static $field_defaults = array(
'site_id' => 1,
'field_type' => 'text',
'field_fmt' => 'none',
'field_show_fmt' => 'n',
'field_maxl' => 500,
'field_label' => '',
@pnomolos
pnomolos / helpers.rb
Created February 21, 2012 22:57
Override dm-validations numericality validator to round rather than failing on out-of-scale floats.
module DataMapper
module Validations
class NumericalityValidator < GenericValidator
private
def value_as_string(value)
case value
when Float then value.to_d(options.fetch(:precision, nil)).round(options.fetch(:scale, 20)).to_s('F')
when BigDecimal then value.to_s('F')
else value.to_s
end
@pnomolos
pnomolos / test-case.rb
Created January 9, 2012 19:09
Non-working has 1, :through => :model relationship
require 'data_mapper'
DataMapper::Logger.new($stdout)
DataMapper.setup(:default, 'sqlite::memory:')
class Pack
include DataMapper::Resource
belongs_to :school
has n, :pack_orders
@pnomolos
pnomolos / file.php
Created January 5, 2012 17:26
Lemonstand - Minimize memory when using products
<?php
$grouped_product = new Shop_Product(null, array('no_validation' => true, 'no_column_init' => true, 'no_timestamps' => true));
$data = $_POST['Shop_Product'];
$data['grouped'] = 1;
$data['name'] = $product->name . ' ('.$attributes->name.')';
$data['url_name'] .= '_'.uniqid('', true);
$data['sku'] .= '-' . strtolower($attributes->sku.uniqid('', true));
$data['grouped_option_desc'] = $attributes->name;
$data['x_aga_ids'] = join(':', $ids);
@pnomolos
pnomolos / deploy.rb
Created January 4, 2012 16:58 — forked from jwo/deploy.rb
Capistrano with Unicorn Reloading
set :application, "yyyyyyyyyyy"
set :repository, "here-be-your-githubs"
set :scm, :git
set :branch, "master"
set :user, "xxxxxxxx"
set :scm_verbose, true
default_run_options[:pty] = true
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true
require 'rubygems'
require 'data_mapper'
::DataMapper.setup(:default,'mysql://root@127.0.0.1/test')
class CartItem
include DataMapper::Resource
belongs_to :product
property :id, Serial
end