Skip to content

Instantly share code, notes, and snippets.

View fnordfish's full-sized avatar

Robert Schulze fnordfish

View GitHub Profile
@fnordfish
fnordfish / api.rb
Last active August 29, 2015 14:22
Somewhat more modular integration of Yaks 0.10 into grape 0.11 (based on https://github.com/sinetris/yaks_grape_mongoid_example)
require 'grape-swagger'
require 'grape-yaks'
module YourApp
class API < Grape::API
default_format :json_api
default_error_formatter :json
include GrapeYaks
yaks do
default_format :json_api
@fnordfish
fnordfish / bench_draper.rb
Created May 19, 2015 18:30
benchmark decorating active record collections with draper
# this code is running in an loaded rails application environment
# most likely it won't run for you.
MyLazyDecorator = Struct.new(:object) do
def name
object.name
end
end
products = Product.limit(25);
@fnordfish
fnordfish / vagrant.md
Last active December 9, 2019 20:01
My vagrant guidelines for happy devboxes

Very rough description on how I use [Vagrant][vagrant]. Uses some SSH magic to get easy git access to your private repos.

Host Setup

  • Use most recent version of [vagrant][vagrant].
  • Some tools and plugins:
    • [vagrant-dns][vagrant-dns] (MacOS only!) starts a local DNS Server, so that you can access your webserver with a domain name (not only an IP). After installation (vagrant plugin install vagrant-dns) run vagrant dns --install to initially setup some system configurations on your system. This will require you to enter your password once.
server {
listen 80;
server_name ~^(?<client>.+).fe\.project\.dev $hostname fe.project.dev;
set $root /home/vagrant/project/current/FrontApp/pub;
access_log /var/log/nginx/fe.project.dev.access.log;
error_log /var/log/nginx/fe.project.dev.error.log;
location ^~ /assets/ {
@fnordfish
fnordfish / .zprofile
Last active August 29, 2015 14:01
ZSH dotfiles
# PATH
export PATH=$HOME/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:$PATH
# Chef-DK
export PATH=$HOME/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/bin:$PATH
# clojure
export CLASSPATH=$CLASSPATH:/usr/local/Cellar/clojure-contrib/1.2.0/clojure-contrib.jar
# GO

Keybase proof

I hereby claim:

  • I am fnordfish on github.
  • I am fnordfish (https://keybase.io/fnordfish) on keybase.
  • I have a public key whose fingerprint is 9EFD F80D E29E 73CA 855A 5C61 10A3 F806 0839 E363

To claim this, I am signing this object:

@fnordfish
fnordfish / decimal.sql
Last active December 27, 2015 04:48
Test MySQL Decimal Precision.
CREATE TEMPORARY TABLE `money` ( `amount` decimal(20,17) DEFAULT NULL );
INSERT INTO `money` (`amount`) VALUES (12345.123);
-- this will always cast to 999.99999999999999999
SELECT CAST(123456.123457 AS decimal(20,17));
@fnordfish
fnordfish / image_controller.rb
Last active December 19, 2015 18:48
A common problem when creating things with file uploads is, that you like to use the newly created ID of that thing to reference the upload. E.g. storing a image asset together with some title and caption. But also when using associations etc.pp. Is there a elegant way of how to filter all file uploads from a rack/sinatra/padrino params Hash?
post :create do
upload_files = {}
params[:image].delete_if {|key, value|
upload_files[key] = value if value.respond_to?(:has_key?) && value.has_key?(:tempfile) && value[:tempfile].is_a?(Tempfile)
}
@image = Image.new(params[:image])
if (@image.save rescue false) && upload_files.empty? || @image.update(upload_files)
# ....
end
def compress_number_list *numbers
numbers.flatten.sort.uniq.inject([]) { |blocks, n|
last_block = blocks[-1]
if !last_block || last_block.last < n-1
blocks << [n]
else
blocks[-1] = [last_block.first, n]
end
blocks
}.map { |ns| ns.join('-') }.join(', ')
⮀vagrant up
[default] Importing base box 'ubuntu-cloud'...
[default] No guest additions were detected on the base box for this VM! Guest
additions are required for forwarded ports, shared folders, host only
networking, and more. If SSH fails on this machine, please install
the guest additions and repackage the box to continue.
This is not an error message; everything may continue to work properly,
in which case you may ignore this message.
[default] Matching MAC address for NAT networking...