Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / pbcopy.md
Created May 4, 2014 21:14
Copy terminal text to clipboard

Variant 1:

pbcopy < file.txt

Variant 2:

command | pbcopy
pp (YourClass.methods - Object.methods).sort
@joshmcarthur
joshmcarthur / README.md
Last active August 29, 2015 14:03
A6 Printing Template

About

The file attached to this Gist in XCF format is a template ready for professional printing with correct bleed and safety margins applied for editing in GIMP. Feel free to download and use it for your own projects. I've created this file because while I found a bunch of instructions on how to set up the file, I couldn't find a good starter. Here it is! I've followed the instructions on The Online Printer's guide to preparing GIMP files, but I also welcome any additional tips.

Usage

  1. Download the file
  2. Make changes as required:
  3. Treat the first level of guides as the edge of your image. This is the bleed line, and you can expect that this would normally be cropped.
  4. Treat the second level of guides as the safety margin. You should not have any important content or imagery crossing this margin.
@joshmcarthur
joshmcarthur / csv_renderer.rb
Created July 21, 2014 00:43
Support rendering CSV content directly as an attachment
# Save this in config/initializers/csv_renderer.rb
ActionController::Renderers.add :csv do |obj, options|
filename = options[:filename] || 'data.csv'
str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
send_data str,
type: Mime::CSV,
disposition: "attachment",
filename: filename
end
@joshmcarthur
joshmcarthur / -
Created August 1, 2014 06:33
Allow using capital letters in API module
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
# app/controllers/api/widgets_controller.rb
module API
class WidgetsController < BaseController
end
end
@joshmcarthur
joshmcarthur / model.rb
Last active August 29, 2015 14:05
Safe assignment of unique token with collision checking
def set_access_token
begin
self.access_token = Devise.friendly_token[0..12]
end while ClientForm.exists?(access_token: self.access_token)
end
@joshmcarthur
joshmcarthur / spec_helper.rb
Last active August 29, 2015 14:05
Disable logging from Authority while running tests (e.g. `User #123 is not authorized...`)
# Turn off authority logging while running tests
Authority.configure do |config|
config.logger = Logger.new('/dev/null')
end