I needed to import blogspot blogs into a different system. I created a plugin that simply parses the xml file that is generated when you export your blogspot blog. Here is the link to github.
HowTo: Import your posts from Blogspot/Blogger
Monday, March 7th, 2011HowTo: Check if email really exists
Friday, December 10th, 2010I had a project that wants to check if an email really exists. Here’s what we came up initially. We ended up not using this in production though, but it’s a great tool.
HowTo: Authorize.Net CIM and ActiveMerchant
Tuesday, December 7th, 2010I’ve started working on a Sample Rails App that uses ActiveMerchant to connect to the Authorize.Net API. Here’s the link.
HowTo: Integrate with ConstantContact
Tuesday, December 7th, 2010I’ve started working on a plugin for ConstantContact. You can find the latest development here.
I hope you find it useful in integrating your Rails Apps with ConstantContact.
Rescue :me
Friday, March 26th, 2010These rescuers are quite handy to prevent nasty errors in production.
# in application_controller.rb
rescue_from ActionController::RoutingError, :with => :route_not_found
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
rescue_from ActionController::MethodNotAllowed, :with => :invalid_method
rescue_from NoMethodError, :with => :show_error
rescue_from ActionView::TemplateError, :with => :template_not_found
private
def route_not_found
render :text => ‘Sorry, we could not find what you were looking for.’, :status => :not_found
end
etc.
HowTo: Changing the column name in a model for validation
Monday, September 21st, 2009Sample code taken from here.
class User > ActiveRecord::Base
HUMANIZED_ATTRIBUTES = {
:email => “E-mail address”
}def self.human_attribute_name(attr)
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
endend
String#present?
Friday, August 14th, 2009>> “brain”.present?
=> true
>> “”.present?
=> false
use something.present?
instead of !something.blank?
HowTo: Add Ajax Indicators
Thursday, March 12th, 2009Copy the following code to public/javascripts/application.js
Ajax.Responders.register({
onCreate: function() {
if (Ajax.activeRequestCount > 0)
Element.show('indicator');
},onComplete: function() {
if (Ajax.activeRequestCount == 0)
Element.hide('indicator');
}
});
Add a div element to your view with an id of ‘indicator’. Remember to keep this div from displaying by setting the display attribute to none.
#indicator{style => 'display:none'}
= image_tag ('/images/my_loading_indicator.gif')
Here is a good site to create your own indicator image.
HowTo: Use older gems
Wednesday, October 22nd, 2008Now that Rails 2.1.1 is out, when you install using the gem, you get Rails 2.1.1. If you want to install an older version, like Rails 2.1.0, you need to specify the version:
sudo gem install rails –version 2.1.0
You’ll do this if you have some dependency on the older rails version. To make sure your app will run when you transfer to a different machine, freeze rails into the vendor directory:
rake rails:freeze:edge TAG=rel_2-1-0
Make sure that your environment.rb specifies the corresponding version.
Finally, you may want to create a new project using an older version of rails. You can do so by:
rails _2.1.0_ project_name
And apparently, you can use that trick for other gem-installed commands, like capistrano.
HowTo: Date operations
Friday, October 10th, 2008I’m just going to copy and paste the code from this site.
Basically, time and date manipulations are easy as long as the operations involved are additions and subtractions. Quoted from the link above:
Not every arithmetic operation makes sense for dates: you could “multiply two dates” by multiplying the underlying numbers, but that would have no meaning in terms of real time, so Ruby doesn’t define those operators.
Remember:
- Time has a unit of 1 secondrequire ‘date’
y2k = Time.gm(2000, 1, 1) # => Sat Jan 01 00:00:00 UTC 2000
y2k + 1 # => Sat Jan 01 00:00:01 UTC 2000
y2k – 1 # => Fri Dec 31 23:59:59 UTC 1999
y2k + (60 * 60 * 24 * 365) # => Sun Dec 31 00:00:00 UTC 2000y2k_dt = DateTime.new(2000, 1, 1)
(y2k_dt + 1).to_s # => “2000-01-02T00:00:00Z”
(y2k_dt – 1).to_s # => “1999-12-31T00:00:00Z”
(y2k_dt + 0.5).to_s # => “2000-01-01T12:00:00Z”
(y2k_dt + 365).to_s # => “2000-12-31T00:00:00Z” - While DateTime and Date have a unit of 1 dayday_one = Time.gm(1999, 12, 31)
day_two = Time.gm(2000, 1, 1)
day_two – day_one # => 86400.0
day_one – day_two # => -86400.0day_one = DateTime.new(1999, 12, 31)
day_two = DateTime.new(2000, 1, 1)
day_two – day_one # => Rational(1, 1)
day_one – day_two # => Rational(-1, 1)
Printing dates on our views is done by using the method strftime.
d = Date.today # 2008-08-12
d.strftime(‘%b %d, %Y’) # will print August 12, 2008
The following are the format codes. For reference, refer to this site.
| Format | Meaning |
| %a | The abbreviated weekday name (“Sun’’) |
| %A | The full weekday name (“Sunday’’) |
| %b | The abbreviated month name (“Jan’’) |
| %B | The full month name (“January’’) |
| %c | The preferred local date and time representation |
| %d | Day of the month (01..31) |
| %H | Hour of the day, 24-hour clock (00..23) |
| %I | Hour of the day, 12-hour clock (01..12) |
| %j | Day of the year (001..366) |
| %m | Month of the year (01..12) |
| %M | Minute of the hour (00..59) |
| %p | Meridian indicator (“AM’’ or “PM’’) |
| %S | Second of the minute (00..60) |
| %U | Week number of the current year, starting with the first Sunday as the first day of the first week (00..53) |
| %W | Week number of the current year, starting with the first Monday as the first day of the first week (00..53) |
| %w | Day of the week (Sunday is 0, 0..6) |
| %x | Preferred representation for the date alone, no time |
| %X | Preferred representation for the time alone, no date |
| %y | Year without a century (00..99) |
| %Y | Year with century |
| %Z | Time zone name |
| %% | Literal “%’’ character |