Skip to content

Instantly share code, notes, and snippets.

class Array
# does a kind of ZIP compression for integer arrays
# examples:
# [0,1,2,3].compress! => [[0, 4]]
# [1,2,3,8,9,10,11].compress! => [[1, 3], [8, 4]]
# [1,3,5].compress! => [1, 3, 5]
# [1,3,5,6].compress! => [1, 3, [5, 2]]
def compress!
compact!
return [] if empty?
@olkeene
olkeene / AppTasks.rake
Created May 4, 2009 17:47
Continuous Testing and Testing Single Methods in Ruby on Rails
# rake app:test:file file=sample_test,other_test,third_test,etc_test
# rake app:test:file file=sample name=method_to_test
# rake app:test:monitor
namespace :app do
namespace :test do
# accepts ARGV[1] as parameter for test file patterns. Format:
# ARGV[1] = "file=[pattern]" (pattern can be a comma list of multiple files)
# ARGV[2] = "name=[method]" name of method to run in test file
desc "Run a single test without preparing database first. Smart about finding the test file.\n Syntax: rake app:test file=[basename] where basename matches (globbed) test(s).\n Will accept multiple comma delimted file patterns. "
module File_ext
# Adds some methods to a file handle so that it looks like a file upload
def as_upload(content_type = nil, fname = nil)
content_type ||= 'text/comma-separated-values'
# Make some JIT methods that make us look like an uploaded Tempfile
class << self
attr_reader :original_filename, :content_type
def local_path; path; end
def size; File.size(path); end
end
class ApplicationController < ActionController::Base
# rescue_from ActionController::RoutingError... do |exception|
# render :template => 'shared/404', :status => :not_found
# end
#
# OR
#
def render_optional_error_file(status_code)
if status_code == :not_found
require 'fastercsv'
def download
filename = 'accounts.csv'
headers.merge!(
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=\"#{filename}\"",
'Content-Transfer-Encoding' => 'binary'
)
@performed_render = false
@olkeene
olkeene / caller_method.rb
Created November 26, 2009 05:49
Need to know which method called you
# Need to know which method called you ? Its as simple as:
# caller = CallChain.caller_method
class CallChain
def self.caller_method(depth=1)
parse_caller(caller(depth+1).first).last
end
private
namespace :translations do
desc "Show available locales"
task :available => :environment do
puts "Available locales: #{I18n.available_locales.join(', ')}"
end
desc "Show missed translations depending on default language environment (:#{I18n.default_locale} by default). You can change it providing LOCALE argument"
task :missed => :environment do
locale = ENV['LOCALE'] || I18n.default_locale
puts "Using :#{locale} as default locale"
# *params* holding a ref_name
# *session*, *cookies* - ref_tracker.tocken
# *check_referral* method only need to handle params ref_name, create ref_tracker and assign it's tocken to session and cookies
def check_referral
return if params[ReferralTracker::REF_KEY].blank? || logged_in?
ref_name = params[ReferralTracker::REF_KEY]
ref_data = RefData.find_by_ref_name(ref_name)
return unless ref_data # referrer is not exists. return
Processing Admin::ExperimentsController#get_stat (for 127.0.0.1 at 2010-06-08 05:10:36) [POST]
Parameters: {"authenticity_token"=>"qGu1YU26HRW+NT4zF5VIOHtg41Z7nbgwJJ5UfjkJxSw=", "experiment"=>{"name"=>"Name", "offer_if_not_raise"=>"", "inactive_from_start"=>"100", "offer_everyone"=>"1", "offer_not_activated_users"=>"0", "raise_vcash_to"=>"", "inactive_group"=>"1", "inactive_from_end"=>"", "inactive_between_start"=>"", "offer_group"=>"1", "vested_cents_skip"=>"", "limit_to"=>"10", "inactive_between_end"=>""}}
User Load (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 63703) LIMIT 1
ExperimentUser Load (2.0ms) SELECT * FROM `experiment_users` WHERE (`experiment_users`.`user_id` = 63703) AND (`experiment_users`.`checked` = 0) LIMIT 1
User Load (0.0ms) SELECT `users`.* FROM `users` INNER JOIN `traits` ON `traits`.id = `users`.trait_id WHERE (((users.id NOT IN(select user_id from experiment_users)) AND (Date(traits.last_activity_on) <= '
@olkeene
olkeene / page.html
Created June 21, 2010 09:01
nasted doughnut chart with Highchart
<div id="container" style="height: 400px"></div>