Friday, July 23, 2010

Few useful links for pdf generation in rails

http://www.accesspdf.com/pdftk/#packages

http://wiki.github.com/sandal/prawn/using-prawn-in-rails
http://railscasts.com/episodes/153-pdfs-with-prawn
http://wilsoncolab.com/blog/bryan/2008/11/30/writing-pdfs-with-ruby-on-rails-using-prawn-and-prawnto/
http://prawn.majesticseacreature.com/
http://www.cracklabs.com/prawnto/use

http://rdoc.info/rdoc/sandal/prawn-layout/blob/bcfac3efffb68a0b3c9f1007c1c1193443470afc/Prawn/Document

Thursday, July 15, 2010

Modifications in rails auto_complete plugin to pass id as param.

The auto_complete plugin explain in railscasts passes the selected name as param.
Rails Autocomplete
In most cases we want to pass the select value id instead of name.
To pass the id instead of name in the above link all we need to do is


<!-- categories/index.js.erb -->
<%= auto_complete_values @categories, :name, :id %>
<%= text_field_with_auto_complete :product, :category_name, { :size => 15 }, { :url => categories_path(:js), :method => :get, :param_name => 'search', :after_update_element => "function(text_field, li_element){$('product_category_id').value = $('text_' + li_element.id).value;}"}) %>
<%= hidden_field "product", "category_id" %>
 
<!-- application_helper -->
  def auto_complete_values(entries, text_field, value_field, phrase = nil)
    return unless entries
    items = entries.map do |entry|
      content_tag("li", phrase ? highlight(entry[text_field], phrase) : h(entry[text_field]), {:id => entry[value_field]}) +
      hidden_field_tag("text_#{entry[value_field]}", entry[value_field])
    end
    content_tag("ul", items.uniq, {:style=> "height: 150px; overflow:auto;"})
  end

Note: In case you want to trigger an event on select of autocomplete list then all you want to do is modify after_update_element
Ex: <%= text_field_with_auto_complete :company, :number, {:class => "title"}, {:url => autocomplete_company_numbers_path(:js), :method => :get, :param_name => 'search', :after_update_element => "function(text_field, li_element){window.location.href='/company__numbers/show/' + $('text_' + li_element.id).value;}"} %>

Tuesday, July 13, 2010

Using Clockwork and Delayed Job gem to send event reminder emails.

code below run a background process to send an event reminder emails.
Install clockwork and delayed_job gem. You might want to upgrade ruby gem version too
commands:
gem install clockwork
gem install delayed_job

Create Reminder model with event_id, reminder_at fields

class CreateReminders < ActiveRecord::Migration
  def self.up
    create_table :reminders do |t|
      t.integer :event_id
      t.datetime :reminder_at
      t.timestamps

    end
  end

  def self.down
    drop_table :reminders
  end
end


Add the association into Reminder model
belongs_to :event

Now create clock.rb in the lib folder of your application and add the following.

  require 'rubygems'
require 'clockwork'
include Clockwork

require 'config/boot'
require 'config/environment'
every(2.minutes, 'reminder.deliver') {
reminders = Reminder.find(:all, :conditions => ["reminder_at <= ? and reminder_at > ?", Time.now.advance(:minutes => 2), Time.now])
#reminders = Reminder.find(:all, :conditions => ["reminder_at <= ?", #Time.now.advance(:minutes => 2)])
unless reminders.nil?
UserMailer.send_later( :deliver_event_reminder, reminders )
end


Now using command prompt go to the root directory of the application and run the command
clockwork lib/clock.rb
If you are using delayed_job then you will have to  rake jobs:work in another command prompt
This will trigger an event every 2 minutes which will get all the reminders that needs to be send in the next two minutes and delivers it.

How to make your pc run as fast a new

STEP 1

For those who believe their computer lags at start up, a quick look at the MSConfig would be the place to start. MSConfig can be accessed by hitting the Windows button on keyboard along with R key.

This will bring up the 'Run' dialog. Type 'MSConfig' and you will get the 'Start Up' tab with a list of programs and executables that are launched when the computer starts up.

Starting up of too many programs when the computer boots could be a reason why the boot time is so long. The remedy would be to go through the entire list and uncheck the unnecessary items.

If there are entries you are not sure about, check them on the internet, as unchecking the wrong entries could potentially mess up the functionality of some of your programs.

STEP 2

Defragment the hard drive once a month. It's like changing your car's oil - it should be the one thing on your list even if you don't do anything else. Windows stores information about your programs in the Registry.

This can get cluttered and affect performance. The solution is to download free registry cleaners from the internet (eg CCleaner) to remove redundant program entries.

STEP 3

Indexing Services is a nifty little program that uses a large amount of RAM. This processes indexes and updates lists of files on your computer. This is done so that when you search for something, it can search faster by scanning the index lists.

So, if you don't search your computer often, turn it off would better performance. Simply go to 'Start' button on taskbar, click on 'Control Panel' and select 'Add/Remove Programs'. Find the tab 'Add/Remove Window Components' and uncheck 'Indexing Services'. Click Next.

Also, if there is a delay every time you open 'My Computer' to browse folders, try this.

Open 'My Computer', click on 'Tools', select 'Folder Options', click on the 'View' tab to uncheck the 'Automatically search for network folders and printers' box.

Click 'Apply' and then reboot your computer for changes to apply.

STEP 4

Hard drive performance plummets as you near the drive's maximum capacity.

Assuming you don't have an additional drive to move the content to, your choices are slim. But before you take a machete to your files, you may want to compress them.

Use the built-in compression tool in Windows, which makes accessing the files no different from it currently is. Go to 'Disk Cleanup' and make sure 'Compress Old Files' is checked.

Click 'Options' and specify the age of the files you want Windows to compress - Windows will compress only the files you haven't accessed in more than six months (or as specified).

STEP 5

An important feature of Windows is its ability to return system files to the state they were in earlier. Before tweaking, users can create a new 'Restore Point'.

Go to Control Panel>Performance and Maintenance>System Restore or Start-All Programs > Accessories > System Tools>System Restore and select 'Create a restore point'.

If it is left alone, this can consume a good portion of your disk space with unnecessary restore points.

By default, it uses up to 12 per cent of each of your drives and, even if you have a big hard drive, many extra restore points can slow down processes like virus checking, disk-defragmenting, etc.

Typically, how much space you should allot to system restore depends on your system (200 MB - the minimum allowed - will hold 5 or 6 restore points) and should suffice for average users.

STEP 6

Also, the Windows XP (and Windows Vista) computers have features like smooth animated menus and transparent windows.

These use processor resources, too, which means they can cause slowdown. Right-click the 'My Computer' icon and click 'Properties'.

Click the 'Advanced' tab and under 'Performance', click 'Settings'. Choose to switch off some of these effects and see the improvement in your PC's performance.

STEP 7

Accumulated dust in computer cases a reason for over-heated PCs. Dust gets into the ball bearings in the fan and cause the fan to stop working. This may lead to overheating of processor and permanent hardware failure.

It is less common, but static charges from dust can also be a threat. A layer of dust on a memory chip can cause static electricity to build up.

This can amount to electric charge, which discharges on to your motherboard or memory chip. The best way to stop dust from building up is to open the case on your PC and vacuum it.

We recommend never tweak without knowing how to return where you began

Wednesday, July 7, 2010