Skip to content

Instantly share code, notes, and snippets.

View ang3lkar's full-sized avatar

Angelos Karagkiozidis ang3lkar

View GitHub Profile
@ang3lkar
ang3lkar / active_model_serializers.rb
Created July 9, 2018 09:06 — forked from pdabrowski6/active_model_serializers.rb
BENCHMARKS: How to create fast JSON serialization in Rails?
# post_serializer.rb
class PostSerializer < ActiveModel::Serializer
has_many :comments
attributes :id, :title, :content
end
# comment_serializer.rb
@ang3lkar
ang3lkar / sub.sh
Last active August 10, 2017 12:14
Rename subtitles according to their video file counterparts
#!/bin/bash
# Renames subtitles files according to tv shows names found in a directory
# Acceped syntaxes for season/episode are: 304, s3e04, s03e04, 3x04, Ep-04 (case insensitive)
#
# Installation:
# Put this gist somewhere in your $PATH, like /usr/local/bin/sub.sh
# Chmod +x it
# cd ~/YourTvShowsFolderContainingVideoAndSubtitleFiles
# sub
@ang3lkar
ang3lkar / post-checkout
Last active May 23, 2017 09:28
Adds missing variables from .env_sample to .env when switching branches
#!/usr/bin/env ruby
begin
env = File.read(File.expand_path('~/projects/workable/.env'))
env_sample = File.read(File.expand_path('~/projects/workable/.env_sample'))
env_keys = env
.split("\n")
.reject { |e| e.start_with?('#') }
.map { |e| e.split('=')[0] }
export YELLOW="\033[0;33m"
export NC="\033[0m" # No Color
# Until I figure out how to set the branch via argument, I have to edit this line
export SOURCE_DIR=~/projects/workable
export TARGET_DIR=~/projects/docker/workable
export BRANCH=research/docker
export DOCKER_TAG=latest
@ang3lkar
ang3lkar / pairs.rb
Created February 28, 2017 15:03
Pair with sum problem
# Find a pair of elements from an array whose sum equals a given number
#
# * The array contains only integers
# * The array can have duplicates
# * Return a boolean that indicates if the list has such a pair
# * We will start with an ordered (ascending order) list example
require 'benchmark/ips'
require 'set'
@ang3lkar
ang3lkar / benchmark.rake
Created December 19, 2016 10:42
Benchmark rendering of Rails view in rake file
namespace :benchmarks do
desc 'With cache'
task cache: :environment do
class RakeActionView < ActionView::Base
include Rails.application.routes.url_helpers
include ::ApplicationHelper
def default_url_options
{ host: 'localhost:3000' }
end
@ang3lkar
ang3lkar / explain_analyze.rb
Created August 29, 2016 16:13
Run EXPLAIN ANALYZE on all select queries and log the results. Only for development!
if Rails.env.development?
require 'active_record/connection_adapters/postgresql_adapter'
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def __explain_analyze(sql, command, *args)
meth = "#{command}_without_explain_analyze".to_sym
if /\A\s*SELECT/i.match(sql)
newsql = "EXPLAIN ANALYZE #{sql}"
plan = send(meth, newsql, *args).map { |row| row['QUERY PLAN'] }.join("\n")
Rails.logger.debug("\e[1m\e[31mQUERY PLAN FOR: #{sql.strip};\n#{plan}\e[0m")
@ang3lkar
ang3lkar / form.js
Created August 23, 2016 10:01
JQuery form plugin
$(function ($) {
$.extend({
form: function (url, data, method) {
if (method == null) method = 'POST';
if (data == null) data = {};
var form = $('<form>').attr({
method: method,
action: url,
}).css({
@ang3lkar
ang3lkar / subdomain_enforcer.rb
Created July 15, 2016 09:00
Rails middleware to enforce subdomain requests
# app/middleware/subdomain_enforcer.rb
class SubdomainEnforcer
def initialize(app)
@app = app
end
def call(env)
@request = Rack::Request.new(env)
if @request.params['sbd'].present?
@ang3lkar
ang3lkar / disk_jokey.rb
Created June 29, 2016 10:37
Failed Delayed::Job runner
djs.each do |dj|
begin
dj.invoke_job
# h = YAML.load(dj.handler)
# puts "Processed dj #{dj.id}"
dj.destroy
''
rescue => e
# ha = YAML.load(dj.handler)
puts "dj_id: #{dj.id}"