Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View msievers's full-sized avatar

Michael Sievers msievers

View GitHub Profile
@msievers
msievers / de.activemodel.errors.yml
Last active August 29, 2015 14:06
Seamlessly integrated custom rails validator
de:
activemodel:
errors:
messages:
distinctness: "müssen unterschiedlich sein"
@msievers
msievers / i18n_translate_with_references.rb
Last active August 29, 2015 14:07
Monkey patch I18n.translate so it recognizes references encoded as ~> some.other.key
#
# config/initializers/i18n_translate_with_references.rb
#
module I18n
class << self
alias_method :original_translate, :translate
def translate(*args)
regexp = /\A~>(.*)\Z/ # e.g. ~> some.other.key
@msievers
msievers / prepend_to_singleton_class_via_inherited.rb
Created October 10, 2014 15:08
Prepend module code to subclasses by prepending to it's instances singleton_class via inherited
module InheritedHandler
def inherited(subclass)
subclass.instance_eval do
alias :original_new :new
def self.inherited(subsubclass)
subsubclass.extend(InheritedHandler)
end
def self.new(*args, &block)
@msievers
msievers / application_helper.rb
Created November 20, 2014 11:11
Render templates only if they exist (with fallback option)
module ApplicationHelper
# https://coderwall.com/p/ftbmsa (Render template if exists in Rails)
def try_to_render(partial_path, options = {})
fallback = options.delete(:fallback_to)
prefixes = partial_path.split("/")[0..-2].presence || controller._prefixes
partial_name = partial_path.split("/").last
if lookup_context.exists?(partial_name, prefixes, true) # "true" is crucial !
render partial_path, options
else
@msievers
msievers / ApplicationHelper.rb
Last active August 29, 2015 14:18
An attempt for a render_relative for rails
# e.g. inside app/views/records/show.html.erb use
#
# <%= render_relative "./record/title" %>
#
# to render app/views/records/show/record/title
module App::ApplicationHelper
def render_relative(relative_partial_path, locals = {}, &block)
calling_file_directory = @virtual_path.split("/")[0..-2].join("/")
partial_path = File.join(calling_file_directory, relative_partial_path)
@msievers
msievers / example.gemspec
Created September 27, 2012 08:19
Gemspec which includes files from git submodules into resulting gem
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/example/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["John Doe"]
gem.email = ["john_doe@example.org"]
gem.description = %q{Write a gem description}
gem.summary = %q{Write a gem summary}
gem.homepage = ""
@msievers
msievers / amd_sample.js.coffee
Last active December 20, 2015 11:08
Sample with AMD in place
# assets/javascript/Component.js.coffee
define ->
class
methodEverybodyShouldHave: ->
# ...
# assets/javascript/SeatPlan/InputDataSanitizer.js.coffee
define ->
class
sanitize: (data) ->
@msievers
msievers / [ActiveRecord, SQL] Multi-model sql-only search with UNION
Last active October 25, 2018 05:54
A simple, sql only, multi model search
The idea is, to have models adhere to a convention, which stats, that there has to be a field "searchable_tokens",
which includes searchable tokens. Now, we can simply have a (virtual) model/table `search_results`, which can be
unioned with "casted selects" of each model we want to search. The casted selects make the columns of each model to fit
into the schema of the search result.
Mixed with ActiveRecord's polymorphic associations, the result is an ActiveRecord relation with elements, which points to
their respective subject.
@msievers
msievers / [Docker] overlay-docker-systemd.sh
Last active October 25, 2018 05:57 — forked from cpswan/overlay-docker-systemd.sh
Configure systemd to use overlay file system for Docker
sudo mkdir /etc/systemd/system/docker.service.d
sudo bash -c 'cat <<EOF > /etc/systemd/system/docker.service.d/overlay.conf
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --storage-driver=overlay
EOF'
sudo systemctl daemon-reload
sudo systemctl restart docker
@msievers
msievers / [Ruby] mri.rb
Last active October 25, 2018 05:58
Example for hijacking ruby via FFI and using native structs and functions
require "ffi"
module MRI
extend FFI::Library
ffi_lib [FFI::CURRENT_PROCESS, "ruby"]
attach_function :rb_str_resize, :rb_str_resize, [:pointer, :long], :pointer
def self.sizeof(type)
Class.new(FFI::Struct) do