Skip to content

Instantly share code, notes, and snippets.

Avatar

Jake Moffatt jakeonrails

View GitHub Profile
@jakeonrails
jakeonrails / report_model_creation_counts.rb
Created November 10, 2011 00:02
Spits out a report at the end of your tests on how many times each model was created
View report_model_creation_counts.rb
if Rails.application.config.report_model_creation_counts
class ReportModelCreationCounts < ActiveRecord::Observer
@@creates = Hash.new { |hash, key| hash[key] = 0 }
def after_create(record)
@@creates[record.class] += 1
end
@jakeonrails
jakeonrails / log_test_names.rb
Created November 10, 2011 00:21
Logs the current example before and after it runs
View log_test_names.rb
if Rails.application.config.log_test_names
RSpec.configure do |config|
config.before(:each) do |x|
Rails.logger.info('*' * 10 + 'Start Example: ' + x.example.metadata[:example_group][:full_description])
end
config.after(:each) do |x|
Rails.logger.info('*' * 10 + 'Finish Example: ' + x.example.metadata[:example_group][:full_description])
View sidebar.html.erb
<h1>Categories</h1>
<% @categories.each do |category| %>
<ul class="X">
<li>
<h5><%= link_to category.name, category %></h5>
</li>
</ul>
<% end %>
@jakeonrails
jakeonrails / api.rb
Created December 14, 2011 02:27
Loading a class inside a module within Rails 3.0
View api.rb
# within app/importer/api.rb
module Importer
class API
# class body here
end
end
@jakeonrails
jakeonrails / rubymine.log
Created January 31, 2012 21:04
rubymine markdown log
View rubymine.log
This file has been truncated, but you can view the full file.
2012-01-27 18:12:33,517 [4154324] INFO - plication.impl.ApplicationImpl - Not enough pooled threads; creating one at:
java.lang.Throwable
at com.intellij.openapi.application.impl.ApplicationImpl$1.newThread(ApplicationImpl.java:150)
at java.util.concurrent.ThreadPoolExecutor.addThread(ThreadPoolExecutor.java:672)
at java.util.concurrent.ThreadPoolExecutor.addIfUnderMaximumPoolSize(ThreadPoolExecutor.java:721)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:657)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:78)
at com.intellij.openapi.application.impl.ApplicationImpl.executeOnPooledThread(ApplicationImpl.java:425)
at com.intellij.openapi.project.FileContentQueue.queue(FileContentQueue.java:85)
at com.intellij.openapi.project.CacheUpdateRunner.processFiles(CacheUpdateRunner.java:72)
@jakeonrails
jakeonrails / xml_parser.rb
Created February 11, 2012 04:23 — forked from kmile/xml_parser.rb
A small nokogiri xml reader DSL.
View xml_parser.rb
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The
# XML Reader is a good way to move a cursor through a (large) XML document fast,
# but is not as cumbersome as writing a full SAX document handler. Read about
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html
#
# Just pass the reader in this parser and specificy the nodes that you are interested
# in in a block. You can just parse every node or only look inside certain nodes.
#
# A small example:
#
@jakeonrails
jakeonrails / database_url.rb
Created May 22, 2012 07:01
Parse heroku DATABASE_URL
View database_url.rb
<%
require 'cgi'
require 'uri'
begin
uri = URI.parse(ENV["DATABASE_URL"])
rescue URI::InvalidURIError
raise "Invalid DATABASE_URL"
end
@jakeonrails
jakeonrails / gist:3895690
Created October 15, 2012 21:34
Postgres table and index sizes
View gist:3895690
SELECT CASE WHEN total IS NULL THEN '' ELSE name END AS table,
index,
pg_size_pretty(size) AS size,
CASE WHEN total IS NULL THEN '' ELSE pg_size_pretty(total) END AS total
FROM
(SELECT name,
index,
size,
total
FROM
@jakeonrails
jakeonrails / robot_user_agents.rb
Created October 15, 2012 21:37 — forked from Sjors/robot_user_agents.rb
Recognize search engines and spammers using user-agents.org
View robot_user_agents.rb
require 'net/http'
require 'xmlsimple'
url = "http://www.user-agents.org/allagents.xml"
xml_data = Net::HTTP.get_response(URI.parse(url)).body
data = XmlSimple.xml_in(xml_data)
agents = data['user-agent'].select{|agent| type = agent["Type"].first; type.include?("R") || type.include?("S")}
agent_names = agents.collect {|agent| agent["String"].first}
@jakeonrails
jakeonrails / benchmark.rb
Created November 6, 2012 23:15 — forked from panthomakos/benchmark.rb
Benchmark Your Bundle
View benchmark.rb
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]