by sandipransing
Methods defined in Helper modules can directly accessed in rails views because this is what they are pretended for but we often come across with situations where we wanted to use some helper methods in controllers, views, models and mailers and obvious we don't want to repeat same lines of code everywhere which also rail does not permit. forgotten DRY? Oh then how to do achieve same without violating rails principle.
Certainly there are ways to do this ..
1. Helper methods all the time for views
class ApplicationController < ActionController::Base
helper :all# include all helpers, all the time for views
end
2. Controller methods in views
class ApplicationController < ActionController::Base
helper_method :current_store
#now controller_method can be accessed in views
end
2. Helper methods in controller
class ApplicationController < ActionController::Base
include ActionView::Helpers::ApplicationHelper
end
3. Helper methods in model
class Student < ActiveRecord::Base
include ActionView::Helpers::ApplicationHelper
end
4. Helper methods in mailer
class Notifier < ActionMailer::Base
add_template_helper(ApplicationHelper)
#...
end
Read More…
by sandipransing
To Add custom conditions to authlogic finders first of all we need to override authlogic find_by_login method.
class UserSession < Authlogic::Session::Base
after_validation :check_if_verified
find_by_login_method :find_by_login_and_deleted_method
end
Then we need to define overridden method inside User model
class User < ActiveRecord::Base
acts_as_authentic
def self.find_by_login_and_deleted_method(login)
find_by_email_and_deleted(login, false)
end
end
got easy..wooooooo :)
Read More…
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…