Skip to content

Instantly share code, notes, and snippets.

View bsa7's full-sized avatar

Belevskij Sergeij bsa7

View GitHub Profile
@bsa7
bsa7 / deploy
Created August 26, 2016 05:56
deploy with puma, ssh and bash
#!/bin/bash
user_name='your_name'
app_name="your_app_name"
deploy_to_port="22"
git push -f $app_name $1
app_path="/home/$user_name/projects"
deploy_to_ip=`cat $app_path/$app_name/tmp/ip`
deploy_app_path="/home/$user_name/projects"
deploy_path="$deploy_app_path/$app_name"
current_path="$deploy_path/current"
@bsa7
bsa7 / dbinfo.rake
Last active August 24, 2016 05:21
ActiveRecord database info
namespace :db do
task info: [:environment] do
tables_reflections = {}
Rails.application.eager_load!
models_created_by_user = ActiveRecord::Base.descendants
models_created_by_user.sort_by { |model| model.table_name || model.name }.each do |model|
begin
tables_reflections[model.table_name] = {}
model.reflections.keys.each do |reflection_key|
reflection = model.reflections[reflection_key]
@bsa7
bsa7 / working.conf
Last active May 3, 2016 05:06
Rails puma nginx ssl
upstream site_name {
server unix:///home/me/projects/site_name/shared/tmp/sockets/site_name.sock;
}
# for redirecting to https version of the site
server {
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
@bsa7
bsa7 / code_marker
Last active July 6, 2016 18:57
code marker
#!/usr/bin/env ruby
require 'digest'
require 'colorize'
def marker_templates
[
{param_key: 'rb', extension_regexp: '\.rb', marker: -> (digest){digest}},
{param_key: 'haml', extension_regexp: '\.html\.haml', marker: -> (digest){"#hook-#{digest}"}},
{param_key: 'erb', extension_regexp: '\.html\.erb', marker: -> (digest){"<div id=\"hook-#{digest}\"></div>"}},
{param_key: 'coffee', extension_regexp: '\.js\.coffee', marker: -> (digest){"i_#{digest} = null"}},
@bsa7
bsa7 / checker.rb
Last active March 18, 2016 17:50
check rails project file extensions
#!/usr/bin/env ruby
extensions = %w[haml erb coffee scss]
check_regexps = %w[html.haml$ html.erb$ js.coffee$ css.scss$].map{|r|Regexp.new(r)}
files = Dir.glob("**/*")
files.each do |filename|
extension = filename[/(?<=\.)[a-z]+$/]
if extensions.include? extension
extension_index = extensions.index extension
check_regexp = check_regexps[extension_index]
unless filename[check_regexp]
@bsa7
bsa7 / block_and_yield.rb
Created March 15, 2016 19:32
ruby own blocks
def cached_entity(key, &block)
cache = Rails.cache
time_lap = 11.seconds # cache expires each time_lap
cache.fetch(key, expires_in: time_lap) do
yield
end
end
def cached_nomenclature_ids()
cached_entity('nomenclature_ids') do
@bsa7
bsa7 / mysqld.cnf.markdown
Last active March 8, 2016 21:36
reduce mysql memory usage

in /etc/mysql/mysql.conf.d/mysqld.cnf change these values:

    key_buffer              = 8M
    max_connections         = 30
    query_cache_size        = 8M
    query_cache_limit       = 512K
    thread_stack            = 128K
@bsa7
bsa7 / example.yml
Last active March 7, 2016 17:18
recursive remove unneeded single and double quotes from rails localization *.yml files
# for testing - place its file inside config/locales in fake rails folder and run ruby replacer.rb from root of this.
en:
test_yaml:
test_1: ':'
test_2: '{'
test_3: '}'
test_4: '['
test_5: ']'
test_6: ','
test_7: '&'
@bsa7
bsa7 / grep.markdown
Created March 7, 2016 11:21
grep regexp - perl notation

newest 'perl' notation in regexp instead POSIX style is a better, but grep by default is use a POSIX. Use key -P:

    grep -r -P ':.+?\%\{.+?\}.+?$' config/locales/.

nhis find all strings in Rails localization files where %{...} construction finded

@bsa7
bsa7 / find_and_sed.markdown
Last active March 7, 2016 11:15
recursive find occuriences in files and replace it by regex with sed in bash
  1. Replace text in one file:
    sed 's/regular_expression_or_string_replace_from/string_replace_to/g' source.sql > target.sql
  1. find all files in current folder and replace text in each:
    ls | xargs -I@ sed -i 's/regular_expression_or_string_replace_from/string_replace_to/g' @