Skip to content

Instantly share code, notes, and snippets.

View jmgarnier's full-sized avatar
💭
❤️🚲

Jean-Michel Garnier jmgarnier

💭
❤️🚲
View GitHub Profile
@jmgarnier
jmgarnier / factory_girl_ar_instrumentation.rb
Created September 8, 2014 09:06
Use FactoryGirl AR instrumentation to track how much time is spent creating test data
# https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#activesupport-instrumentation
RSpec.configure do |config|
test_data_setup_time = 0
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
execution_time_in_seconds = finish - start
test_data_setup_time += execution_time_in_seconds
@jmgarnier
jmgarnier / explain_analyze_in_dev.rb
Last active August 29, 2015 14:21
Postgres "EXPLAIN ANALYZE" from Rails 3.2 console with syntax hightlighting
if Rails.env.development?
require 'active_record/connection_adapters/postgresql_adapter'
require 'pygments' # add `gem 'pygments.rb', require: false` to Gemfile
module ExplainAnalyze
@@use_explain_analyze = false
def explain_analyze
@@use_explain_analyze = true
# Monday is 26/01/2009, Sunday is 01/02/2009
# I have to use a constand because RSpec can not access variables outside of "it" blocks ...
TWENTY_SIX_OF_JANUARY_20009 = "26/01/2009".to_date_with_european_date_format
describe "On going events" do
before :each do
@event = create_ongoing_event_for_barcelona(
:starts_at => TWENTY_SIX_OF_JANUARY_20009,
:ends_at => "01/02/2009".to_date_with_european_date_format)
script/cucumber features/_balloon_day/a_register_campaign.feature --drb
/Library/Ruby/Gems/1.8/gems/cucumber-0.3.9.3/lib/cucumber/rails/world.rb:97: undefined method `World' for main:Object (NoMethodError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from /Users/jeanmichel/Projects/betterplace/trunk/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:496:in `require'
from /Users/jeanmichel/Projects/betterplace/trunk/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'
from /Users/jeanmichel/Projects/betterplace/trunk/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:496:in `require'
from /Users/jeanmichel/Projects/betterplace/trunk/features/support/env.rb:18
from /Library/Ruby/Gems/1.8/gems/bmabey-spork-0.4.4/bin/../lib/spork.rb:15:in `each_run'
from /Users/jeanmichel/P
/* Mozilla Ubiquity Command for Diigo, currently only support add bookmark.
* Some code from https://ubiquity.mozilla.com/standard-feeds/social.js
*/
var store = Application.storage;
const DIIGO_CUR_LOGIN = "ubiquity_diigo_cur_login";
var Choices = {"yes": "yes", "no": "no"};
var noun_type_share = {
_name: "yes/no",
require 'benchmark'
$require_level = 0
module Kernel
alias require_without_timing require
def require(path)
result = seconds = nil
begin
$require_level += 1
@jmgarnier
jmgarnier / stats.rake
Created November 7, 2011 14:10
rails statistics hacked to get gross LOC of Views, css, js, coffeescript, RSpec and cucumber
# Note THIS IS some hackety hack code without tests. CodeStatistics class could be rewritten to be more configurable ...
task :stats => [:statsetup, :stats_assets]
# Setup specs / cuke & js for stats
task :statsetup do
require 'code_statistics'
class CodeStatistics
alias calculate_statistics_orig calculate_statistics
@jmgarnier
jmgarnier / active_record_migration_ext.rb
Created February 17, 2012 10:16
Fast rename columns for MYSQL, very handy for huge tables
class ActiveRecord::Migration
def self.fast_add_columns(table_name, new_columns_count)
tmp_table_name = "#{table_name}_tmp"
execute "DROP TABLE IF EXISTS #{tmp_table_name}"
execute "CREATE TABLE #{tmp_table_name} LIKE #{table_name}"
yield tmp_table_name
execute("INSERT INTO #{tmp_table_name} SELECT * #{', NULL' * new_columns_count} FROM #{table_name}")
@jmgarnier
jmgarnier / gist:2251117
Created March 30, 2012 12:16
Remove unused images
require "fileutils"
img =Dir.glob("**/*.jpg")+Dir.glob("**/*.png")+Dir.glob("**/*.gif")
data=Dir.glob("**/*.htm*")+Dir.glob("**/*.css")+Dir.glob("**/*.js")
puts "#{img.size} images found & #{data.size} files found to search against"
content=""
data.each do |f|
content+=File.open(f, 'r').read
@jmgarnier
jmgarnier / statistics.rb
Created August 6, 2012 13:34
Inner classes to implement SRP in ruby
class Statistics
def initialize
@configuration = Configuration.new(self)
end
delegate :for_caller,
:rtp_errors,
to: :@configuration