Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
219 lines (183 sloc) 6.14 KB
require 'tmpdir'
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
after_rails_loaded = lambda do |*args|
app_reference = defined?(app) && app
if defined?(Delayed::Job)
DJ = Delayed::Job
DJ.class_eval do
def run; run_with_lock(1, 1); end
class << self
def method_histogram
all.map(&:payload_object).map(&:method).to_ordered_histogram
end
def dump
all.map(&:payload_object)
end
def creation_summary
find_by_sql(<<-SQL).inject([]) { |ary, j| ary << OpenStruct.new(j.attributes); ary }
SELECT regexp_matches(handler, E'method: :(.*?)\n') as method,
to_char(created_at, 'YYYY/MM/DD HH24') as created_hour,
count(*) as count
FROM delayed_jobs GROUP BY created_hour, method
ORDER BY created_hour DESC, count(*) DESC
SQL
end
end
end
end
if defined?(Resque)
def Resque.delete_all
self.queues.each do |q|
self.remove_queue(q)
end
end
end
if defined? Rails
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
if defined? ActiveRecord::Base
ActiveRecord::Base.class_eval do
define_method(:navigate) do |*args|
url_options = ActionMailer::Base.default_url_options || (args.first.respond_to?(:default_url_options) && args.first.default_url_options)
base_url = args.first ||
(url_options.present? && 'http://' + url_options[:host] + ([443, 80].include?(url_options.fetch(:port, 80)) ? '' : url_options[:port].to_s)) ||
'http://localhost:3000'
if respond_to? :path
object_path = path
elsif app_reference.respond_to?("#{self.class.name.downcase}_path")
object_path = app_reference.send("#{self.class.name.downcase}_path", self)
else
object_path = app_reference.url_for(self).sub(/^https?:\/\/[^\/]+/,'')
end
system('open', "#{base_url + object_path}")
end
def dev_passwordify!(new_password = 'test123')
raise "Whoops, can't passwordify this one" unless respond_to?(:password=)
self.password = new_password
save!
end
end
clazz = User rescue nil # rails auto-loading
if clazz
if User.respond_to?(:find_by_login)
def andrew; User.find_by_login('andrew'); end
alias :me :andrew
end
end
end
TMail::Mail rescue nil # make sure constant gets autoloaded by rails if necessary
if defined?(TMail::Mail)
TMail::Mail.class_eval do
def h(*args); ERB::Util.h(*args); end
def show
emails_dir = "#{Rails.root}/tmp/emails"
FileUtils.mkdir_p(emails_dir)
mail_path = File.join(emails_dir, "#{object_id}.html")
File.open(mail_path, "w") do |f|
f.puts '<meta http-equiv="Content-type" content="text/html; charset=utf-8"></meta>'
f.puts "<pre>"
header.each do |k, v|
f.puts h "#{k}: #{v}"
end
f.puts h "Smtp_envelope_to: #{smtp_envelope_to}" if defined?(smtp_envelope_to)
['cc', 'bcc', 'reply-to'].each do |optional_recipients|
if self[optional_recipients].present?
f.puts h "#{optional_recipients.to_s.humanize}: #{self[optional_recipients]}"
end
end
f.puts "</pre>"
f.write self.body
end
system("open", "file:///#{mail_path}")
end
end
end
def with_smtp_email
old_delivery_method, ActionMailer::Base.delivery_method = ActionMailer::Base.delivery_method, :smtp
yield
ensure
ActionMailer::Base.delivery_method = old_delivery_method
end
end
if defined?(Pry)
Pry.hooks.add_hook(:when_started, :add_rails_helpers, after_rails_loaded)
elsif Object.const_defined?('IRB')
IRB.conf[:IRB_RC] = after_rails_loaded
end
def benchmark(&block)
require 'benchmark'
Benchmark.bm {|x| x.report(&block) }
end
def show_log
change_log(STDOUT)
end
def hide_log
change_log(nil)
end
def change_log(stream, colorize=true)
ActiveRecord::Base.logger = ::Logger.new(stream)
ActiveRecord::Base.clear_all_connections!
if ActiveRecord::Base.respond_to?(:colorize_logging=)
ActiveRecord::Base.colorize_logging = colorize
end
end
class Object
def clip
to_s.tap do |string|
IO.popen('pbcopy', 'w') { |io| io << string }
end
end
def show_html
to_s.tap do |html|
file_path = File.join(Dir.tmpdir, "show_html_#{html.object_id}.html")
File.open(file_path, 'w') do |f|
f << '<!DOCTYPE html>'
f << '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>'
f << html
f << '</body></html>'
end
system("open", "file:///#{file_path}")
end
end
end
def decode_session(session)
Marshal.load(session.split('--').first.unpack('m').flatten.first)
end
def add_debugger(clazz, method)
debugger_method = binding.respond_to?(:pry) ? 'binding.pry' : 'debugger'
unless clazz.method_defined? "#{method}_with_debugger"
clazz.class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{method}_with_debugger(*args, &block)
#{debugger_method}
#{method}_without_debugger(*args, &block)
end
alias_method_chain :#{method}, :debugger
CODE
end
end
def remove_debugger(clazz, method)
return unless clazz.method_defined? "#{method}_with_debugger"
clazz.class_eval do
alias_method method, "#{method}_without_debugger"
undef_method "#{method}_with_debugger"
undef_method "#{method}_without_debugger"
end
end
def debug_sql_calls(clazz, method = :find_by_sql)
add_debugger(class << clazz; self; end, :find_by_sql)
end
def benchmark_url(url, attempts = 100)
require 'benchmark'
require 'net/http'
total = 0.0
attempts.times do |i|
doc = nil
actual_time = (Benchmark.realtime { doc = Net::HTTP.get(URI.parse(url)) }.to_f * 1000).round
puts "Trial #{i}: actual_time: #{actual_time}ms"
total += actual_time
end
puts "\nTotal time was #{(total / 1000)} seconds, for an average of #{(total / attempts)}ms"
end