Skip to content

Instantly share code, notes, and snippets.

View ryancheung's full-sized avatar
🌝

Ryan Cheung ryancheung

🌝
  • Earth
View GitHub Profile
@audionerd
audionerd / gist:4703b4ed3ddad82999f7
Created October 24, 2014 21:47
Replacing Paperclip file basename with UUID on upload (is there a better way?)
# replace file basename with a UUID when uploaded
# e.g.: avatar.jpg becomes 41bf830f-80cf-4efe-ad90-3b29bc6708b5.jpg
# but don't regenerate a UUID each time file url is accessed
class User < ActiveRecord::Base
has_attached_file :avatar, path: ":basename.:extension"
before_validation { set_avatar_file_name }
def set_avatar_file_name
# replace any NEW filename with a UUID
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
@robertjwhitney
robertjwhitney / activeadmin_sortable.js.coffee
Created July 3, 2012 03:37
Simple drag/drop reordering of records in an ActiveAdmin view table
# http://stackoverflow.com/a/8936202
#
# ActiveAdmin already includes the necessary jquery in active_admin/base,
# so just add this to javascripts/active_admin.js after //= require active_admin/base
#
#
# Serialize and Sort
#
# model_name - you guessed it, the name of the model we are calling sort on.
# This is the actual variable name, no need to change it.
@xiy
xiy / README.md
Created May 30, 2012 01:53
Install Nginx+Unicorn on Red Hat OpenShift

Installing Nginx+Unicorn on Red Hat OpenShift

This set of scripts and config files will help you set up the awesome combination of Unicorn and Nginx as a server environment for Ruby web applications on Red Hat's OpenShift platform while I finish my cartridge.

Notes

  • Before you get started, you should read my post on how to set up Ruby 1.9 environment on OpenShift here: http://goo.gl/ufI5G This will (hopefully) get you started on building a Rails app on OpenShift the unofficial way (for now!).
@ctavan
ctavan / gist:4482825
Last active February 18, 2019 16:09
Init-script for optionally starting multiple redis instances on the same host running Ubunut/Debian. Highly inspired by the memcached init script that comes with the Ubuntu package. This is useful since redis is single-threaded.
#! /bin/bash
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
@sidane
sidane / capybara_selenium_resize_window.rb
Last active June 3, 2022 13:27
Resize browser window with Capybara Selenium
# Resize selenium browser window to avoid Selenium::WebDriver::Error::MoveTargetOutOfBoundsError errors
#
# Example usage with Rspec (in spec/support/spec_helper.rb):
#
# config.before(:each) do
# set_selenium_window_size(1250, 800) if Capybara.current_driver == :selenium
# end
#
def set_selenium_window_size(width, height)
window = Capybara.current_session.current_window.resize_to
@defunkt
defunkt / connection_fix.rb
Created November 19, 2009 20:00
MySQL server has gone away fix
# If your workers are inactive for a long period of time, they'll lose
# their MySQL connection.
#
# This hack ensures we re-connect whenever a connection is
# lost. Because, really. why not?
#
# Stick this in RAILS_ROOT/config/initializers/connection_fix.rb (or somewhere similar)
#
# From:
# http://coderrr.wordpress.com/2009/01/08/activerecord-threading-issues-and-resolutions/
@joewiz
joewiz / post-mortem.md
Last active September 3, 2023 11:57
Recovery from nginx "Too many open files" error on Amazon AWS Linux

On Tue Oct 27, 2015, history.state.gov began buckling under load, intermittently issuing 500 errors. Nginx's error log was sprinkled with the following errors:

2015/10/27 21:48:36 [crit] 2475#0: accept4() failed (24: Too many open files)

2015/10/27 21:48:36 [alert] 2475#0: *7163915 socket() failed (24: Too many open files) while connecting to upstream...

An article at http://www.cyberciti.biz/faq/linux-unix-nginx-too-many-open-files/ provided directions that mostly worked. Below are the steps we followed. The steps that diverged from the article's directions are marked with an *.

  1. * Instead of using su to run ulimit on the nginx account, use ps aux | grep nginx to locate nginx's process IDs. Then query each process's file handle limits using cat /proc/pid/limits (where pid is the process id retrieved from ps). (Note: sudo may be necessary on your system for the cat command here, depending on your system.)
  2. Added fs.file-max = 70000 to /etc/sysctl.conf
# unicorn_rails -c /data/github/current/config/unicorn.rb -E production -D
rails_env = ENV['RAILS_ENV'] || 'production'
# 16 workers and 1 master
worker_processes (rails_env == 'production' ? 16 : 4)
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
@amolkhanorkar
amolkhanorkar / PG::Error: ERROR: new encoding (UTF8) is incompatible
Last active November 29, 2023 17:57
Postgres PG::Error: ERROR: new encoding (UTF8) is incompatible
======= Prolbem =================================================================================================================
I have installed : ruby-2.0.0,postgres-9.2 , now in rails app when I execute:
rake db:create , command I get:
PG::InvalidParameterValue: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT: Use the same encoding as in the template database, or use template0 as template.
: CREATE DATABASE "my_db_name" ENCODING = 'unicode'.......
bin/rake:16:in `load'