Skip to content

Instantly share code, notes, and snippets.

@duhast
duhast / take-coffee.sh
Created April 14, 2014 23:19
Migrate Rails project from pure JavaScript to CoffeeScript
#!/bin/bash
# install js2coffee first with `npm install js2coffee`
find ./app/assets/javascripts -iname "*.js" | \
while read I;
do
`js2coffee ${I} > ${I}.coffee`
echo "$I has been coffefied"
`rm ${I}`
done
@duhast
duhast / application_controller.rb
Last active September 29, 2015 19:16
Flushing Rails Flash messages to HTTP headers for XHR/AJAX requests; Generic AJAX result notifications
class ApplicationController < ActionController::Base
#...
after_filter :flash_to_headers
#...
protected
def flash_to_headers
@duhast
duhast / pgsql_import_all.sh
Created June 10, 2014 20:28
(re)Create PostgreSQL databases from a bunch of .sql.gz dumps
#!/bin/bash
# (re)Create PostgreSQL databases from a bunch of .sql.gz dumps
DBHOST=localhost
DBUSER=root
for f in *.sql.gz
do
echo "Unpacking $f ..."
@duhast
duhast / init_loader.js.coffee
Created September 24, 2014 15:43
AJAX request indicator (CoffeeScript, BackboneJS, jQuery)
$(document).ready ->
myApp.loader = new myApp.views.LoadingIndicator()
$(document).ajaxSend -> myApp.loader.start()
$(document).ajaxComplete -> myApp.loader.finish()
$(document).ajaxError -> myApp.loader.finish()
@duhast
duhast / re_email_subject.coffee
Created July 27, 2015 14:23
CoffeeScript / JavaScript function to compose a subject of an email reply
makeReplySubject = (sourceSubject) ->
match = sourceSubject.match(/^Re\s*(\[\s*(\d)+\s*\])?:\s*(.+)$/i)
return "Re: #{sourceSubject}" unless match?
if match[2]?
replyNumber = parseInt(match[2])
"Re [#{replyNumber + 1}]: #{match[3]}"
else
"Re [2]: #{match[3]}"
@duhast
duhast / olxphotos2bbcode.js
Last active December 17, 2018 15:13
Фото объявления на OLX в BBCode
// Выполнить строку в консоли инструментов разработчика
//В BBCode
copy($('#bigGallery a').map(function(i){ return '[url='+$(this).attr('href')+'][img]'+$(this).attr('rel')+'[/img][/url]'; }).get().join("\n"));alert('Photos BBCode copied to clipboard');
// Список фоток для aria2 input file
copy($('#bigGallery a').map(function(i){ return $(this).attr('href')+"\n out="+i+'.jpg'; }).get().join("\n"));
@duhast
duhast / imagemagick_font_tool.rb
Created January 6, 2017 15:50
ImageMagick helper to import custom fonts from Ruby
# ImageMagick helper to import custom fonts from Ruby instead of imagick_type_gen python script
# Designed for OSX/Heroku
# Usage:
# desc 'Init custom fonts'
# task fonts: :environment do
# ImagemagickFontTool.export_type_xml(nil, '/app') # For Heroku
# if Rails.env.development?
# home_type_config = File.expand_path '~/.magick/myapp-type.xml' # Need to register this manually in ~/.magick/type.xml
# ImagemagickFontTool.export_type_xml home_type_config
@duhast
duhast / grid-totals-row.component.ts
Last active January 22, 2018 02:19
[Angular 2] Attaching a component inside other component dynamically - total row in a table
import * as _ from 'lodash';
import { Component, DoCheck, Input, IterableDiffer, IterableDiffers, OnInit } from '@angular/core';
import { Trend } from '../../../models/trend-trade/trend-trade';
@Component({
template: `
<tr>
<td></td>
<td colspan="2">Total:</td>
<td>{{ totalValue }}</td>
@duhast
duhast / routes.rb
Created April 18, 2019 19:19
Rails route lambda
MyRailsApp::Application.routes.draw do
get :have_fun, :to => proc { |env|
result = {some: 'data'}
res = result.to_json
[200, {"Content-Type" => 'application/json'}, [res]]
}
end
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac