NinjaCipher:-*-:ro60

Archive for the ‘ruby’ Category

Python vs Ruby on Google Blogs

Thursday, February 28th, 2008

Here is a graph I generated from trendrr.com to show how many search results google blogs returns for ruby vs python. It makes me happy on the inside to see them so neck and neck.

autoCreated ajax ext dialog

Sunday, August 12th, 2007

Here is a tidbit that will autoCreate an ExtJs BasicDialog populated by a partial returned from an AJAX call to a rails controller action.

In your js

function loadDialog(){
    var cb = {
      success: showDialog,
      failure: displayError
    };
    Ext.lib.Ajax.formRequest( 'merge_form', '/controller/get_dialog' , cb , null, false, null);
}
function showDialog(response){
   var dlg = new Ext.BasicDialog("example_dialog",{
		        height: 200,
		        width: 300,
		        resizeable: false,
			autoCreate: true,
		        modal: true,
		        draggable:false,
			collapsible:false,
		        shadow: true,
			title:'Hola'
		    });

		    dlg.addKeyListener(27, dlg.hide, dlg);
		    dlg.addButton('Yes', dlg.hide, dlg);
		    dlg.addButton('No', dlg.hide, dlg);
		    dlg.body.dom.innerHTML = response.responseText;
		    dlg.show();
		}
function displayError(response){
   if(response.responseText == null){
	 Ext.MessageBox.alert("Woops!","The requested operation failed.");
   }else{
	Ext.MessageBox.alert("Woops!",response.responseText);
   }
}

In your controller

  def get_dialog
    @msg = "hello world"
    render :partial=>"/dialogs/content"
  end

In your partial

  <%= @msg %>

In your view

<input type="button" onClick="loadDialog();" value="show dialog" />

3 helpfull functions

Saturday, August 11th, 2007

These are a few functions that I use in every rails project I do so I figured I would pass them along. Perhaps someone will find them useful.

This first one is a function to get a page number for pagination based on start index and result set size.

  def get_page(_start,_limit)
    start = (_start || 1).to_i
    size = (_limit || 25).to_i
    return ((start/size).to_i)+1
  end

This function returns an age based on birth date thats pulled from a DateTime db col.

  def age(birthdate)
    unless birthdate.nil?
      age = Date.today.year - birthdate.year
      if Date.today.month < birthdate.month ||
      (Date.today.month == birthdate.month && birthdate.day >= Date.today.day)
        age = age - 1
      end
      return age
    else
      return 0
    end
  end

This function takes a string and a length and truncates it while also appending … to the end.

  def ellipses(str,length)
    if str.nil?
      return
    else
      return (str.length > length) ? "#{str[0,length]}…” : str
    end
  end

merging model validation errors

Monday, May 14th, 2007

Here is a simple chunk of code that I find very useful when working with multiple objects in one form that leverage the Rails model validation functions.

In this example we see how to merge validation errors for more then one object so they can be displayed with the error_messages_for helper.

if @user.update_attributes(params[:user])
   flash[:notice] = “Account Updated”
else
   flash[:notice] = “Account Updated Failed”
   @user.profile.errors.each do |attribute, error|
      @user.errors.add(attribute, error)
   end
end