Skip to content

Instantly share code, notes, and snippets.

View fjfish's full-sized avatar

Francis Fish fjfish

View GitHub Profile
@fjfish
fjfish / timer.rb
Created April 20, 2024 10:23
Timer wrapper if you want to time something
def timer(id)
start_time = Time.zone.now
result = yield
elapsed_time = Time.zone.now - start_time
puts "Find #{id} Execution time: #{elapsed_time.round(2)}s"
result
end
@fjfish
fjfish / models.rb
Last active December 1, 2023 11:20
Automatic generation of rspec model tests and factories for factory girl
class Models
def self.generate what = :model
Rails.application.eager_load!
ActiveRecord::Base.descendants.each do |model|
method(what)[model]
end
true
end
def self.factory model
factory_file_name = "spec/factories/#{model.name.underscore}.rb"
@fjfish
fjfish / SearchableAdapter.java
Created June 30, 2012 15:48
Simple String Adapter for Android ListView that has a filter that gives whatever it finds and ignores word boundaries
package com.yourco.yourapp;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
@fjfish
fjfish / bad-words
Created October 1, 2012 15:34
Plain text file of regexps to match "bad" words, e.g. using grep -fi bad-words *.txt
[^[:alnum:]]ahole[^[:alnum:]]
[^[:alnum:]]anus[^[:alnum:]]
[^[:alnum:]]ash[0o]le[^[:alnum:]]
[^[:alnum:]]ass[^[:alnum:]]
[^[:alnum:]]azz[^[:alnum:]]
[^[:alnum:]]bassterd[^[:alnum:]]
[^[:alnum:]]bast[ae]+rd[^[:alnum:]]
[^[:alnum:]]b[i1]+[a]*tch[^[:alnum:]]
[^[:alnum:]]bl[o0]+wj[o0]+b[^[:alnum:]]
[^[:alnum:]]boffing[^[:alnum:]]
@fjfish
fjfish / make_me_a.rb
Last active August 18, 2021 13:06
Pass a model in and it prompts you for the values of the columns
# Construct a hash to create a given model for testing
# Pressing return ignores the column
def make_me_a model
{}.tap do |model_hash|
model.columns.dup.delete_if { |x| x.name.in?(%w[created_at updated_at]) }.each do |column|
name = column.name
puts "#{name}?"
value = gets.chomp
model_hash[name.to_sym] = value if value.length > 0
end
@fjfish
fjfish / check_migrations.rb
Last active July 20, 2017 15:14
Script to check if migrations in the structure.sql file match the ones on the file system
IO.readlines("db/structure.sql").each do |line|
if line["INSERT INTO schema_migrations"]
migration_id = line.split(/'/)[1]
puts "#{migration_id} is missing" if Dir.glob("db/migrate/#{migration_id}_*.rb").empty?
end
end
@fjfish
fjfish / find-bots
Created June 23, 2016 15:07
Find bots plaguing your apache installation
#! /bin/bash
# Get info xmrpc get the ip sort count sort for humans
cat /var/log/apache2/*log | grep xmlrpc | awk '{ print $1 '} | sort | uniq -c | sort -h
@fjfish
fjfish / purge_tags.rb
Created August 22, 2013 09:34
Remove all html (and probably xml) tags from an input string
def purge_tags taggy
taggy.gsub(/\<style.*?\<\/style\>/mi,'').gsub(/\<script.*?\<\/script\>/mi,'').
gsub(/<\/?[a-z][a-z0-9[:space:]]*[^<>]*>/, '').gsub( /&[^;]+;|[\r\n]/,'').
gsub( /[<>]*/, '').strip
end
@fjfish
fjfish / combinations.rb
Created July 30, 2013 16:16
get the combinations for a table for laying out in cucumber
strings = ["Not Set", "Participant", "Manager"]
strings.product(strings).each do |pair|
puts sprintf("|1 |%-12s |%-12s |", *pair)
end
@fjfish
fjfish / Create Form Fields
Created July 15, 2013 17:51
Put your list of form fields in, load the file in irb, and out pops the html ready to be edited without you having to do a lot of repetitive typing
def make_group col
<<-EOT
<div class="control-group">
<%= form.label :#{col}, class: 'control-label' %>
<div class="controls">
<%= form.text_field :#{col}, { class: 'input-xlarge' } %>
</div>
</div>
EOT
end