Skip to content

Instantly share code, notes, and snippets.

View bemurphy's full-sized avatar

Brendon Murphy bemurphy

View GitHub Profile
@toolmantim
toolmantim / Makefile
Last active December 5, 2022 23:14
An example of using Make instead of Grunt for fast, simple and maintainable front-end asset compilation.
# A simple Makefile alternative to using Grunt for your static asset compilation
#
## Usage
#
# $ npm install
#
# And then you can run various commands:
#
# $ make # compile files that need compiling
# $ make clean all # remove target files and recompile from scratch
@korny
korny / jquery_ujs.prompt.js.coffee
Last active July 3, 2017 19:06
Simple prompt functionality for Rails / jQuery UJS, modelled after "confirm". Tested with Rails 4 (edge).
# Usage: link_to …, prompt: { message: 'Some message', default: 'default value', param: 'name of parameter' }
# The prompt will ask for "message" and use "default" as the default value.
# Unless user selects cancel, "param"=<new value> will be sent to the given path.
# Optionally, you can just use `prompt: "message"`.
$.rails.prompt = (message, defaultValue) ->
window.prompt message, defaultValue
$.rails.handlePrompt = (element) ->
config = element.data 'prompt'
@jonkgrimes
jonkgrimes / unicorn.rb
Last active November 18, 2016 18:44
Unicorn.rb for keen.io configuration
worker_processes 3 # amount of unicorn workers to spin up
timeout 30 # restarts workers that hang for 30 seconds
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
before_fork do |server,worker|
defined?(ActiveRecord::Base) and
@maxivak
maxivak / resize_nocrop_noscale
Last active April 25, 2023 03:27
Crop and resize an image using MiniMagick in Ruby on Rails. Two approaches.
def resize_nocrop_noscale(image, w,h)
w_original = image[:width].to_f
h_original = image[:height].to_f
if w_original < w && h_original < h
return image
end
# resize
image.resize("#{w}x#{h}")
@rkh
rkh / chat.rb
Created December 14, 2011 12:55
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
def daemonize(name = File.basename($0), options = {})
pid_path = options[:pid_path] || File.expand_path("tmp/pids/#{name}.pid")
# If the pid file exists, check that the process is actually still running.
begin
if File.exists?(pid_path) && Process.kill(0, File.read(pid_path).to_i)
$stderr.puts "Already running."
exit 1
end
rescue Errno::ESRCH
@alobato
alobato / find_replace
Created May 11, 2010 22:46
Find and replace text in multiple files
# Find and replace text in multiple files
# http://www.24hourapps.com/2009/03/linux-tips-17-find-and-replace-text-in.html
# Multiple file find and replace is a rarely used, but an extremely time saving, ability of
# Linux that I cannot live without. It can be achieved by chaining a few commands together
# to get the list of files you want to change (find), make sure the files contain the strings
# you want to replace (grep), and do the replacements (sed).
# Lets say we have a lot of code that uses the function registerUser that was implemented when
# there was only one class of users, but now another class of users need to access the system