Skip to content

Instantly share code, notes, and snippets.

@siannopollo
siannopollo / application_record.rb
Created April 4, 2018 22:45
#delay method for ActiveJob jobs
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
include ApplicationJob::PerformableMethod
end
@siannopollo
siannopollo / stored_as_cents.rb
Created October 24, 2015 21:21
Convenience methods for storing price-like columns as cents, to avoid floating point math!
module StoredAsCents
def stored_as_cents(*methods)
methods.each do |m|
define_method "#{m}=" do |value|
cents = (value.to_f * 100).round
write_attribute "#{m}_in_cents", cents
end
define_method m do
cents = read_attribute "#{m}_in_cents"
@siannopollo
siannopollo / README.md
Last active July 13, 2022 19:58 — forked from iamatypeofwalrus/README.md
Pluck in batches

pluck_in_batches

Sometimes you need to iterate over a ton of items and you don't want the overhead of creating AR objects out of all of them. Hell, you only need a few things! Well, #pluck has your back.

But what if you want to iterate over many tonnes of items?

Pluck in batches to the rescue!

Enjoy!

@siannopollo
siannopollo / delegateController.h
Created December 10, 2013 15:33
Starting with this tutorial (http://www.cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html) I ended up with these methods to handle scrolling the view when the iOS keyboard is shown. This performs the minimum amount of scrolling needed to show the selected text field plus a 10 point padding. This was used on a view using autolay…
@interface DelegateController : UIViewController <UITextFieldDelegate> {
CGFloat keyboardAnimatedDistance;
}
@siannopollo
siannopollo / ssl.js
Created June 10, 2013 18:26
Bookmarklet to switch to SSL on any website.
javascript:(function(){if(window.location.protocol=='https:')return;else{window.location.protocol='https:'}})();
@siannopollo
siannopollo / PaginatedTable.m
Created November 3, 2012 14:47
NSTableView subclass to handle being printed and paginated properly
#import <Cocoa/Cocoa.h>
@interface PaginatedTable : NSTableView {
NSMutableArray *topBorderRows;
NSMutableArray *bottomBorderRows;
}
@end
@implementation PaginatedTable
@siannopollo
siannopollo / klass.rb
Created September 14, 2012 17:27
Anonymous class instantiation
thing = Class.new do
def boo
puts 'ahhh!'
end
end
thing.new.boo # => "ahhh!"
# without the variable
(Class.new {def hello() puts("hello, dude") end}).new.hello # => "hello, dude"
@siannopollo
siannopollo / canvas_marker.js
Created September 4, 2012 19:18
Google map marker using canvas
var canvas = document.createElement("canvas")
document.body.appendChild(canvas)
var c = canvas.getContext("2d")
c.beginPath()
c.strokeStyle = 'darkred'
c.arc(11,11,10,0.8*Math.PI,0.2*Math.PI,false)
c.stroke()
c.lineTo(11,34)
c.stroke()
@siannopollo
siannopollo / benchmark.rb
Last active February 25, 2023 07:01 — forked from chanks/benchmark.rb
Benchmark haml vs. erb
require 'rubygems'
require 'benchmark'
require 'haml'
haml_template = File.read('template.haml')
erb_template = File.read('template.erb')
Benchmark.bm do |x|
x.report('haml:') { 1000.times{ Haml::Engine.new(haml_template).render } }
x.report('erb:') { 1000.times{ ERB.new(erb_template, nil, '-').src } }
@siannopollo
siannopollo / AVItoMOV.rb
Created July 13, 2011 03:38
Encode my AVI files to MOV using ffmpegX, ruby and AppleScript
Dir[ARGV.join(" ") + '/*.AVI'].each do |movie|
command = %{
tell application "ffmpegX"
activate
set contents of the fifth text field of front window to "#{movie}"
set contents of the second text field of front window to "#{movie.sub('AVI.ff.mov', 'mov').sub('AVI', 'mov')}"
tell application "System Events"
tell process "ffmpegX" to keystroke "\r"
end tell
end tell