Skip to content

Instantly share code, notes, and snippets.

@derwiki
derwiki / parser.rb
Created January 4, 2017 02:38
Using `parser` to look for `Resque.enqueue` calls inside a `ActiveRecord::Base.transaction` block.
require 'parser/current'
class Processor < AST::Processor
attr_accessor :verbose
def initialize(*args)
super
self.verbose = false
end
@derwiki
derwiki / sessions.diff
Last active December 2, 2016 06:56
Allowing users to see/sign out other sessions in Rails
diff --git a/app/controllers/admin/profile_controller.rb b/app/controllers/admin/profile_controller.rb
index 29a7f9a..08a5ea9 100644
--- a/app/controllers/admin/profile_controller.rb
+++ b/app/controllers/admin/profile_controller.rb
@@ -24,6 +24,7 @@ class Admin::ProfileController < Admin::BaseController
def update_password
@user = current_user
if @user.update_with_password(params[:user])
+ other_sessions.destroy_all
flash.now[:success] = 'Password updated!'
@derwiki
derwiki / review-apps-postdeploy.sh
Created May 13, 2016 23:10
`postdeploy` script for Heroku Review Apps that replaces the default Postgres DB with a MySQL DB from ClearDB.
#!/app/bin/ruby
require 'platform-api'
cleardb_url = ENV['CLEARDB_DATABASE_URL']
if cleardb_url.nil? || cleardb_url == ''
puts "Error: CLEARDB_DATABASE_URL not set"
exit -1
end
@derwiki
derwiki / s3.sh
Last active May 31, 2018 08:56 — forked from chrismdp/s3.sh
PUT to S3 via cURL, Bash -- no external dependencies
#!/bin/bash
# usage ./s3.sh filename.ext remote/path/filename.ext
S3_KEY="REDACTED"
S3_SECRET="REDACTED"
S3_BUCKET="REDACTED"
REMOTE_PATH=$2
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
@derwiki
derwiki / derewecki-resume.txt
Last active November 12, 2020 15:30
Resume for Adam Derewecki
Adam Derewecki
adam@der.wiki
github.com/derwiki
linkedin.com/in/derewecki
Lyft, Senior Software Engineer (11/2017 - Present)
* Tech stack: Python 3/React/AWS
* Building products for self-driving cars; formerly coupons and subscriptions
Certain Lending, Senior Software Engineer Contractor (9/2017 - 11/2017)
@derwiki
derwiki / website.rb
Created March 10, 2016 17:04
Overriding `default_scope` in `has_many` `through` association.
class Website < ActiveRecord::Base
default_scope { where.not(is_private: true) }
end
@derwiki
derwiki / README.md
Last active September 27, 2023 17:50
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.

Class.new(ActiveSupport::LogSubscriber) do
def sql(event)
return
query = event.payload[:sql]
if query =~ /SELECT/
Rails.logger.info "Traced query: #{query}"
Rails.logger.info '!stacktrace!begin'
# Rails.logger.info Rails.backtrace_cleaner.clean(caller).join("\n")
Rails.logger.info Rails.backtrace_cleaner.clean(caller).first
Rails.logger.info '!stacktrace!end'
[5] pry(main)> geoip.city('8.18.218.175')
=> #<struct GeoIP::City
request="8.18.218.175",
ip="8.18.218.175",
country_code2="US",
country_code3="USA",
country_name="United States",
continent_code="NA",
region_name="",
city_name="",
@derwiki
derwiki / CameraLends-logo.psd
Last active August 29, 2015 14:20
CameraLends Multimedia
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.