Keypress vs keydown in jquery

In todays web world, forms and input bars are very important. We all use searching and filtering in our applications. The easiest and obvious way to do this while using JS(jquery) is binding key events to the search bar.

The event I usually bound was ‘keypress’. This is fine and works good. But the actual meaning of keypress , in its original form in IE world (!!!!!) is the event which actually adds some value in the input field. Some keys like backspace, delete, clt etc do not add any value and hence are not detected by keypress event.

These are important keys as often in filtering when the input field is empty, we might want to show all results. The user might type some query and delete it and hence detection of such keys is important.

To avoid such confusion, one should always use keydown or keyup events. These events detect events where any key is pressed and one can use e.which to determine what key is pressed and perform action.

 

I personally like keyup becuase keydown event is called just after pressing the key and hence value of input field lags one character behind the actual input.

 

Hope it helps !

Install apache web server and passenger on Ubuntu 11.04(Natty)

I just Installed apache and passenger on Ubuntu 11.04 to run and deploy my ruby on rails applications Passenger is a gem and can work with apache as well as nginx…

The reason for choosing apache is that its an industry standard.

Also one thing I did with apache was created virtual hosts and ran my apps on local machine with domains like http://www.application1.com

In this post I will list the steps I followed in order to setup apache web server and passenger.

Here is my stack:

  •  Ubuntu 11.04
  •  ruby 1.9.2 via rvm
  •  rubygems 1.8.10

I will be using rails 3.1.10(lastest this morning).!!

So lets start !!

  • Install rvm

To install rvm just type the following in the terminal(ctrl+alt+t)

 user$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

You can find more information about rvm on Here

  • Install ruby via rvm

Once rvm is installed you can install the latest or desired version of ruby by typing the following in terminal

rvm install 1.9.2

This will install ruby 1.9.2 on your system.

More information on how to make it your default ruby is available on here

  • Install rails.

just need to do

gem install rails

  • Install passenger

just do

gem install passenger

  • Install apache web server.

To install apache web server type:

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
  • Install passenger apache module.

Once this is done, we must install passenger , an apache module thats hepls us to rub rails apps on apache.

sudo passenger-install-apache2-module
  • Configure.

Finally everything is done.

When you run the passenger apache module, there would be some instructions that installer will give you.

The last thing it will tell is to paste some text in apache configuration file

The configuration file is located at /etc/apache2/apache2.conf

Now once this is done, we are ready to deploy.

This has a lot of information about deploying.

In my next post, I will show how to start rails apps on local machine with apache.

Hope it helps.!

Paperclip Resizing options…

Paperclip allows us to specify image dimensions in the  model iteself. This makes life very simple in cases where we only want image resizing.

Here is a list of things we can do to get the best out of it….

Please note : it applies to image magick also.

The basic specification is <width>x<height> in pixels, optionally followed by a modifier. In some cases you can omit either width or height.

    • 256×256
      This specifies the Maximum dimensions for the image, while preserving the aspect ratio of the image. So if your image were actually 512×256 it would be resized to 256×128 in order to fit inside the specified size.
    • 256×256!
      This specifies the Exact image size, so a 512×256 image would be changed into 256×256, losing the aspect ratio.
    • 256x
      This specifies the desired width of the target image, leaving the height to be set so as to preserve the aspect ratio.

 

    • x256
      This specifies the desired height of the target image, while preserving the aspect ratio.
    • 256×256^
      This specifies the Minimum size for the target image, so the resized image may be larger than these dimensions.
    • 256×256>
      This specifies that the image will be resized only if it is Larger than the dimensions.
    • 256×256<
      This specifies that the image will be resized only if it is Smaller than the dimensions.

Deploying with Pasenger and apache

Recently had come accross the task of deploying a rails application with apache and passenger.

I did everything as prescribed in the way I used to but for some reasons I was shown the directory listing of the application instead of application being executed.

The change I had taken from my conventional way of giving the paths to using symbolic links.

Finally i figured out the solution as an issue with passenger and the way it treats symlinks:

 

here is what you need to put between the <directory> tags to get stuff working:

 

PassengerResolveSymlinksInDocumentRoot on

 

 

Hope it helps !

Why not to use Strings in Cucumber

Cucumber allows you to define step definitions using strings instead of regular expressions.

This might seem simpler at first, but it has other problems, which I’ll illustrate with an example.

Here is a step definition that uses a plain string.

Given “I have 100 in my Account” do

end

We couldn’t write $100 here, because the $ has a special meaning when you define step definitions with strings.

Any $ signs—including any letter or number following it—will be interpreted as an argument, .

This step definition uses an argument:

Given “I have $amount in my Account” do |amount|

 

end

This step definition would match both of the following Gherkin steps:

Given I have 100 in my Account

Given I have $100 in my Account

 

In the first case, the Step Definition’s amount argument would have the value “100”. In the second case it would have the value “$100”. If our Step Definition expects a string with only digits, this can be problematic. We have no way to enforce a consistent way to write Gherkin steps, and the step definitions have to anticipate many kinds of input.

 

This is why using strings instead of regular expressions is not as advantageous as you might think. They give far less control over what gets matched and what arguments a step definition can receive.

Segmentation Fault while doing bundle install

I recently encountered this error of segmentation fault while doing bundle install in one of my rails application.

After a bit of googling I found out its an issue with ruby 1.9.3.

Here is the solution:

 

rvm pkg install iconv
 rvm pkg install openssl
 rvm reinstall 1.9.3 --with-openssl-dir=~/.rvm/usr --with-iconv-dir=~/.rvm/usr

 

Resizing the image without chaning its dimensions using paperclip

Hi,

In a recent application, the need of the hour was to reduce the file size of an image without changing its dimensions.

Now, this can be acheived by reducing the quality of the image.

ImageMagick library has a “convvert” command which does exactly the same..

for eg(on the terrminal) : convert image1.jpg -quality 10 image2.jpg

image1.jpg—>original image

image2.jpg—->final image

 

According to the paperclip wiki there is a :quality parameter which is allowed.

I tried that but for some reasons , I was not able to get it running.

I also tried using :convert_options but it did not work too.

 

Thus , I wrote my own processor and got the stuff working. Here is the model and processor code :

#######Model code###########

class Passet < ActiveRecord::Base
attr_accessible :caption, :markup, :media_passet, :pcontent_id
has_attached_file :media_passet,
:styles => {
: original => {
:geometry => “256×256<“,
:processors => [:qresize]
}
}
belongs_to :pcontent
end

 

###################Processor code########################

make a file called qresize.rb in #{Rails.root}/lib/papaerclip_processors

The path is mentioned because this path is loaded automatically….

module Paperclip
# handles compression of image by reducing its quality
class Qresize < Processor
def initialize file, options = {}, attachment = nil
super
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end
def make
src = @file
dst = Tempfile.new([@basename, @format])
dst.binmode
begin
parameters = []
parameters << “:source”
parameters << “-quality 10”
parameters << “:dest”
parameters = parameters.flatten.compact.join(” “).strip.squeeze(” “)
success = Paperclip.run(“convert”, parameters, :source => “#{File.expand_path(src.path)}[0]”, :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError => e
raise PaperclipError, “There was an error during the Size Reduction  for #{@basename}” if @whiny
end
dst
end
end
end

 

This is a general code for image resizing and probably at this stage, we may not require to write a processor.

But the advantage of doing this is that we can extend this to do a lot more as and when requirement comes.

Also, we can handle our video and audio processing by this easily.

The other option is to pass a block(lambda or Proc) to :styles hash but I personally feel this is a much cleaner approach.

 

Hope it helps.