Skip to content

Instantly share code, notes, and snippets.

View 0xradical's full-sized avatar

Thiago Brandão 0xradical

View GitHub Profile
@0xradical
0xradical / first_with.rb
Last active December 14, 2015 16:49
Find first that matches block
module QueryingWithBlock
def first_with(&block)
find_each do |record|
return record if block.call(record)
end
end
end
# ActiveRecord::Relation includes QueryingWithBlock
ActiveRecord::Relation.class_eval do
@0xradical
0xradical / reset_mac_password.txt
Created March 4, 2013 04:26
reset_mac_password.txt
Boot into single user mode (press Command-S at power on)
Type fsck -fy
Type mount -uw /
Type launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
Type dscl . -passwd /Users/username password, replacing username with the targeted user and password with the desired password.
Reboot
@0xradical
0xradical / phone_query.rb
Last active December 14, 2015 07:29
Script to query phones
class PhoneQuery
def initialize(input)
@input = input
end
def to_sql
query = Phone.where('(1 = 0)').to_sql
return query if @input.blank?
require 'ap'
require 'ripper'
CLASS_TEXT = "class A; end; class B; end"
CLASS_SEXP = Ripper.sexp(CLASS_TEXT)
BIG_TEXT = "class A; def a; @a = b; end; def b; @d = a; @e = a; end; end; module B; def b; end; end"
BIG_SEXP = Ripper.sexp(BIG_TEXT)
@0xradical
0xradical / github_ruby_coloring.rb
Created February 7, 2013 17:28
Testing Github colors for Ruby
require "test"
CONSTANT = 777
# Sample comment
class Module::Class
include Testcase
render :action => 'foo'
def foo(parameter)
data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
@0xradical
0xradical / csv_streamer.rb
Created November 13, 2012 13:01
CSV streamer
# controller method
def csv_export
respond_to do |format|
format.csv {
@filename = "responses-#{Date.today.to_s(:db)}.csv"
self.response.headers["Content-Type"] ||= 'text/csv'
self.response.headers["Content-Disposition"] = "attachment; filename=#{@filename}"
self.response.headers['Last-Modified'] = Time.now.ctime.to_s
@0xradical
0xradical / controller_not_dry.rb
Created November 13, 2012 12:38 — forked from bsodmike/controller_not_dry.rb
Streaming CSV file to the Client with Rails 3.1
format.csv do
@scheduled_session = Session.find(params[:id])
@applicants = @scheduled_session.applicant_signups
filename = "#{@scheduled_session.title.parameterize.downcase}_applicant_list_#{Time.now.strftime('%d%m%Y')}.csv"
headers['Content-Disposition'] = 'attachment; filename="' + filename + '"'
titles = [
"Title", "First Name", "Last Name", "DOB Day", "DOB Month", "DOB Year", "Nationality", \
"Email", "Mobile Number", "Address Line 1", "Address Line 2", "Place of Work", \
"Discipline/Speciality", "Job Title", "Key Responsibilities", "Payment Ref."
]
@0xradical
0xradical / foo_controller.rb
Created November 12, 2012 23:10 — forked from lloeki/foo_controller.rb
Streaming (CSV) data in Rails 3.2
class FooController
respond_to :csv
def index
@foos = Foo.scoped
if stale?(:last_modified => Foo.something)
respond_with @gta do |format|
format.csv { stream_csv @foo }
end
end
@0xradical
0xradical / server from new install
Created October 5, 2012 16:29 — forked from rossnelson/server from new install
Leopard, Homebrew, Git, RVM, Ruby, MySQL and ImageMagick
# 2012-01-07
#
# Mac OS X 10.5.8
# Homebrew 0.9
# Xcode 3.1.4
# Git 1.7.10
# RVM 1.3.0
# Ruby 1.9.3
# MySQL 5.5.20
# ImageMagick 6.7.5-7