Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Nick Hammond nickhammond.com

rails preserve line breaks



simple_format(string)

CSS Global Reset w/ strong, em

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-weight: inherit;
    font-style: inherit;
    font-size: 100%;
    font-family: inherit;
    vertical-align: baseline;
    background: transparent;
}

:focus {
    outline: 0;
}

body {
	
}

ol, ul {list-style: none;}

caption, th, td {
  text-align: left;
  font-weight: normal;
}

blockquote:before, blockquote:after, q:before, q:after {content: "";}
blockquote, q {quotes: "" "";}

strong{font-weight: bold;}
em{font-style: italic;}
.clear{clear: both;}

#content:after{
  content: ".";
  height: 0;
  display: block;
  clear: both;
  visibility: hidden;
}

.left, #left{
	float: left;
	display: inline;
}

.right, #right{
	float: right;
	display: inline;
}

CSS Clear

#something:after{
 content: ".";
  height: 0;
  display: block;
  clear: both;
  visibility: hidden;
}

Rails numbered migrations instead of timestamps

change migrations to the old rails way of just version numbers instead of the mysql timestamp. Only recommended if a few people are working on a project.
config.active_record.timestamped_migrations = false

target blank with jquery and prototype

To be xhtml strict compliant there can't be the target attribute in an anchor tag. Below is a way around it with prototype and jquery.

jQuery
$(function(){
  $('a[@rel$='external']').click(function(){
    this.target = "_blank";
  });
});


Prototype
Event.observe(window, 'load', function() {
    $$('a[rel="external"]').each(function(link){
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
            link.writeAttribute('target','_blank');
        }
    });
});



All links that you want to open in a blank window now need to have rel="external" set in the anchor link.
<a rel="external" href="http://google.com">Google Rocks.</a>

Simple javascript image slideshow

HTML
<div id="slide-show">
     <ul id="slide-images">
     	<li><img src="images/one.jpg" alt="One" title="One" /></li>
     	<li><img src="images/two.jpg" alt="Two" title="Two" /></li>
     	<li><img src="images/three.jpg" alt="Three" title="Three" /></li>
     </ul>
</div>


CSS
#slide-images{
	position:relative;
	display:block;
	margin:0px;
	padding:0px;
	width:290px; /* Adjust to width-height of your images */
	height:142px;
	overflow:hidden;
}

#slide-images li{
	position:absolute;
	display:block;
	list-style-type:none;
	margin:0px;
	padding:0px;
	background-color:#FFFFFF;
}

#slide-images li img{
	display:block;
	background-color:#FFFFFF;
}


JS
var delay = 1000;
var start_frame = 0;

function init() {
	var lis = $('slide-images').getElementsByTagName('li');
	
	for( i=0; i < lis.length; i++){
		if(i!=0){
			lis[i].style.display = 'none';
		}
	}
	end_frame = lis.length -1;
	
	start_slideshow(start_frame, end_frame, delay, lis);
	
	
}



function start_slideshow(start_frame, end_frame, delay, lis) {
	setTimeout(fadeInOut(start_frame,start_frame,end_frame, delay, lis), delay);
}


function fadeInOut(frame, start_frame, end_frame, delay, lis) {
	return (function() {
		lis = $('slide-images').getElementsByTagName('li');
		Effect.Fade(lis[frame]);
		if (frame == end_frame) { frame = start_frame; } else { frame++; }
		lisAppear = lis[frame];
		setTimeout("Effect.Appear(lisAppear);", 0);
		setTimeout(fadeInOut(frame, start_frame, end_frame, delay), delay + 1850);
	})
	
}


Event.observe(window, 'load', init, false);


You also need the following javascript files... prototype.js, scriptaculous.js, effects.js

Just load the js above in your head and it'll start slidin'

Credits: Andrew Sellick - http://www.andrewsellick.com/30/simple-javascript-slide-show-using-scriptaculous

Reload rails plugins in console


add to environment.rb
config.reload_plugins = true

Recursive download over ftp

Need to ensure consistency, might miss files because of permissions. This should only be used if you don't have shell access to a box. If you have shell access then you should archive it, compress it and download it.

wget -r ftp://user:pass@domain.com

Basic htaccess authentication


AuthType Basic
AuthName "Protected"
AuthUserFile /var/www/blah/.htpasswd
Require valid-user

generate .htpasswd from command line

htpasswd -bc .htpasswd user pass