Tuesday, 23 October 2012

pretty_routes rake

desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :pretty_routes => :environment do
all_routes = ENV['CONTROLLER'] ? Fanhub::Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : Fanhub::Application.routes
routes = all_routes.routes.collect do |route|
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => route.name, :verb => route.verb, :path => route.path, :reqs => reqs}
end
File.open(File.join(Rails.root, "routes.html"), "w") do |f|
f.puts "<html><head><title>Rails 3 Routes</title></head><body><table border=1>"
f.puts "<tr><th>Name</th><th>Verb</th><th>Path</th><th>Requirements</th></tr>"
routes.each do |r|
f.puts "<tr><td>#{r[:name]}</td><td>#{r[:verb]}</td><td>#{r[:path]}</td><td>#{r[:reqs]}</td></tr>"
end
f.puts "</table></body></html>"
end
end
view raw gistfile1.rb hosted with ❤ by GitHub

rake voting emulator

#Save in lib/taks/emulator.rake
#after run rake emulate:mobile
require 'rubygems'
require 'active_support'
require 'logger'
namespace :emulate do
desc "Emulate realtime votes for mobile poll view"
task :mobile, :user, :poll do |t, args|
puts "Starting Emulation"
vals = ["Left", "Right"]
ActiveRecord::Base.logger = Logger.new(STDOUT)
while(true) do
[*1..10].sample.times {Vote.create!(user_id: args.user, poll_id: args.poll, value: vals.sample)}
puts "-----------------------------------------------------------------------------------"
puts ("Sleep for 15 at - #{Time.now}")
sleep(15)
end
end
end
view raw gistfile1.rb hosted with ❤ by GitHub

Sunday, 21 October 2012

Js Profiler

if (window.console && window.console.profile) {
console.profile("label for profile");
// insert code to profile here,
// all function calls will be profiled
console.profileEnd();
}
view raw gistfile1.js hosted with ❤ by GitHub

Saturday, 13 October 2012

window orientationchange

jQuery(window).bind('orientationchange', function(e) {
switch ( window.orientation ) {
case 0:
alert('portrait mode');
break;
case 90:
alert('landscape mode screen turned to the left');
break;
case -90:
alert('landscape mode screen turned to the right');
break;
}
});
view raw gistfile1.js hosted with ❤ by GitHub

Wednesday, 10 October 2012

parse tweets

<html>
<head>
<title>Total tweets</title>
<style type="text/css">
/*
input {
float: left;
clear: both;
}
*/
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var getPage = new RegExp( /page=([^&]+)/ );
var url = "http://search.twitter.com/search.json?q=";
var search, page, pageTotal, tweets, loading;
var beforeCounter = "<b>";
var afterCounter = "</b>";
var totalTweets = 0;
function getTweets(search, page, pageTotal) {
$.getJSON( url + search + '&page=' + page + '&callback=?',
function(data) {
if( data.results.length != 0 && page != pageTotal ) {
$('#pagesDone span').html(page);
getData(data);
}
else {
showTotal();
}
}
);
}
function showTotal() {
$('#totalTweets span').html(beforeCounter + totalTweets + afterCounter);
$('#pagesDone span').html('0');
totalTweets = 0;
loading = false;
}
function getData(data) {
tweets = data.results;
page = getPage.exec(data.next_page);
if( page == null ) {
showTotal();
return;
}
page = page[1];
totalTweets += tweets.length;
getTweets(search, page, pageTotal);
}
function submitTerms() {
$('#totalTweets span').html('');
$('#pagesDone span').html('0');
search = encodeURIComponent($('#query').prop('value'));
page = $('#startPage').prop('value');
pageTotal = $('#pageTotal').prop('value');
if( search == '' ) {
alert('Please enter search query');
return;
}
if( page == 0 ) {
alert('0 not allowed as start page');
return;
}
loading = true;
getTweets(search, page, pageTotal);
}
function status() {
if( loading ) $('#loading,#pagesDone').show();
else $('#loading,#pagesDone').hide();
}
$(function() {
loading = false;
setInterval(status, 10);
});
</script>
</head>
<body>
<h1>Get your twitter counts here!</h1>
<form action="" method="get" onsubmit="submitTerms(); return false;">
<label for="query">Search for: </label>
<input id="query" name="query" type="text" />
<br />
<label for="startPage">Start on what page (must be greater than 1) </label>
<input id="startPage" name="startPage" type="text" value="1" />
<br />
<label for="pageTotal">How many pages (0 to search until no more results) </label>
<input id="pageTotal" name="pageTotal" type="text" value="0" />
<input id="submit" name="submit" type="submit" value="Go" />
<span>or press enter</span>
</form>
<div id="totalTweets">Total tweets: <span></span></div>
<br />
<div id="loading" style="display:none;">Loading!</div>
<div id="pagesDone" style="display:none;">Pages done: <span></span></div>
<span style="display:none;">Do Electric Cars Pollute More Than Their Gasoline Counterparts</span>
</body>
</html>
view raw gistfile1.js hosted with ❤ by GitHub

