Skip to content

Instantly share code, notes, and snippets.

View tristanm's full-sized avatar

Tristan McHardie tristanm

View GitHub Profile
@tristanm
tristanm / digispark_temp_light.ino
Created February 18, 2016 05:25
Digispark Temp Light
void setup() {
pinMode(1, OUTPUT);
}
void loop() {
if (getTemp() > 30) {
digitalWrite(1, HIGH);
} else {
digitalWrite(1, LOW);
@tristanm
tristanm / digispark_flasher.ino
Created February 18, 2016 05:00 — forked from anonymous/digispark_flasher.ino
Digispark Flasher
void setup() {
pinMode(1, OUTPUT);
}
void loop() {
for(int i = 20; i <= 255; i++) {
analogWrite(1, i);
delay(1);
}
@tristanm
tristanm / dup.rb
Created November 5, 2015 23:00
Ruby image file duplicator
require 'fileutils'
glob_path = File.join(
Rails.application.config.retail_plus_images_path,
'*.{jpg,jpeg,png,gif}'
)
Dir.glob(glob_path).each do |src|
basename = File.basename(src, '.*')
dest = src.gsub(basename, basename + '_2')
@tristanm
tristanm / new.html.erb
Created September 22, 2015 05:08
Oversimplified example of using Offsite Payments and PxPay
<%# Example of using OffsitePayments::ActionViewHelper to generate a form %>
<%= payment_service_for 'ORD123456', '[user id]', service: :pxpay, return_url: 'http://localhost:3000/pxpay/return', credential2: '[key]', currency: 'NZD', amount: 12.34 do |service| %>
<%= submit_tag 'Pay Now' %>
<% end %>
<%# Example of generating a URL directly (see controller) %>
<%= link_to @service.credential_based_url, @service.credential_based_url %>
@tristanm
tristanm / localizable.rb
Created September 4, 2015 05:46
Concerns::Localizable
module Concerns::Localizable
extend ActiveSupport::Concern
included do
around_action :localize
end
def localize
return false if set_locale === false
yield
@tristanm
tristanm / README.md
Last active May 9, 2024 17:44
Migrating a Rails project from MySQL to PostgreSQL

Migrating a Rails project from MySQL to PostgreSQL

This brief guide is written from my own experience with migrating a large (~5GB) MySQL database to PostgreSQL for a Rails project.

No warranties, guarantees, support etc. Use at your own risk and, as always, ENSURE YOU MAKE BACKUPS FIRST!

I chose [pgloader][1] because it's extremely fast. YMMV.

  1. Replace mysql2 gem with pg in Gemfile.
  2. Update config/database.yml for PostgreSQL. I used [Rails' template][2] as a starting point.
@tristanm
tristanm / .rubocop.yml
Last active August 29, 2015 14:01
Rails' Rubocop Offenses
AllCops:
RunRailsCops: true
# Fails in 0.21.0
Delegate:
Enabled: false
@tristanm
tristanm / times_tables.rb
Last active August 29, 2015 14:00
Times Table Chart Generator
values = (0..12).to_a
values.product(values).each do |factors|
product = factors[0] * factors[1]
puts "#{factors[0]} x #{factors[1]} = #{product}"
end
@tristanm
tristanm / ordinal_filter.coffee
Last active December 24, 2015 23:19
AngularJS filter for ordinalising a number. Not thoroughly tested. Known not to work for 11, 12 and 13.
# Credit to https://github.com/jdpedrie/angularjs-ordinal-filter (which also doesn't work for 11, 12 and 13)
ghostBooking.filter "ordinal", ->
(number) ->
suffixes = ["th", "st", "nd", "rd"]
value = number % 100 # The last two digits, e.g. 12345 % 100 => 45
number + (suffixes[(l2d - 20) % 10] || suffixes[0])
@tristanm
tristanm / fuel_stars.rb
Created July 15, 2013 21:06
Random fuel stars 0-5
# Note does not ever include 5
((rand * 5) * 2).round / 2.0