Skip to content

Instantly share code, notes, and snippets.

Benjamin Fleischer bf4

Block or report user

Report or block bf4

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bf4
bf4 / live_database_dump.rb
Last active Jun 19, 2019 — forked from njakobsen/live_database_dump.rb
Live stream a database dump (or any other STDOUT) using Rails 4. Why would you want this? If you have a large database dump and want to avoid storing it in memory as Rails streams it. This allows pipe the dump directly into the http response instead of storing it as a file, sending it, and then deleting it. Let me know what you think! I've teste…
View live_database_dump.rb
class DatabaseController < ApplicationController
def database_dump
database = Rails.configuration.database_configuration[Rails.env]["database"]
send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup")
pipe = IO.popen("pg_dump '#{database}' -F c")
stream = response.stream
while (line = pipe.read(1024)) # per https://gist.github.com/njakobsen/6257887#gistcomment-1238467
stream.write line
Thread.pass # per https://gist.github.com/njakobsen/6257887#gistcomment-1235792
@bf4
bf4 / clock
Created Apr 3, 2019
Script to run only in the specified minute range
View clock
#!/usr/bin/env bash
# Created by Benjamin Fleischer 2019
# Distributed under the MIT license
#
# Usage:
# MINUTE_RANGES="0...10 30...40" ./clock echo 'we done it'
# DEBUG=true MINUTE_RANGES="0...10 30...40" ./clock echo 'we done it'
main() {
local ranges
@bf4
bf4 / comment.md
Created Jul 8, 2014
invalid %-encoding error in application for malformed uri
View comment.md
@bf4
bf4 / Gemfile
Created Jul 2, 2013
Rails lograge and logstash request logging
View Gemfile
gem 'lograge' # more readable logs
gem 'logstash-event' # for logstash json format
gem 'mono_logger' # threadsafe logging
@bf4
bf4 / polymorphic_many_to_many_in_rails.md
Last active Nov 26, 2018
a polymorphic many-to-many association in Rails
View polymorphic_many_to_many_in_rails.md

This was my solution for a polymorphic many-to-many association

class ItemCountry < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
  belongs_to :country
  # fields are :locatable_id, :locatable_type, :country_id
end

class Title < ActiveRecord::Base
  has_many :countries, :through => :item_countries, :as => :locatable
@bf4
bf4 / bin-squash_migrations
Created Nov 12, 2015
Squash Rails migrations in this one easy step
View bin-squash_migrations
#!/usr/bin/env bash
git ls-files db/migrate/*.rb | sort | tail -1 | \
ruby -e "schema_version=STDIN.read[/\d+/]; init_schema=%(db/migrate/#{schema_version}_init_schema.rb);
%x(git rm -f db/migrate/*.rb;
mkdir db/migrate;
git mv db/schema.rb #{init_schema};
rake db:migrate;
git add db/schema.rb; git commit -m 'Squashed migrations')"
@bf4
bf4 / rails32_http_patch_support.rb
Last active Jun 4, 2018
HTTP PATCH support in Rails 3.2
View rails32_http_patch_support.rb
# Rails 3.2 support for HTTP PATCH.
fail "Remove this patch" if Rails::VERSION::MAJOR > 3
# see http://weblog.rubyonrails.org/2012/2/26/edge-rails-patch-is-the-new-primary-http-method-for-updates/
# https://github.com/rails/rails/pull/505
# Be very conservative not to monkey-patch any methods until
# the relevant files are loaded.
ActiveSupport.on_load(:action_controller) do
ActionDispatch::Request.instance_eval do
# Is this a PATCH request?
@bf4
bf4 / interview_questions.txt
Last active May 28, 2018
Interview questions
View interview_questions.txt
Good Interview Questions
"Give Him/Her the opportunity to impress you, and maybe even give you a revelation"
Ask open-ended questions, see
---> attitudes about the topic
---> how s/he thinks
---> what s/he's been thinking about lately
Q: Compare Ruby to another language.
@bf4
bf4 / jdbc_sample.rb
Created Apr 18, 2018 — forked from rwjblue/jdbc_sample.rb
Convert JDBC ResultSet into Ruby Hash with JRuby
View jdbc_sample.rb
require './lib/adsjdbc-10.10.0.28.jar'
java_import java.sql.Driver
java_import java.sql.DriverManager
java_import 'com.extendedsystems.jdbc.advantage.ADSDriver'
class AdsAdapter
def initialize(connect_string)
@connect_string = connect_string
end
You can’t perform that action at this time.