by sandipransing
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…
by sandipransing
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…
by sandipransing
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…