Skip to content

Instantly share code, notes, and snippets.

View ang3lkar's full-sized avatar

Angelos Karagkiozidis ang3lkar

View GitHub Profile
@ang3lkar
ang3lkar / README.md
Created March 4, 2016 14:25 — forked from derwiki/README.md
Ruby module that you can use in a `before_action` on sensitive controllers for which you'd like a usage audit trail

Adding an audit log to your Rails app

If you have any sort of administrative interface on your web site, you can easily imagine an intruder gaining access and mucking about. How do you know the extent of the damage? Adding an audit log to your app is one quick solution. An audit log should record a few things:

  • controller entry points with parameter values
  • permanent information about the user, like user_id
  • transient information about the user, like IP and user_agent

Using the Rails framework, this is as simple as adding a before_action to your admin controllers. Here’s a basic version that I’m using in production.

@ang3lkar
ang3lkar / backend_test.rb
Created June 27, 2016 14:59
Rails performance test setup
require 'test_helper'
require 'rails/performance_test_help'
require_relative 'support/backend_test_helper'
class BackendTest < ActionDispatch::PerformanceTest
include BackendTestHelper
attr_reader :eddard, :jaime
setup do
@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}"
@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 / 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 / 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 / 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 / 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'
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 / 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] }