Moving to anywhere on a line in terminal on Mac OSX
If your like me and use terminal on you mac you might find yourself wanting to move to a point on the line you just typed. Well there is a simple way that l know at least. Just press and hold the “Option” key and click your mouse anywhere on the line and your cursor will move there.
I know there might be a better way without going back to the evil mouse but, l just don’t know it.
Rendering google style pie chart in your rails app
Google has some of the best charts on the web. If you ever wanted to have cool pie charts like google here is a simple way to do it. Simply add the following to your application_helper.rb file.
def google_pie_chart(data, options = {})
options[:width] ||= 250
options[:height] ||= 100
options[:colors] = %w(0DB2AC F5DD7E FC8D4D FC694D FABA32 704948 968144 C08FBC ADD97E)
dt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."
options[:divisor] ||= 1
while (data.map { |k,v| v }.max / options[:divisor] >= 4096) do
options[:divisor] *= 10
end
opts = {
:cht => "p",
:chd => "e:#{data.map{|k,v|v=v/options[:divisor];dt[v/64..v/64]+dt[v%64..v%64]}}",
:chl => "#{data.map { |k,v| CGI::escape(k + " (#{v})")}.join('|')}",
:chs => "#{options[:width]}x#{options[:height]}",
:chco => options[:colors].slice(0, data.length).join(',')
}
image_tag("http://chart.apis.google.com/chart?#{opts.map{|k,v|"#{k}=#{v}"}.join('&')}")
rescue
end
Once you have that in then in your view its as simple as below to render a nice pie chart. Go ahead have a piece.
<%= google_pie_chart([["Rails",80],[".Net",10],["Java",10]) %>
Fixing the enter key in ASP.NET with jQuery
One of the fundamental problems with ASP.NET WebForms is the fact that you can only have one form per page. This jumps up and bites you when you have a set of fields and a button and you want the enter key to submit the form.
In ASP.NET 2.0 they came out with a “fix” for this. You can wrap your fields in a Panel and set the default button:
<asp:Panel ID="_pnlLogin" DefaultButton="_btnLogin" runat="server">
UserName: <asp:TextBox ID="_txtUserName" runat="server" />
Password: <asp:TextBox ID="_txtPassword" runat="server" />
<asp:LinkButton ID="_btnLogin" Text="Login" runat="server"/>
</asp:Panel>
Some of you will know limitation here. This works with a Button, but not a LinkButton. Let’s see why.
Here is the resulting HTML:
<div id="_pnlLogin" onkeypress="javascript:return WebForm_FireDefaultButton(event, &#x27;_btnLogin&#x27;)">
UserName: <input name="_txtUsername" type="text" id="_txtUsername" />
Password: <input name="ctl00$mainContent$_txtPassword" type="text" id="_txtPassword" />
<a id="_btnLogin" href="javascript:__doPostBack('_btnLogin','')">Login</a>
</div>
The Panel renders as a div, and on any key pressed will call WebForm_FireDefaultButton:
function WebForm_FireDefaultButton(event, target) {
if (!__defaultFired && event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
var defaultButton;
if (__nonMSDOMBrowser) {
defaultButton = document.getElementById(target);
} else {
defaultButton = document.all[target];
}
if (defaultButton && typeof(defaultButton.click) != "undefined") {
__defaultFired = true;
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
This method checks for the enter key and then calls click on the default button. All good except for one thing: the LinkButton renders as an “a” tag. Anchor tags don’t have a click() method unfortunately. At least not in Firefox.
So let’s fix it! I’ve recently been reading “Learning jQuery”
which is a great book on this cool javascript library. I’ll use jQuery instead of straight javascript as it is much easier to write cross browser code and it’s a lot more concise. Plus there are heaps of plugins for it, and it’s a pretty small download for your page.
I want to improve the markup of my page at the same time so I’ll try to minimise the amount of server controls I use and get rid of that inline javascript call. My new aspx looks like this:
<div class="form">
UserName: <asp:TextBox ID="_txtUsername" runat="server" />
Password: <asp:TextBox ID="_txtPassword" runat="server" />
<asp:LinkButton ID="_btnLogin" Text="Login" runat="server" CssClass="form_submit"/>
</div>
So if I have a div with the class “form”, the default submit button is the first element with the “form_submit” class. Nice, simple and unobtrusive.
Here’s the javascript I ended up with:
$(document).ready(function(){
var $btn = $('.form_submit');
var $form = $btn.parents('.form');
$form.keypress(function(e){
if (e.which == 13 && e.target.type != 'textarea') {
if ($btn[0].type == 'submit')
$btn[0].click();
else
eval($btn[0].href);
return false;
}
});
});
Basically it’s going to find any .form_submit elements, find a parent form, and add a keypress handler for the form. When the keypress is triggered, if it’s the enter key and we’re not in a textarea, we’ll click the default submit button if it IS a button, if it’s a link we’ll Eval the href attribute, which is going to be our javascript postback call.
Cool, I can keep it tucked away in an js file include, and i’ve improved my markup.
Setting up gravatar’s in your rails app
Gravatars allow users to configure an avatar to go with a users email address at a central location: http://gravatar.com. Gravatar-aware websites can then look up and display each user’s preferred avatar, without having to handle avatar management. This makes life easy on the developer and the user only has to manage one central location.
First step is to get the plugin
ruby script/plugin install git://github.com/woods/gravatar-plugin.git
Now most user models have an email field so it’s usually something like below to access their email.
user.email
If thats the case then to have the gravatar appear in your views it’s simply
<%= gravatar_for @user %>
Thanks Scott A. Woods for a great and simple plugin.
Timezone support in Rails 2.1+
Setting up Timezone support in the past was a pain, but now with rails 2.1 and above it’s easy and done in minutes. Here is what you need to do
# config/environment.rb config.time_zone = "Pacific Time (US &amp; Canada)" # controllers/application.rb before_filter :set_user_time_zone private def set_user_time_zone Time.zone = current_user.time_zone if logged_in? end # Add to field to user model add_column :users, :time_zone, :string
Once you have this in place then to user the users timezone in your app its just something like this
# To use users timezone rather then server time current_user.time_zone.today




