Skip to content

Instantly share code, notes, and snippets.

View swrobel's full-sized avatar

Stefan Wrobel swrobel

View GitHub Profile
@swrobel
swrobel / string_enum.rb
Created November 22, 2022 23:53
String Enum module for Rails
module StringEnum
extend ActiveSupport::Concern
module ClassMethods
# ActiveRecord enum has undocumented support for enums with string rather than integer representations.
# To enable it you have to define an enum like so:
#
# enum field: { value1: 'value1', value2: 'value2' }
#
# which is rather redundant. This is a shortcut which lets you define it as follows:
@swrobel
swrobel / copy_db.sql
Created March 23, 2012 17:41
MySQL script to copy from one database to another in pure SQL
DELIMITER $$
DROP PROCEDURE IF EXISTS `CopyDB` $$
CREATE PROCEDURE `CopyDB` (sourceDB VARCHAR(64),targetDB VARCHAR(64))
TheStoredProcedure:BEGIN
DECLARE found_count,ndx,ndx_last INT;
DECLARE sqlcmd VARCHAR(1024);
SELECT COUNT(1) INTO found_count
FROM information_schema.tables
@swrobel
swrobel / 1-OpsWorksPapertrail.md
Last active August 5, 2021 20:41
Send OpsWorks log files to Papertrail using the remote_syslog gem
  1. Add remote_syslog to your Gemfile
  2. Add papertrail.rake to lib/tasks
  3. Add remote_syslog.yml to config/
  4. Add before_restart.rb to deploy/
  5. Set the PAPERTRAIL_PORT ENV var to the one provided by Papertrail for your system

Inspiration from Scott W. Bradley

@swrobel
swrobel / finders.rb
Created April 11, 2020 23:42
Generic finders for a rails base controller
def model
@model ||= controller_name.classify.constantize
end
def record
@record ||= records.find(params[:id])
end
def records
@records ||= policy_scope(model) # Requires Pundit
@swrobel
swrobel / gist:77626ff3d4967ca65c3028dcb336d57a
Last active November 14, 2019 22:34
Rails 6.0 time zones as TZInfo identifiers
ActiveSupport::TimeZone.all.group_by(&:formatted_offset).each {|offset| puts "### UTC #{offset[0]}"; offset[1].each {|tz| puts "* #{tz.tzinfo.identifier}"}; puts "\n"};nil

UTC -11:00

  • Pacific/Pago_Pago
  • Pacific/Midway

UTC -10:00

  • Pacific/Honolulu
@swrobel
swrobel / bench.rb
Created August 3, 2019 01:59
Ruby memoization gem benchmarks
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem 'benchmark-ips'
gem 'benchmark-memory'
gem 'memo-it'
gem 'memoist'
gem 'memery'
@swrobel
swrobel / spree_1.3.3_to_2.0.4.md
Last active December 18, 2018 16:48
HOWTO: Upgrade Spree 1.3.3 to 2.0.4

HOWTO: Upgrade Spree 1.3.3 to 2.0.4

These steps were sufficient to upgrade Cult Cosmetics to Spree 2.0.4 from 1.3.3. Here are some details on our environment.

  • We only have single-variant products
  • We are not making use of stock locations (single warehouse)
  • Frontend entirely overridden w/ custom bootstrap layout
  • Braintree gateway
  • Rails 3.2.14
  • Upgraded from Ruby 1.9.3-p448 to 2.0.0-p247 in the process (no issues)
@swrobel
swrobel / README.md
Last active May 25, 2018 21:25 — forked from denvazh/README.md
Scheduled update for homebrew

Scheduled updates for homebrew

This two launchdaemon scripts provide scheduled updates and upgrade for homebrew packages.

It will run in the following way:

  • brew update every day at 12:10
  • brew upgrade every day at 12:20

How to install

@swrobel
swrobel / dedup-imovie-library
Last active September 30, 2017 20:26 — forked from kabadisha/dedup-imovie-library
When you import movies into iMovie 10 libraries, the file is always copied, wasting space and hindering editability. This script replaces the copy with a hardlink, reclaiming disk space.
#!/usr/bin/env ruby
# Usage: dedup-imovie-library LIBRARY ORIGINALS
#
# Goes through an iMovie 10 library and replaces all the "Original Media" with
# symlinks to the actual original media, in order to conserve disk space. Note
# that because they're symlinks, if the path to the originals changes (e.g. you
# rename the external drive they are on) then the links will be broken.
#
# This assumes you've already imported the files into iMovie and waited for them
@swrobel
swrobel / gemfile_changelog.rb
Created March 23, 2012 01:21
Get Changelogs for outdated gems in Gemfile
#!/usr/bin/env ruby
def changelog_for_gem(gem)
changelogs = `bundle exec gem contents #{gem}`.lines.grep(/history|changelog|news/i)
if changelogs.empty?
puts "No changelog found for gem #{gem}"
return nil
end