Skip to content

Instantly share code, notes, and snippets.

View davydovanton's full-sized avatar
:shipit:
Working from home

Anton Davydov davydovanton

:shipit:
Working from home
View GitHub Profile
@davydovanton
davydovanton / in_two_columns.rb
Created November 1, 2013 18:11
array in two columns
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:
@davydovanton
davydovanton / link_to_custom.rb
Created November 15, 2013 19:03
link_to_custom
@davydovanton
davydovanton / active_record_all_table_and_column.rb
Last active December 28, 2015 12:59
List of all table and column in ActiveRecord
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
http://www.facebook.com/sharer.php?u={url}&t={title}
http://www.liveinternet.ru/journal_post.php?action=n_add&cnurl={url}&cntitle={title}
http://www.livejournal.com/update.bml?event={url}&subject={title}
http://connect.mail.ru/share?url={url}&title={title}
http://www.odnoklassniki.ru/dk?st.cmd=addShare&st._surl={url}&title={title}
http://twitter.com/share?text={title}&url={url}
http://vkontakte.ru/share.php?url={url}
http://www.google.com/buzz/post?message={title}&url={url}
http://del.icio.us/post?url={url}&title={title}
http://digg.com/submit?url={url}&title={title}&media=news&topic=people&thumbnails=0
@davydovanton
davydovanton / Ruby tips
Last active August 29, 2015 14:01
Ruby tips
# 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 }
@davydovanton
davydovanton / gist:dfd90a973e0a73347404
Created July 8, 2014 13:54
Convert pathogen plugins to vundle
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
@davydovanton
davydovanton / gist:948934dd69856ae1c766
Last active April 12, 2021 09:25
Ruby string interpolation benchmark
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('_') } }
@davydovanton
davydovanton / gist:5aa215915488069614fc
Last active September 30, 2015 02:28
Simple jekyll plugin for create a html figure construction.
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 :)
" 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)
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/' do
erb :index
end
get '/follower_viz' do