Skip to content

Instantly share code, notes, and snippets.

@olegantonyan
olegantonyan / application.rb
Last active August 29, 2015 14:25
Monkey patch to_partial_path to fix namespaced models (https://github.com/rails/rails/issues/21004)
module ActiveModel
module Conversion
def to_partial_path
original_path = self.class._to_partial_path
parts = original_path.split('/')
return original_path if parts.length == 2
pluralized_parts = parts[0..-3].map { |p| p.pluralize }
"#{pluralized_parts.join('/')}/#{parts.last(2).join('/')}".freeze
end
end
@olegantonyan
olegantonyan / crud_responder.rb
Last active October 4, 2015 10:21
CrudResponder - DRYing controller actions. Now in gem https://github.com/olegantonyan/crud_responder
# lib/crud_responder.rb
module CrudResponder
include ActionView::Helpers::TextHelper
protected
def crud_respond(object, options = {})
method = options.fetch(:method, method_by_caller(caller))
success_url = options.fetch(:success_url) { default_redirect_url(method, object) }
error_action = options.fetch(:error_action) { default_render_action(method, object) }
@olegantonyan
olegantonyan / test_mixin.rb
Last active October 26, 2015 03:22
Mixin class methods
module Foo
def self.included base
base.send :extend, ClassMethods
end
module ClassMethods # the name doesn't matter as long as it match with line 3
def bar
puts 'hello'
end
end
[GEM_ROOT]/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:263 :in `const_get`
261 constant.const_get(name)
262 else
263 candidate = constant.const_get(name)
264 next candidate if constant.const_defined?(name, false)
265 next candidate unless Object.const_defined?(name)
[GEM_ROOT]/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:263 :in `block in constantize`
[GEM_ROOT]/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:259 :in `each`
@olegantonyan
olegantonyan / atom_update_check.sh
Created May 2, 2016 09:07
Linux RPM check Atom update
#!/bin/sh
NOTIFY_USER=oleg # optional, for kdialog
USER_DISPLAY=:0 # optional, for kdialog
LOCATION=`curl -I https://github.com/atom/atom/releases/latest | perl -n -e '/^Location: (.*)$/ && print "$1"'`
VERSION=`basename $LOCATION | perl -n -e '/(\d+.\d+.\d+)/ && print "$1"'`
echo "available version: $VERSION"
INSTALLED=`rpm -q atom | perl -n -e '/atom-(.*?)-/ && print "$1"'`
@olegantonyan
olegantonyan / gist:be8ec1b32c220ad35e79
Created December 22, 2015 06:15
reicast crash on start
Config dir is: /home/oleg/.config/reicast/
Data dir is: /home/oleg/.local/share/reicast/
Personality: 00000000
Updated personality: 00000000
Linux paging: 00001000 00001000 00000FFF
MAP 00800000 w/ 25165824
MAP 20000000 w/ 25165824
MAP 04000000 w/ 16777216
MAP 06000000 w/ 16777216
MAP 0C000000 w/ 0
@olegantonyan
olegantonyan / pulse_simple_play.py
Created November 10, 2017 04:41 — forked from ignacysokolowski/pulse_simple_play.py
Python: Play a WAV file with PulseAudio simple API
#!/usr/bin/env python
import ctypes
import wave
import sys
pa = ctypes.cdll.LoadLibrary('libpulse-simple.so.0')
PA_STREAM_PLAYBACK = 1
PA_SAMPLE_S16LE = 3
@olegantonyan
olegantonyan / gist:735848cb0f188410ca3863de718e7c53
Created August 13, 2018 07:16
ruby monotonic timer benchmark
def benchmark(what)
start = ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield
finish = ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
sap "#{what} took #{finish - start} seconds"
end
@olegantonyan
olegantonyan / userContent.css
Created March 10, 2017 10:30
Firefox dark theme fix inputs
/* ~/.mozilla/firefox/XXXXXX.default/chrome/userContent.css */
input {
border: 2px inset white;
background-color: white;
color: black;
-moz-appearance: none !important;
}
textarea {
border: 2px inset white;
def benchmark(what)
start = ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield
finish = ::Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "#{what} took #{finish - start} seconds"
end