Sunday, November 28, 2010

authlogic custom conditions for authentication

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

Thursday, November 18, 2010

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

Wednesday, September 29, 2010

Best ways to populate dynamic array of numbers in ruby

by sandipransing 0 comments
Array of years using range
((yr=Date.current.year)-9..yr).to_a #=> [2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010]
Array of years using lambda
Array.new(10){|i| Date.current.year-i} #=> [2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001]
Array of months
Date::MONTHNAMES.compact #=> ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
Array of abbreviated months
Date::ABBR_MONTHNAMES.compact #=> ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
Array of abbreviated months with index (ps. collect_with_index is core extension method added to array)
Date::ABBR_MONTHNAMES.compact.collect_with_index{|m, i| [m, i]} #=> [["Jan", 1], ["Feb", 2], ["Mar", 3], ["Apr", 4], ["May", 5], ["Jun", 6], ["Jul", 7], ["Aug", 8], ["Sep", 9], ["Oct", 10], ["Nov", 11], ["Dec", 12]] OR Date::ABBR_MONTHNAMES.compact.each_with_index.collect{|m, i| [m, i+1]} #=> [["Jan", 1], ["Feb", 2], ["Mar", 3], ["Apr", 4], ["May", 5], ["Jun", 6], ["Jul", 7], ["Aug", 8], ["Sep", 9], ["Oct", 10], ["Nov", 11], ["Dec", 12]]
Read More…

Thursday, September 9, 2010

Rails 3 Beautiful Code

by sandipransing 0 comments
Check out this SlideShare Presentation:
Read More…

Saturday, August 21, 2010

Zipcode validation using geokit in rails

by sandipransing 0 comments
1.Install geokit gem
gem install geokit OR # Add following line inside rails initialize block Rails::Initializer.run do |config| config.gem 'geokit' end
And then run command
rake gems:install 2. Consider User model with zipcode as attribute field
include Geokit::Geocoders class User < ActiveRecord::Base set_table_name :users validate_presence_of :zipcode validate :request_zipcode_validation_using_geokit, :if => :zipcode private def request_zipcode_validation_using_geokit # Method request google api for location # if location found then zipcode is valid otherwise # add validation error on zipcode field # as it method contacts with google api and takes time # to return result, poll request only when zipcode gets # changed poll = true # default true for new objects if self.id ## this means already existing user and zipcode is valid last time # Hack to find where zipcode got modified or not # old_user = User.find self.id poll = false if old_user.zipcode == self.zipcode end # Actual requesting api to return location associated with zipcode if poll loc = MultiGeocoder.geocode(self.zip_code) end # Add Validation Error if location is not found errors.add(:zip_code, "Unable to geocode your location from zipcode entered.") unless loc.success end
Please note that same method can also be used to validate state, city and country.
Again we can use combination of fields to validate each other.
Like -
1. Based on country entered, state validation
2. Based on state, city validation
3. Based on city, zipcode validation
or
4. Based on zipcode and country, state and city validation

Here is another method to validate state and city based on zipcode and country.
Lets take example of 'US'
def request_state_and_city_validation_based_on_zipcode poll = true # default true for new objects if self.id ## this means already existing user and all attributes were valid last time # Hack to find any one of location attribute got modified # old_user = User.find self.id loc_attrs = %w{zipcode state city} # keep in mind country US is default assumed if loc_attrs.all? {|attr| self.attribute_for_inspect(attr) == old_user.attribute_for_inspect(attr)} self.poll = false end end # Actual requesting api to return location associated with zipcode if poll loc = MultiGeocoder.geocode("#{self.zip_code}, US") end # Add Validation Error if location is not found unless loc.success errors.add(:zip_code, "Unable to geocode your location from zipcode entered.") else # Validate state and city fields in compare to loc object returned by geocode errors.add(:state, "State doesn't matches with zipcode entered") if self.state != loc.state errors.add(:city, "City doesn't matches with zipcode entered") if self.city != loc.city end end
Note***
If you are subscriber of blog and not displaying post correctly. I request you to visit post on blog itself. Somehow style is not getting correctly in email. I will try to fix this problem asap.

Upcoming Posts
1. Geokit finders: Find locations in/within/beyond particular radius from specified location using acts_as_mappable plugin
2. Customizing authlogic for multiple sessions i.e. using different models for role based authentication.
Read More…

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