Friday, October 22, 2010

Overriding to_s method for BigDecimal instance

by sandipransing 0 comments
requirement was to display decimal numbers which are having scale values present to be displayed in decimal format otherwise display them as integer.

Output expected
12.23 => 12.23
12.00 => 12


While rendering any object on html page by default "to_s" method gets executed. So, i overwrote "to_s" method of BigDecimal class as below.

Anyone having better solution. Please reply with your solutions. Many thanks!
Put below code in file "config/intializers/core_extensions.rb"

class BigDecimal alias :old_s :to_s def to_s return to_i.to_s if eql? to_i self.old_s end end
Read More…

Sunday, October 17, 2010

Twinkle + FF Setup (Click To Call) using telify

by sandipransing 0 comments
On linux machine following settings needs to be done in order to get in browser calling enabled for phone numbers. Installation setup
1. Install twinkle setup (Create user profile) and get it working for outgoing and incoming calls
2. Install telify add-on (can be un-installed once completed with all settings)
3. Copy wrapper script twinkle_tel) click here to download and copy it to "/usr/bin" on user's machine (make sure it has executable permissions)
4. In Firefox go to "preferences/applications"
5. Search for "tel" protocol and change value of tel protocol to "/usr/bin/twinkle_tel"
Usage/ Pre-requisites
HTML source code of phone number should like as below

1. Dialing source code
<a title="phone number" class="telified" nr="9860648108" href="tel:9860648108">9860648108</a>
2. Call disconnect source code
<a title="disconnect call" class="telified" href="tel:disconnect">Disconnect</a>

Read More…

Wednesday, October 6, 2010

DelayedJOb's (DJ) fancy methods

by sandipransing 0 comments
Delayed Job provides send_later and send_at as instance as well as class_methods methods along-with handle_asynchronously as class method to be written inside class

module Delayed module MessageSending def send_later(method, *args) Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sy m, args) end def send_at(time, method, *args) Delayed::Job.enqueue(Delayed::PerformableMethod.new(self, method.to_sy m, args), 0, time) end module ClassMethods def handle_asynchronously(method) aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 with_method, without_method = "#{aliased_method}_with_send_later#{pu nctuation}", "#{aliased_method}_without_send_later#{punctuation}" define_method(with_method) do |*args| send_later(without_method, *args) end alias_method_chain method, :send_later end end end end
Usage of send_later, send_at and handle_asynchronously
# instance method user.send_later(:deliver_welcome) # class_method Notifier.send_later(:deliver_welcome, user) Notifier.send_at(15.minutes.from_now, :deliver_welcome, user) # Inside User class write below line after deliver_welcome method handle_asynchronously :deliver_welcome
Read More…

Tuesday, October 5, 2010

Solution to DelayedJob(DJ) gem server start problem

by sandipransing 0 comments
Solution to DelayedJob(DJ) gem server start problem
I had installed delayed_job gem 2.0.3, daemons gem but after staring DJ server it shows daemon started but actually process gets killed automatically.
I performed steps given by Kevin on google group and it worked like charm
Here are the steps:

1) sudo gem sources -a http://gems.github.com
2) sudo gem install alexvollmer-daemon-spawn
3) Move the old daemons delayed job script out of the way -> mv script/delayed_job script/delayed_job.daemons
4) Make this your new script/delayed_job: http://gist.github.com/104314

Try it out again making sure it writes to the tmp/pids directory ok.
My line looks like this:
RAILS_ENV=production script/delayed_job start
then to check (besides running 'ps'), you can run this:
RAILS_ENV=production script/delayed_job status
Read More…

Monday, October 4, 2010

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter