Skip to content

Instantly share code, notes, and snippets.

module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))
@estum
estum / user.rb
Last active December 21, 2015 05:49
Migrate passwords from legacy systems to Devise
# updated variant of older solution:
# http://www.davidverhasselt.com/2012/05/13/how-to-migrate-passwords-from-legacy-systems-to-devise
class User < ActiveRecord::Base
# ...
def valid_password?(password)
if legacy_password?
# Use Devise's secure_compare to avoid timing attacks
@estum
estum / dsl.rb
Last active December 21, 2015 14:59
Bundler::DSL extension to provide OS-depended gems in Gemfile
# lib/bundler/dsl.rb
# Bundler::DSL extension to provide OS-depended gems in Gemfile
# https://github.com/bundler/bundler/wiki/Platform-as-a-parameter
#
# Allows to group os-dependent gems in blocks with one or more symbols, strings or regexps as parameters.
# OS name is obtained from `RbConfig::CONFIG['host_os']` or `RUBY_PLATFORM` if the first one is not available.
#
# Place this file in the project's lib folder and require it in your Gemfile:
# require File.expand_path('../lib/bundler/dsl', __FILE__)
@estum
estum / passenger.rb
Created September 5, 2013 04:38
Capistrano task which generates nginx passenger config
# -- config/passenger.rb
# require this file in the config/deploy.rb
Capistrano::Configuration.instance.load do
# Config template path (config/passenger/template.nginx.conf)
set :template_path, File.expand_path('../passenger/template.nginx.conf', __FILE__)
# Upload config to:
set :config_deploy_path, "#{deploy_to}/config/nginx_passenger.conf"
@estum
estum / .pryrc.rb
Last active December 27, 2015 20:39
Custom "reload!" command for pry, which just restarts rails. Works with non-rails projects.
# .pryrc
Pry::Commands.create_command "reload!" do
description "Exits and runs again pry process"
def process
target.eval <<-EVAL
at_exit { puts "Reloading pry..."; exec "pry" }
exit
EVAL
# config/initializers/unauthorized_cross-origin_response_fix.rb
#
# Fixes unauthorized cross-origin response problem for .js templates (problem description in Russian http://habrahabr.ru/post/209618/)
# in Ruby On Rails 3.2, idea and code extracted from https://github.com/rails/rails/blob/4f4fdd643f9d19fbbeeec3ac77674f791c9beffa/actionpack/lib/action_controller/metal/request_forgery_protection.rb
#
# for Rails 4 just save https://raw2.github.com/rails/rails/4f4fdd643f9d19fbbeeec3ac77674f791c9beffa/actionpack/lib/action_controller/metal/request_forgery_protection.rb
# to config/initializers/unauthorized_cross-origin_response_fix.rb
#
# for Rails 4.1 there is https://github.com/rails/rails/pull/13345)
@estum
estum / table-responsive.coffee
Last active August 29, 2015 13:56
Improved responsive tables for Bootstrap 2
# ==============================================
# = Improved responsive tables for Bootstrap 2 =
# ==============================================
#
# Add .table-responsive to enable this feature for tables.
# Change $.fn.tableResponsive.defaults.media_query to support other devices.
class TableResponsive
provide_uniq_id = (table) ->
table.id ?= "table-responsive-#{ just.genuniq() }"
@estum
estum / tabbable.rb
Last active August 29, 2015 13:56
Bootstrap 2 tabs helper method for Ruby on Rails
# helpers/bootstrap_helper/tabbable.rb
# Bootstrap 2 tabs helper method for Ruby on Rails
# http://getbootstrap.com/2.3.2/components.html
#
# == Usage (slim):
#
# = tabbable do |t|
# = t.section :tab_1 do
# / Content for Tab #1
@estum
estum / switch_on.rb
Last active August 8, 2023 05:52
Ruby alternative switch-case syntax.
# = Kernel#switch
# Provides alternative switch-case syntax.
#
# # support methods:
# value = []
# switch value do
# on empty?: -> { "none" }
# on one?: -> { "one" }
# on many?: -> { "many" }
# end
@estum
estum / Open gem homepage.tmCommand
Last active August 29, 2015 14:06
Textmate commands to open gems from Gemfiles and gemspecs.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby -wU
unless ENV['TM_FILENAME'] =~ /^(?:Gemfile|.*\.gemspec)$/