Skip to content

Instantly share code, notes, and snippets.

View fjfish's full-sized avatar

Francis Fish fjfish

View GitHub Profile
@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 / 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 / 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
@fjfish
fjfish / gist:5720959
Created June 6, 2013 11:49
Little script to take a file of quotes either as an argument or from a redirect and turn it into a CSV that hootsuite will load.
#! /usr/bin/ruby
require 'date'
require 'set'
quotes = $<.readlines.map { |line| line.strip }
tag = "#unicorns"
url = "https://leanpub.com/unicorns"
@fjfish
fjfish / gist:5706122
Created June 4, 2013 13:57
Method to create a session object with poltergeist so you can play with it in IRB
# load this file to create a method that creates a capybara session you can play
# with in irb
require 'capybara'
require 'capybara/poltergeist'
require 'ext/capybara/session'
def session(driver = :poltergeist)
Capybara.register_driver :poltergeist do |app|
@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:]]