Skip to content

Instantly share code, notes, and snippets.

@jrochkind
jrochkind / gist:2161449
Created March 22, 2012 18:40
A Capistrano Rails Guide

A Capistrano Rails Guide

by Jonathan Rochkind, http://bibwild.wordpress.com

why cap?

Capistrano automates pushing out a new version of your application to a deployment location.

I've been writing and deploying Rails apps for a while, but I avoided using Capistrano until recently. I've got a pretty simple one-host deployment, and even though everyone said Capistrano was great, every time I tried to get started I just got snowed under not being able to figure out exactly what I wanted to do, and figured I wasn't having that much trouble doing it "manually".

@jrochkind
jrochkind / Explanation.md
Created October 15, 2012 17:19
Truncating html with nokogiri, with/without Rails

Rails has a handy truncate helper (which is actually mostly a method added to String ), but it warns you it's not safe to use on html source, it'll cut off end tags and such.

What if you want an HTML safe one? There are a variety of suggested solutions you can google, none of which were quite robust/powerful enough for me.

So I started with my favorite, by Andrea Singh, using nokogiri.

But:

  • I modified it to not monkey-patch Nokogiri, but be a static method instead (sadly making already confusing code yet more confusing, but I didn't want to monkey patch nokogiri)
@jrochkind
jrochkind / gist:59b6330e3f52710cc49e
Last active June 8, 2023 07:47
Monkey patch to ActiveRecord to forbid
######################
#
# Monkey patch to ActiveRecord to prevent 'implicit' checkouts. Currently tested with Rails 4.0.8, prob
# should work fine in Rails 4.1 too.
#
# If you create a thread yourself, if it uses ActiveRecord objects without
# explicitly checking out a connection, one will still be checked out implicitly.
# If it is never checked back in with `ActiveRecord::Base.clear_active_connections!`,
# then it will be leaked.
#
@jrochkind
jrochkind / slug.rb
Last active September 23, 2022 19:53
module Slug
def self.customize(field: :name)
Module.new do
define_method :to_param do
public_send(field).downcase.gsub /\W+/, '-'
end
end
end
end
@jrochkind
jrochkind / gist:2636355
Created May 8, 2012 15:31
reddit 'hot' algorithm, in ruby, with typo fixed
require 'date'
# Actually doesn't matter WHAT you choose as the epoch, it
# won't change the algorithm. Just don't change it after you
# have cached computed scores. Choose something before your first
# post to avoid annoying negative numbers. Choose something close
# to your first post to keep the numbers smaller. This is, I think,
# reddit's own epoch.
$our_epoch = Time.local(2005, 12, 8, 7, 46, 43).to_time
@jrochkind
jrochkind / report.markdown
Created December 19, 2012 15:10
Is Set faster than Array? Not neccesarily, depending on size of collection and number of reads vs writes. (MRI) (anything you notice invalid or could be done better about the way these benchmarks are done? Let us know, and give us code and results for the better way :) )
# test_for == N / 5 -- approximate array average case?
{:benchmark_iterations=>10000, :class=>Array, :test_for=>5, :num_rewrites=>1, :col_size=>10, :num_reads=>10}
0.140000 0.000000 0.140000 ( 0.141915)
{:benchmark_iterations=>10000, :class=>Set, :test_for=>5, :num_rewrites=>1, :col_size=>10, :num_reads=>10}
0.190000 0.010000 0.200000 ( 0.198503)
----
{:benchmark_iterations=>10000, :class=>Array, :test_for=>50, :num_rewrites=>1, :col_size=>100, :num_reads=>10}
@jrochkind
jrochkind / optimized_presigned_s3.rb
Created August 25, 2020 20:45
Optimized creation of S3 presigned_url in ruby
AWS_SIG4_SIGNER = Aws::Sigv4::Signer.new(
service: 's3',
region: AWS_CLIENT.config.region,
credentials_provider: SOME_AWS_CLIENT.config.credentials,
unsigned_headers: Aws::S3::Presigner::BLACKLISTED_HEADERS,
uri_escape_path: false
)
def naive_with_uri_escape_escaping(shrine_file)
# because URI.escape does NOT escape `/`, we don't need to split it,
@jrochkind
jrochkind / gist:f1510c7777de1c57e05950fc9ffd1aaa
Created August 25, 2020 20:42
ruby S3 public_url benchmark, SDK vs naive implementation
original AWS SDK public_url implementation 0.053043 0.000275 0.053318 ( 0.053782)
naive implementation 0.004730 0.000016 0.004746 ( 0.004760)
@jrochkind
jrochkind / naive_s3.rb
Created August 25, 2020 20:39
A naive ruby S3 public url generation
def naive_public_url(shrine_file)
"https://#{["#{shrine_file.storage.bucket.name}.s3.amazonaws.com", *shrine_file.storage.prefix, shrine_file.id].join('/')}"
end
naive_public_url(model.image)
#=> "https://somebucket.s3.amazonaws.com/path/to/image.jpg"
@jrochkind
jrochkind / naive_s3_rb
Created August 25, 2020 20:39
A naive ruby S3 public URL generation implementation
def naive_public_url(shrine_file)
"https://#{["#{shrine_file.storage.bucket.name}.s3.amazonaws.com", *shrine_file.storage.prefix, shrine_file.id].join('/')}"
end
naive_public_url(model.image)
#=> "https://somebucket.s3.amazonaws.com/path/to/image.jpg"