Skip to content

Instantly share code, notes, and snippets.

@joshmcarthur
joshmcarthur / json_default.rb
Created April 7, 2014 04:51
A controller concern to set the default format to JSON if none is provided, and enforce the requested formats
# app/controllers/concerns/json_default.rb
# USAGE:
# Simply include into a controller that should be restricted to JSON format:
# `include JSONDefault`
module JSONDefault
extend ActiveSupport::Concern
included do
before_action :set_default_format, :assert_valid_format!
@joshmcarthur
joshmcarthur / gist:10013958
Created April 7, 2014 02:16
Run Passenger install script from a non-root account with rbenv installed
sudo $(rbenv which passenger-install-nginx-module)
@joshmcarthur
joshmcarthur / email.yml
Created March 31, 2014 03:39
Load email settings from a YAML configuration file
# config/email.yml
---
test:
:delivery_method: :test
:url_host: 'localhost:3000'
development:
:delivery_method: :smtp
:host: "localhost:1025"
production:
:delivery_method: :smtp
@joshmcarthur
joshmcarthur / FittedImageView.java
Last active August 29, 2015 13:55
An Android widget to fit an ImageView to the screen bounds of a device without affecting it's aspect ratio.
package com.company.sample.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* FittedImageView
* @extends ImageView
*
@joshmcarthur
joshmcarthur / load_from_doc.js
Created January 12, 2014 00:13
A Node.js script to download DOC Track JSON data from their GIS service (ArcGIS Server). Run using `node load_from_doc.js`
/* load_from_arcgis_server
* Loads JSON from ArcGIS DOC Tracks server, then parses
* it to get tracks path data in turn
*/
(function() {
var http = require('http');
var fs = require('fs');
function queryLayer(callback) {
@joshmcarthur
joshmcarthur / gist:7666812
Created November 26, 2013 21:44
Move all files matching a pattern from a series of subfolders
for folder in folder1 folder2 folder3; do mv "$folder/_file_old.html.erb" "$folder/_file_new.html.erb"; done
@joshmcarthur
joshmcarthur / model.rb
Created November 17, 2013 20:42
Complex has_one associations in Rails 4
has_one :previous_membership, -> {
paid
.where(member_id: self.member_id)
.where('activated_at < ?', self.activated_at)
.order('activated_at DESC')
}, class_name: 'Membership'
@joshmcarthur
joshmcarthur / data.json.jbuilder
Created November 13, 2013 03:16
Pass an array into method arguments
json.(member, :contact_number, :gender, :date_of_birth, *Member.communication_preferences)
@joshmcarthur
joshmcarthur / jquery.export_to_csv.js.coffee
Created October 30, 2013 00:06
Export an HTML table to a CSV file using HTMl5 Blob object, with data URI fallback
$.fn.exportToCsv = ->
export_element = $( $(this).data('export-target') )
csv_string = csvStringFrom(export_element)
if Blob
$(this).prop(
download: csvFilename(),
href: URL.createObjectURL(new Blob([csv_string], type: 'text/csv'))
)
else
@joshmcarthur
joshmcarthur / date_helper.rb
Created October 24, 2013 22:23
This Rails helper allows simple parsing of params that have originated from ActionView's `date_select` helper method into a `Date` or `DateTime` object in a consistent and clean way.
module DateHelper
def date_from_date_select_params(date_params, key)
string_from_date_select_params(date_params, key).to_date
end
def date_time_from_date_time_select_params(date_params, key)
string_from_date_select_params(date_params, key).to_datetime
end