Skip to content

Instantly share code, notes, and snippets.

# 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 / 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)$/
@estum
estum / splat_struct.rb
Created December 3, 2014 20:23
Ruby's Struct subclass with splats support
#= SplatStruct
# `Struct` subclass with splats support
#
# SampleClass = SplatStruct.new(:a, :b, [:c]) do |klass|
# def avgc
# Rational(c.inject(:+), c.length).to_f
# end
# end
#
# object = SampleClass.new(1,2,3,4,5)
@estum
estum / pryew.sh
Created December 18, 2014 23:50
Pry Everywhere: shell command to run pry in the current context
#!/usr/bin/env bash
VERBOSE="false"
while getopts "v" flag; do
case "${flag}" in
v) VERBOSE="true" ;;
esac
done
readonly VERBOSE
@estum
estum / plutil.rb
Created January 29, 2015 16:10
Ruby "plutil" command class wrapper
require 'json'
class Plutil
module JSON
def self.load(plist)
Plutil.convert plist, to: :json do |converted_io|
::JSON.load(converted_io)
end
end
@estum
estum / has_many_limit_per_likes.rb
Last active August 29, 2015 14:15
Eager load has many polymorphic association with limit per each owner
# Standart behavior: if you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects
# This example shows how to eager load limited number of records in collection.
# Usage:
# Post.find(42).likes.size # => 300
# Post.find(42).likes.limit_per_target(5).size # => 5
# Post.preload(:likes).map {|t| t.likes.size } # => [100, 200, 300, ... ]
# Post.preload(:limited_likes).map {|t| t.likes.size } # => [5, 5, 5, ... ]
#
# recommended: postgres ~>9.4, ruby ~>2.2, rails ~>4.2, squeel
@estum
estum / named_captures.rb
Created February 23, 2015 20:43
MatchData#named_captures: a hash of names and matches of captures
require "active_support/core_ext/hash/transform_values"
class MatchData
# Returns a hash containing the names and matches of captures or nil.
#
# A key of the hash is a name of the named captures. A value of the hash is a
# matched string or an array of corresponding matches.
#
# puts /(?<foo>.)(?<bar>.)(?<baz>.)(?<baz>.)/.match("hoge", &:named_captures).inspect
# # => {"foo"=>"h", "bar"=>"o", "baz"=>["g", "e"]}
@estum
estum / awesome_print_proc.rb
Created April 13, 2015 00:33
AwesomePrint::Proc: inspecting Proc instances with source code (using Sourcify)
# = Awesome Print Proc Formatter
#
# Prints source when inspecting Proc object.
# Requires <tt>sourcify (>= 0.6.0.rc4)</tt> gem.
begin
require "sourcify"
rescue LoadError
end