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"})})

Wednesday, July 08, 2009

authenticity_token not available in Rails 2.X version for non forms

This problem occur  if view is not having any form ...
When a form is being generated rails automatically adds something like
this:
< input name="authenticity_token" type="hidden" value="11ff3908e6cd4be7b4041a93b783829ce6b12349" >  
If view does not contain form ("authenticity_token")  InvalidAuthenticityToken is being raised.
 
So You can do something like this in your view to make your authenticity 
token available to your javascript in your views.

<%= javascript_tag "window._token = '#{form_authenticity_token}'" %>

That will make your authenticity token available to your custom 
javascript Ajax requests.  If you're using prototype.js and you want to 
do a custom PUT, you do something like this.

  new Ajax.Request ('/products/1', {
    method: 'put',
    parameters: 'product[name]=chair&authenticity_token=' + 
window._token});
 
If you use Jquery :=
$.ajax({
              url: "people/1,
              global: false,
              type: "PUT",
              data: ({'person[name]' : 'hey',authenticity_token : window._token}),
              dataType: "html",
              success: function(msg){
                 //ur msg;
              }});

Thursday, June 11, 2009

MS Word processing or parsing using ruby and Apache POI

I am looking for MS word document processing to get text information using ruby. But could not find right gems in ruby. Some win32 ole libraries are there but will not work in linux. So thought about using Poi java api to process word document.

I hv downloded poi jar files from here.
http://www.apache.org/dyn/closer.cgi/poi/release/

changed the name of the directory to poi folder .

Check the following ruby script to get the parsed text .. it uses java interface code in WordSampleReader.java   this file should be compiled and should be available under poi folder where all jar files are there.

CONFIG = {}
class WordReader
  #include Config
  CONFIG['host'] = 'mswin32'
  def self.generate_text(filename)
   
    interface_classpath=Dir.getwd+"/poi"
  
    case CONFIG['host']
      when /mswin32/
        Dir.foreach("poi") do |file|
          interface_classpath << ";#{Dir.getwd}/poi/"+file if (file != '.' and file != '..' and file.match(/.jar/))
        end
        path = "java -cp \"#{interface_classpath}\" WordSampleReader "+filename
      else
        Dir.foreach(Dir.getwd+"/poi/") do |file|
          interface_classpath << ":#{Dir.getwd}/poi/"+file if (file != '.' and file != '..' and file.match(/.jar/)) 
        end
        path = "java -cp \"#{interface_classpath}\" WordSampleReader "+filename
      end
    result = ""
    IO.popen(path, "w+b" ) { |x| result= x.read }
    result
  end
end

puts reader = WordReader.generate_text('poi/test.doc')


Though i dont know java used sample code found in google..  The file called WordSampleReader.java.

The source code file is ...

//package com.informit.poi;

// Import POI classes
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.extractor.*;

// Import Java classes
import java.io.*;
import java.util.*;

public class WordSampleReader
{
  public static void main( String[] args )
  {
   if( args.length == 0 )
   {
     System.out.println( "Usage: WordSampleReader " );
     System.exit( 0 );
   }

   String filename = args[ 0 ];
   try
   {
     // Create a POI File System object; this is the main class for the POIFS file system
     // and it manages the entire lifecycle of the file system
     POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream( filename ) );

     // Create a document for this file
     HWPFDocument doc = new HWPFDocument( fs );

     // Create a WordExtractor to read the text of the word document
     WordExtractor we = new WordExtractor( doc );
    
     // Extract all paragraphs in the document as strings
     String[] paragraphs = we.getParagraphText();

     // Output the document
     //System.out.println( "Word Document has " + paragraphs.length + " paragraphs" );
    // for( int i=0; i
     //{
      //System.out.println( paragraphs[ i ] );
     //}
    //  output text
      System.out.println( we.getText() );

   }
   catch( Exception e )
   {
     e.printStackTrace();
   }
  }
}

Monday, May 11, 2009

QuickBase autheticate ruby method bug in quickbase client library

For subsequent calls with the help of ruby quickbase api , it should consider the old token number.

Existing method :
def authenticate( username, password, hours = nil )

      @username, @password, @hours = username, password, hours

      if username and password

         @ticket = nil
         xmlRequestData = toXML( :hours, @hours ) if @hours
         sendRequest( :authenticate )
         @userid = getResponseValue( :userid )
         return self if @chainAPIcalls
         return @ticket, @userid

      elsif username or password
         raise "authenticate: missing username or password"
      elsif @ticket
         raise "authenticate: #{username} is already authenticated"
      end
   end

The above one missing token variable and which is very important for next or immediate calls.
so add this line in the above method.
@ticket = getResponseValue( :ticket )

Saturday, April 11, 2009

Import Gmail Contacts - Ruby on Rails

Tried with my own ruby script to get contact information from google but requires some more extra work to make request in secure way.

First, you need to register your domain with Google. This can be as easy as uploading a temporary file to your domain’s root directory to verify control of the domain. The steps for doing this are clearly outlined at Registration for Web-Based Applications.  

I find good  article to import google contact information. Use the same code but check the code for error free.. you should have same patience to make it work :-)

http://blog.guitarati.com/2008/08/google-accounts-authentication-using.html