Monday, January 31, 2011

Restful routes for non ID( primary key ) rails 3

Lets suppose that you have a Profile model, and  you have a resources :profiles route.

Generally, profile_path will build a path with the profile object’s ‘id’ in it:
profile = profile.find_by_name('jaganreddy')
  profile_path(profile)  # => "/profiles/1"
You can override to_param in your model to make profile_path build a path using the profile’s name instead of the profile’s id:
class Profile < ActiveRecord::Base
    def to_param # overridding default behaviour
       name 
    end
 end
    
  

  profile = Profile.find_by_name('jaganreddy')
  profile_path(profile)  # => "/profiles/jaganreddy" 
 
Also take care for nested resources too
In your controller always looks for  
Profile.find_by_name(params[:id])

Wednesday, January 26, 2011

how to set primary key and autoincrement on non id column using rails 3 -- sqlite

sqlite_adopter uses
       def default_primary_key_type
          if supports_autoincrement?
            'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'
          else
            'INTEGER PRIMARY KEY NOT NULL'
          end
        end
and under the create table definition the line says:
table_definition.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false

that means we should not set :id => false ... many people will do to avoid id column..  this is fine
but if you want  other column as primary key add at table definition not at column definition.(dont add :id => false)..

adding primary_key at table definition will take care of removing id column.  the following line will also add t ocolumn definition
  'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'
 ofcourse based on sqlite3 version...

      def supports_autoincrement? #:nodoc:
        sqlite_version >= '3.1.0'
      end
 Note: so autoincrement for :primary_key is hard coded
sample:
create_table :students, :primary_key => student_id do |t|
   t.string :name
end
it generates students table as follows;
  students;
                  student_id : primary key autoincrement
                 name :   varchar


This is for what i am using version of rails : 3.0.3

Monday, January 10, 2011

undefined method `user_signed_in? with Rails 3.0.3 - Devise

I just started a new rails 3.0.3 application after doing a gem update
(loaded rails 3.0.3 and devise 1.1.5) and I got the same error. This
was following the default devise install instructions.

answer:
for got to add
devise_for :users in routes.rb

Friday, November 05, 2010

Date sort in ruby from array can have date formats ,non date formats

Date sorting(ruby) from array is easy if array contain strings with proper date formats. Sometime invalid date formats raise argument error.

Sample i have used  for date column attribute as follows.
def sort_on_mature_date(listings)
    listings.sort do |x,y|
      x_matures_on = x.when_does_it_mature? #date type
      y_matures_on = y.when_does_it_mature? #date type
      begin  Date.parse(x_matures_on)   rescue  x_matures_on="1900-1-1" end    #string could be not a date like "now"
      begin Date.parse(y_matures_on) rescue y_matures_on="1900-1-1" end
      Date.parse(x_matures_on)<=>Date.parse(y_matures_on)
    end
  end

Friday, October 22, 2010

Rails rounded corner box helper

Here is the helper include in your rails app. You need to take care of images .
#usage:

 <%top = {"background-image"=>"url('/images/accept_box_top.png')",:height=>"6px",:width=>"328px"}%>


  <%mid = {"background-image"=>"url('/images/accept_box_mid.png')",:height=>"405px",:width=>"auto"}%>

 


  <%bottom = {"background-image"=>"url('/images/accept_box_bottom.png')",:height=>"9px",:width=>"auto"}%>

 


  <%content = {"padding"=>"15px 28px 22px 26px"}%>
  <%=round_box(top,mid,bottom,content){|f|  "nice box"} %>


Make sure that you pass right height and width. It automatically renders the box and content inside.
If you want you also add extra css attributes too!

#round_box_helper.rb

module RoundBoxHelper

  def round_box(top, mid,bottom,content,&b)

     css_elements( top,mid,bottom,content)+

     "<div class = 'accept_gift_box'>

      <div class='accept_gift_box_top'></div>

        <div class='accept_gift_box_mid'>

          <div
class='accept_gift_box_content'>

             
"+b.call+"

          </div>

        </div>

    <div class='accept_gift_box_bottom'> </div>

  </div>"

  end


  def css_elements top,mid,bottom,content

     "<style type='text/css'> " +

      ".accept_gift_box_top{"+

        parse_styles( top )+

      "}"+

       ".accept_gift_box_mid{"+

        parse_styles( mid )+

      "}"+

      ".accept_gift_box_bottom{"+

        parse_styles( bottom )+

      "}"+

      ".accept_gift_box_content{"+

        parse_styles( content )+

      "}"+

      ".accept_gift_box{"+

       "float:left;"+

      "}"+

     "</style>"

  end


  def parse_styles el

    str=""

    el.each_pair do |attr,value|

      str = str+attr.to_s+":"+value.to_s+";"

    end

    str

  end


end

Tuesday, September 28, 2010

width and top of invisible element in IE may give 0 zero using jQuery

jQuery element width for invisible element in IE will not give exact result.
But  in Firefox it works perfectly.

$(e).width() //using jQuery to get the width.

i have used a way to solve this one by showing, calculate width and hiding the element.

$(e).
show(). // to get the width of visible element ==> IE issue
                    css({zoom: 1,left:$(e).position().left-parseInt($(e).width()),top:$(e).position().top}).
                    hide().

jQuery animation effect slide bug in IE 7

The best example is for this ind of bug is presented by ryan as follows

http://www.ryancramer.com/misc/jquery_slide/#

the slide effects of jquery may not work 100% correctly in IE 6 & IE 7 . Effects are misbehave like effects complete till 50% or so...


So i have used show call back function to rescue the bug.. Though it is 100% not correct but once the effects renders second time it works perfectly.

So make it show and show it again..with less duration.. to avoid flickering effect.

$("my_element").show(1,function(){$(this).show("slide",{direction:"right"})})