This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def in_two_columns(objects) | |
items_in_column = (olbjects.size / 2.0).ceil | |
objects.in_groups_of(items_in_column, false) do |obj| | |
yield obj if block_given? | |
end | |
end | |
# method inputs array and block | |
# | |
# example: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def link_to_custom(name, options = {link_name: name}) | |
page = cms_site.pages.find_by_label(name) | |
page = page.nil? ? "#" : page.full_path | |
link_options = options.select {|key, value| key != :link_name && key != :link_page } | |
link_to( page , link_options ) do | |
block_given? ? yield : options[:link_name] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ActiveRecord::Base.connection.tables.each do |table_name| | |
puts "\n" + table_name | |
ActiveRecord::Base.connection.columns(table_name).each {|c| puts "- " + c.name + ": " + c.type.to_s + " " + c.limit.to_s} | |
end | |
User.reflect_on_all_associations.each { |a| puts "#{a.macro} => #{a.name}" } | |
# has_many => posts | |
# has_one => avatar | |
# has_one => posts_filter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check type class in STI in rails | |
Post.uniq.pluck(:type).each do |class_type| | |
method_name = class_type.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + '?' | |
define_method method_name do | |
type == class_type | |
end | |
end | |
# Use mb_chars.uppcase for many varibles | |
first_name, middle_name, last_name = [:first_name, :middle_name, :last_name].map{ |i| user.send(i).mb_chars.capitalize } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in */.; do | |
cd $i | |
repo=$(git remote show origin | grep 'Fetch URL:') | |
re="Fetch URL: https://github.com/(.+)" | |
if [[ $repo =~ $re ]]; then | |
var="$var\nPlugin '${BASH_REMATCH[1]}'" | |
fi | |
cd - | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
first = 'first' | |
second = 'second' | |
n = 1_000_000 | |
Benchmark.bm do |x| | |
x.report('string interpolation: '){ n.times { "#{first}_#{second}" } } | |
x.report('string interpolation #2: '){ n.times { first + '_' + second } } | |
x.report('string interpolation #3: '){ n.times { "%s_%s" % [first, second] } } | |
x.report('join array: '){ n.times { [first, second].join('_') } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jekyll | |
module Tags | |
# Simple jekyll plugin for create a html figure construction. | |
# About figure: | |
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure | |
# | |
# The figure tag should take this form: | |
# | |
# {% image_with_caption you_image_url %} | |
# This is caption block. This should be parsed as *markdown* [man](http://example.com/). Or not :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Tools for searching and replacing text in Vim | |
" Search for the current visual selection | |
fun! StarSearch(direction) | |
let old_reg = getreg('"') | |
let old_regtype = getregtype('"') | |
exe 'normal! gvy\<esc>gV' | |
let @/ = EscapeMagic(@") | |
call histadd("/", @/) | |
call setreg('"', old_reg, old_regtype) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'sinatra' | |
get '/' do | |
erb :index | |
end | |
get '/follower_viz' do |
OlderNewer