Just posting it here since I did not find anything on google when searching this for our rails 2 upgrade ๐
view_context.instance_variable_get(:@_virtual_path)
Just posting it here since I did not find anything on google when searching this for our rails 2 upgrade ๐
view_context.instance_variable_get(:@_virtual_path)
Kill those warnings without having to load activesupport, just drop this wherever you need ๐
Usage
class Foo; BAR = 1; end
silence_warnings{ Foo.const_set :BAR, 2 }
Code
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
Tried a few strategies, but this seems to be the fastest:
DatabaseCleaner truncate_all: 0.8s
This: 0.2s
# fast truncation of all tables that need truncations (select is 10x faster then truncate)
# https://grosser.it/2012/07/03/rubyactiverecord-fastest-way-to-truncate-test-database/
def truncate_all_tables
config = ActiveRecord::Base.configurations[::Rails.env]
connection = ActiveRecord::Base.connection
connection.disable_referential_integrity do
connection.tables.each do |table_name|
next if connection.select_value("SELECT count(*) FROM #{table_name}") == 0
case config["adapter"]
when "mysql", "mysql2", "postgresql"
connection.execute("TRUNCATE #{table_name}")
when "sqlite", "sqlite3"
connection.execute("DELETE FROM #{table_name}")
connection.execute("DELETE FROM sqlite_sequence where name='#{table_name}'")
end
end
connection.execute("VACUUM") if config["adapter"] == "sqlite3"
end
end
Make your links stay in a container, great for will_paginate / sorting headers and friends ๐
Usage
// ajaxify all links that have “tab=my_container” in their href
$(“#my_container”).ajaxifyContainer(‘a[href*=”tab=my_container”]’)
Code
// make selected links in a container replace the container
// https://grosser.it/2012/06/21/jquery-pjaxcontainer-extensions
$.fn.ajaxifyContainer = function(selector){
selector = selector || 'a';
var $container = $(this);
$(selector, $container).live('click', function(){
$container.html("Loading ...").load($(this).attr("href"));
return false;
});
};
// show loading animation and errors inside the container that is being replaced
// https://grosser.it/2012/06/21/jquery-load-extension-to-indicate-loading-and-errors
$.fn.responsiveLoad = function(url, callback){
var loading = '
';
var $container = $(this);
$container.html(loading).load(url, function(response, status, xhr){
if (status == "error") {
$container.html("Error:" + xhr.status + " " + xhr.statusText);
} else {
if(callback) callback(response, status, xhr);
}
});
};