Skip to content

Instantly share code, notes, and snippets.

@schnittchen
schnittchen / include_constants.rb
Created October 13, 2012 15:53
A Ruby quirk with include. Quite annoying.
module A
module B
end
module Wrapper
module C
end
end
#Cite:
#When this module is included in another, Ruby calls append_features in this
@schnittchen
schnittchen / comments
Created November 5, 2012 10:02
Using backup gem in rails project with separate Gemfile
Note: the full file names are config/backup/Gemfile and config/deploy.rb, which gist.github.com can't handle ;-)
Of course, you will need a backup config file, preferrably in config/backup/config.rb, and you need to trigger the backup from cron (check out the whenever gem if you want that behaviour bundled with your rails app).
In both files, you need to take the locations of stuff (rails root, backup config file) into consideration.
# in m/m_q.rb
module M
module MQ
end
end
# in m/m2.rb
module M
@schnittchen
schnittchen / routes.rb
Created March 3, 2013 21:06
Routing constraints tied to your app without breaking reloading in development mode
# Sometimes Constraints need to interact with controllers (fex. in order not to query the database twice), then I like
# to put them inside the controller. If you load controllers from your routes.rb (more precisely, when routes.rb is loaded),
# these won't reload in development mode.
# If your constraints live somewhere else (say, /app/constraints), it's basically all the same.
# This demonstrates an easy workaround by wrapping the fetching of constraints in a block:
class ConstraintWrapper
# Helps to load constraints from inside controllers without breaking
# development environment reloading
@schnittchen
schnittchen / spec_helper_snippet.rb
Last active December 15, 2015 00:28
Make view specs fail when image_tag is used for a non-existing asset
config.before(:each) do
if self.class.metadata[:type] == :view
view.singleton_class.send :alias_method, :original_image_tag, :image_tag
view.singleton_class.send :define_method, :image_tag do |path, options = {}|
normalized_path = path_to_image(path.to_s)
asset_paths = Sprockets::Helpers::RailsHelper::AssetPaths.new(nil) # hack: easy access to is_uri?
unless asset_paths.is_uri?(normalized_path)
normalized_path = normalized_path[asset_prefix.length+1..-1]
unless Rails.application.assets.find_asset(normalized_path) or path.respond_to?(:stubbed?)
@schnittchen
schnittchen / groovy_bostbuild_script.groovy
Created July 22, 2014 22:02
Hacked jenkins integration for slack
// Install the "Groovy Postbuild" plugin and use this as postbuild script:
def message = "${manager.build.result}" + ': ' + manager.build.getDisplayName() + ' ' + manager.build.getAbsoluteUrl()
def command = ['sh', '-c', 'echo "' + message + '" | curl --data-binary @- "https://YOURSLACKDOMAIN/services/hooks/slackbot?token=YOUR_SLACKBOT_INTEFGRATION_TOKEN&channel=URL_ENCODED_CHANNEL_NAME"' ]
command.execute()
@schnittchen
schnittchen / gist:a47e40760e804a5cc8b9
Created October 8, 2014 20:22
"Tunneling" an ssh agent connection through into an lxc container (for bootstrapping)
while :; do socat UNIX:$SSH_AUTH_SOCK EXEC:"lxc-attach -n $CONTAINER_NAME -- /usr/bin/socat STDIN UNIX-LISTEN\:/agent-sock"; done
# After entering the container the usual way using lxc-attach (from a separate host shell), set the SSH_AUTH_SOCK environment variable to "/agent-sock".
# Needs socat both on the host and in the container.
@schnittchen
schnittchen / gist:2b765d2c03063e8c49ad
Created January 13, 2015 11:22
Rust beginners resources
Intended at programmers, not absolute beginners.
Consume these resources in this order to get the hang of Rust:
1) for motivation and the basic resource management notions https://d3fenhwk93s16g.cloudfront.net/0l7u7x/mp4.mp4?t=142109848354b43df3bf31f
2) for a tour of the language features and to get a grip on the syntax http://rustbyexample.com/
3) go more into depth http://doc.rust-lang.org/book/
Work in progress. Suggestions welcome!
# jsxlint does not consider files not ending in .jsx as containing JSX syntax.
# With react-rails, a .jsx file is not subject to 6to5/babel transformation,
# hence I cannot use JSX syntax and ES6 at the same time.
# This little hack (and a little convention) does the trick for me:
#
# The sprockets-es6 gem provides both transformations I want for the .es6 ending.
# I name my files ending in .es6.jsx (which happens to be descriptive as well)
# and make the .jsx transformation step a NOP (by overwriting the engine
# which has been registered for .jsx before by react-rails).
@schnittchen
schnittchen / German-Zip-Codes.csv
Last active August 29, 2015 14:25 — forked from jbspeakr/German-Zip-Codes.csv
list of German zip codes (Postleitzahl), including dialing code, name of the city and federal state.
We can't make this file beautiful and searchable because it's too large.
Ort;Zusatz;Plz;Vorwahl;Bundesland
Aach;b Trier;54298;0651;Rheinland-Pfalz
Aach;, Hegau;78267;07774;Baden-Württemberg
Aachen;;52062;0241;Nordrhein-Westfalen
Aachen;;52064;0241;Nordrhein-Westfalen
Aachen;;52066;0241;Nordrhein-Westfalen
Aachen;;52068;0241;Nordrhein-Westfalen
Aachen;;52070;0241;Nordrhein-Westfalen
Aachen;;52072;0241;Nordrhein-Westfalen
Aachen;;52074;0241;Nordrhein-Westfalen