you are here: codestackercodes [RSS]leozera [RSS]

humanized attributes

class User < ActiveRecord::Base

  HUMANIZED_ATTRIBUTES = {
    :email => "E-mail address"
  }

  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super
  end

end

copy to clipboard

function copyToClipBoard(id) {
    Copied = document.getElementById('id').innerText.createTextRange();
    Copied.execCommand("Copy");
}

back to top

<a href="#top" onclick="$('html, body').animate({scrollTop:0}, 'slow'); return false;">top</a>

unobstrusive popup

http://www.quirksmode.org/js/popup.html

   function popup(url) {
        newWindow = window.open(url, 'name', 'height=400,width=335,scrollbars=yes');
        if (window.focus) { newWindow.focus() }
            return false;
    }

// <a href="link.html" onclick="popup('link.html');">Link</a>

force download .htaccess

AddType application/octet-stream .doc .mov .avi .pdf .xls .mp4

delete recursively a file (.DS_Store, thumbs.db...)

find . -name ".DS_Store" -depth -exec rm {} \;

wordpress popular posts

<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

hide/show dl list with jquery

		$(document).ready(function() {
			$('dd').toggle();
			$('dt').click(function() {$(this).next('dd').toggle('slideDown');});
		});

ie 6 and 7 selectors hack

* html #selector {property:value;} /* Only IE6 and lower will see this */
*+html #selector {property:value;} /* Only IE7 will see this */

backup all mysql database

mysqldump -u username -ppassword –all-databases > dump.sql

iPhone CSS

<link media="only screen and (max-device-width: 480px)" href="iPhone.css" type="text/css" rel="stylesheet" />

paperclip ie validation

validates_attachment_content_type :image,
  :content_type => [
    'image/jpeg',
    'image/pjpeg', # for progressive jpeg (IE mine-type for regular jpeg) 

    'image/png',
    'image/x-png', # IE mine-type for PNG

    'image/gif'
]

hover for ie 6

/* IE6 - pseudo class :hover */ 
$(document).ready(function(){ 
	if(jQuery.browser.msie && jQuery.browser.version<7){ 
		$('[class*="bla"]').hover( 
		function () { 
			$(this).addClass('hover'); 
		}, 
		function () { 
			$(this).removeClass('hover'); 
		} 
		); 
	} 
});

vertical align plugin for jquery

http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/

(function ($) {
// VERTICALLY ALIGN FUNCTION

$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

// $('#mydiv').vAlign();

hartija css print framework

http://code.google.com/p/hartija/

body { 
width:100% !important;
margin:0 !important;
padding:0 !important;
line-height: 1.4;
word-spacing:1.1pt;
letter-spacing:0.2pt; font-family: Garamond,"Times New Roman", serif; color: #000; background: none; font-size: 12pt; }

h1,h2,h3,h4,h5,h6 { font-family: Helvetica, Arial, sans-serif; }
h1{font-size:19pt;}
h2{font-size:17pt;}
h3{font-size:15pt;}
h4,h5,h6{font-size:12pt;}

code { font: 10pt Courier, monospace; } 
blockquote { margin: 1.3em; padding: 1em;  font-size: 10pt; }
hr { background-color: #ccc; }

img { float: left; margin: 1em 1.5em 1.5em 0; }
a img { border: none; }

a:link, a:visited { background: transparent; font-weight: 700; text-decoration: underline;color:#333; }
a:link[href^="http://"]:after, a[href^="http://"]:visited:after { content: " (" attr(href) ") "; font-size: 90%; }
a[href^="http://"] {color:#000; }

table { margin: 1px; text-align:left; }
th { border-bottom: 1px solid #333;  font-weight: bold; }
td { border-bottom: 1px solid #333; }
th,td { padding: 4px 10px 4px 0; }
tfoot { font-style: italic; }
caption { background: #fff; margin-bottom:2em; text-align:left; }
thead {display: table-header-group;}
tr {page-break-inside: avoid;} 

finding duplicates entries

having example in rails. from: http://blog.grayproductions.net/articles/five_activerecord_tips

duplicates = User.find( :all,
  :select     => "email, COUNT(email) AS duplicate_count",
  :conditions => "email IS NOT NULL AND email != ''",
  :group      => "email HAVING duplicate_count > 1"
)

count domain users using sql

from: http://www.mendable.com/sql-trick-where-are-your-users-from/

SELECT COUNT(*) AS Total, SUBSTRING_INDEX(email, '@', -1) AS Domain FROM users 
GROUP BY SUBSTRING_INDEX(email, '@', -1) ORDER BY COUNT(*) DESC LIMIT 10;

jquery fade and slide toggle plugin

from: http://www.position-absolute.com/articles/jquery-fade-and-slide-toggle-plugin/

jQuery.fn.fadeSliderToggle = function(settings) {
 	 settings = jQuery.extend({
		speed:500,
		easing : "swing"
	}, settings)
	
	caller = this
 	if($(caller).css("display") == "none"){
 		$(caller).animate({
 			opacity: 1,
 			height: 'toggle'
 		}, settings.speed, settings.easing);
	}else{
		$(caller).animate({
 			opacity: 0,
 			height: 'toggle'
 		}, settings.speed, settings.easing);
	}
};

// use: $('#my_div').fadeSliderToggle();

succ method

from: http://www.ruby-doc.org/core/classes/String.html#M000795

>> "abb".succ
=> "abc"
>> "1".succ
=> "2"
>> "1999zzz".succ
=> "2000aaa"
>> 

hirb example

to install:
gem install cldwalker-hirb --source http://gems.github.com

>> require 'hirb'
=> []
>> Hirb.enable
=> nil
>> Exam.find(:all, :limit => 5)
+----+----------+----------+----------+----------+---------+----------+------+
| id | title    | exam     | creat... | updat... | user_id | categ... | hits |
+----+----------+----------+----------+----------+---------+----------+------+
| 1  | Simul... | Simul... | 2009-... | 2009-... | 1       | 1        | 152  |
| 2  | Simul... | Simul... | 2009-... | 2009-... | 1       | 1        | 143  |
| 3  | Espec... | Espec... | 2009-... | 2009-... | 1       | 1        | 1089 |
| 4  | Espec... | Espec... | 2009-... | 2009-... | 1       | 1        | 80   |
| 5  | Espec... | Espec... | 2009-... | 2009-... | 1       | 1        | 40   |
+----+----------+----------+----------+----------+---------+----------+------+
5 rows in set
>>
Displaying Code 121 - 140 of 270 in total