Skip to content

Instantly share code, notes, and snippets.

View chadwilken's full-sized avatar

Chad Wilken chadwilken

View GitHub Profile
@fractaledmind
fractaledmind / has_many_attached.rb
Created October 25, 2023 20:21
Vanilla Rails isomorphic attachment validations
# /app/models/concerns/has_many_attached.rb
module HasManyAttached
extend ActiveSupport::Concern
class_methods do
def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false, **options)
super(name, dependent: :purge_later, service: nil, strict_loading: false)
if options[:file_types].any?
validate "validate_#{name}_file_types".to_sym
@syafiqfaiz
syafiqfaiz / how-to-copy-aws-rds-to-local.md
Last active February 21, 2024 06:00
How to copy production database on AWS RDS(postgresql) to local development database.
  1. Change your database RDS instance security group to allow your machine to access it.
    • Add your ip to the security group to acces the instance via Postgres.
  2. Make a copy of the database using pg_dump
    • $ pg_dump -h <public dns> -U <my username> -f <name of dump file .sql> <name of my database>
    • you will be asked for postgressql password.
    • a dump file(.sql) will be created
  3. Restore that dump file to your local database.
    • but you might need to drop the database and create it first
    • $ psql -U <postgresql username> -d <database name> -f <dump file that you want to restore>
  • the database is restored
@brianhempel
brianhempel / bench_rails_memory_usage.rb
Last active October 6, 2022 12:47
A script to test the memory usage of your Rails application over time. It will run 30 requests against the specified action and report the final RSS. Choose the URL to hit on line 45 and then run with `ruby bench_rails_memory_usage.rb`.
require "net/http"
def start_server
# Remove the X to enable the parameters for tuning.
# These are the default values as of Ruby 2.2.0.
@child = spawn(<<-EOC.split.join(" "))
XRUBY_GC_HEAP_FREE_SLOTS=4096
XRUBY_GC_HEAP_INIT_SLOTS=10000
XRUBY_GC_HEAP_GROWTH_FACTOR=1.8
XRUBY_GC_HEAP_GROWTH_MAX_SLOTS=0
@jberkus
jberkus / gist:6b1bcaf7724dfc2a54f3
Last active January 7, 2024 21:26
Finding Unused Indexes
WITH table_scans as (
SELECT relid,
tables.idx_scan + tables.seq_scan as all_scans,
( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,
pg_relation_size(relid) as table_size
FROM pg_stat_user_tables as tables
),
all_writes as (
SELECT sum(writes) as total_writes
FROM table_scans
@Tombar
Tombar / gist:7882116
Created December 9, 2013 22:26
logstash remove fields with mutate filter
mutate {
remove_field => ['geoip.areacode', 'geoip.continent_code', 'geoip.country_code3', 'geoip.country_name', 'geoip.dma_code',
'geoip.latitude', 'geoip.longitude', 'geoip.postal_code', 'geoip.real_region_name',
'ua.minor', 'ua.major', 'ua.os_minor', 'ua.os_major', 'ua.patch', 'ua.os_name' ]
}
@mattetti
mattetti / rails_json_session.rb
Last active September 23, 2020 07:04
This is a monkey patch to change Rails 4's default session/signed cookie serializer from Marshal to JSON for security and compatibility reasons. Note that this is a hack, a pretty terrible one and you should only use it if you know what you're doing. Also, I only wrote this patch for my own personal use, so don't be surprised if it doesn't work …
# Hack to change the Rails cookie serializer from Marshal to JSON and therefore allow the session
# to be shared between different languages but also avoid that someone knowing the
# cookie secret key could execute arbitrary code on the server by unmarshalling
# modified Ruby code added to the session/permanent cookie.
#
# Note that all users will beed to login again since both the remember me cookie and the session cookies
# won't be valid. Note also that the remember me cookie is tested multiple times per request even when it fails.
# for performance reasons you might want to delete it if these extra cycles are too costly for you.
#
# Rails 4 (not tested on Rails 3).
@ruckus
ruckus / statistics.sql
Created June 5, 2013 23:26
Postgres statistics queries
** Find commmonly accessed tables and their use of indexes:
SELECT relname,seq_tup_read,idx_tup_fetch,cast(idx_tup_fetch AS numeric) / (idx_tup_fetch + seq_tup_read) AS idx_tup_pct FROM pg_stat_user_tables WHERE (idx_tup_fetch + seq_tup_read)>0 ORDER BY idx_tup_pct;
Returns output like:
relname | seq_tup_read | idx_tup_fetch | idx_tup_pct
----------------------+--------------+---------------+------------------------
schema_migrations | 817 | 0 | 0.00000000000000000000
user_device_photos | 349 | 0 | 0.00000000000000000000
@equivalent
equivalent / README.md
Last active December 16, 2021 16:34
String "false" to_bool ... or how to convert Rails/SimpleForm radio buttons to boolean

This gist was writen in 2012 and it was solving specific problem in Rails & SimpleForm. Some fellow developers were pointing out this may be out dated concept. That's why I advise everyone to read comment section bellow to have a full grasp of alternative solutions

other sources that may be helpful to understand why this may not be best idea:

@embs
embs / Rails 3.1 authentication from scratch.markdown
Created June 12, 2012 17:49
Authentication from Scratch with Rails 3.2.5

Authentication from Scratch with Rails 3.2.5

Rails 3.1 gives us a really easy way to authenticate users in our app: http_basic_authenticate_with. Using it, all we need to do is create a model for the user (certainly, User model :P) with an attribute called password_digest and some views feature for login and register users. After all, let's relax and let Rails do the hard work.

What we'll need

Gemfile

gem 'bcrypt-ruby', '~> 3.0.0'

Model

First at all, an User model which we can generate by following: rails g model user email:string password_digest:string and then add the following methodo call to generated class: has_secure_password