migrating to (PostgreSQL) 9.2.1


From logs

DETAIL:  The data directory was initialized by PostgreSQL version 9.1, which is not compatible with this version 9.2.1.
FATAL:  database files are incompatible with server



#1. Stop the server
 launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

#2. Move old files
 mv /usr/local/var/postgres /usr/local/var/postgres.old

#3.Create a new dir
 initdb /usr/local/var/postgres

# DONE
# Success. You can now start the database server using:
# postgres -D /usr/local/var/postgres
# or
# pg_ctl -D /usr/local/var/postgres -l logfile start

#4.Upgrade all db
pg_upgrade -d /usr/local/var/postgres.old/ -D /usr/local/var/postgres -b /usr/local/Cellar/postgresql/9.1/bin -B /usr/local/Cellar/postgresql/9.2.1/bin

Fix iphone autocomplete - autocorrect -autocapitalize

function fix_iphone_autocomplete(){
$("#entry_captcha").attr('autocorrect','off');
$("#entry_captcha").attr('autocomplete','off');
$("#entry_captcha").attr('autocapitalize','off');
}
view raw gistfile1.js hosted with ❤ by GitHub

Tuesday, 9 October 2012

rails custom errors

<% if @entry.errors.include?(:state) %>
<div class="control-group">
<div class="controls">
<div class="error"> <%= @entry.errors["state"][0] %></div>
</div>
</div>
<% end %>
###############OR
#in application rb
Include the errors in the html fields
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
"<div class=\"field_with_errors\">
#{html_tag}
<div class=\"error\">#{instance.error_message.first}</div>
</div>".html_safe
end
view raw gistfile1.erb hosted with ❤ by GitHub

Monday, 8 October 2012

Support tables for HTML5, CSS3, etc

http://caniuse.com/

ie css

background: white; /* all browsers */
*background: black; /* will show black on ie7 only */
_background: blue; /* will show blue on ie 6 only */
view raw gistfile1.css hosted with ❤ by GitHub

Friday, 5 October 2012

Fix modal for phone devices (@media (max-width: 480px))

/*MODAL FIXES*/
@media (max-width: 480px) {
.modal {
height: 500px; /* Set a default max height of the modal (adjusted later)*/
position: fixed; /* Display modal in the centre of your screen */
overflow-y: scroll; /* Ensure that the modal is scroll-able */
-webkit-overflow-scrolling: touch; /* Avoid having to use 2 finger scroll on iOS */
}
.modal.fade.in{
top: 5px;
}
.modal-body{
max-height: 2400px;
}
.modal h3 {font-size:15px}
}
@media (max-width: 480px) and (max-height: 500px){.modal{ height: 450px}}
@media (max-width: 480px) and (max-height: 450px){.modal{ height: 400px}}
@media (max-width: 480px) and (max-height: 400px){.modal{ height: 350px}}
@media (max-width: 480px) and (max-height: 350px){.modal{ height: 300px}}
@media (max-width: 480px) and (max-height: 300px){.modal{ height: 250px}}
@media (max-width: 480px) and (max-height: 250px){.modal{ height: 200px}}
@media (max-width: 480px) and (max-height: 200px){.modal{ height: 150px}}
view raw gistfile1.css hosted with ❤ by GitHub