Skip to content

Instantly share code, notes, and snippets.

View msievers's full-sized avatar

Michael Sievers msievers

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / ungroup_grouped_objects_in_fabricjs.js
Last active October 14, 2022 12:46
Ungroup programatically grouped objects in fabric.js (simply past this code into the "execute" tab of fabricjs kitchensink demo)
// clear canvas
canvas.clear();
// add red rectangl
canvas.add(new fabric.Rect({
width: 50, height: 50, left: 50, top: 50, fill: 'rgb(255,0,0)'
}));
canvas.add(new fabric.Rect({
width: 50, height: 50, left: 110, top: 50, fill: 'rgb(255,0,0)'
@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 = ""