Thursday, September 30, 2010

Moving rails app from 2.3.5 to 2.3.9

In my last post, at the end, I mentioned about migrating my rails app from version 2.3.5 to 2.3.9 for making it more closer to rails 3.0. I worked on the same and it went well. I localized rack-1.1.0 version as rails 2.3.9 has this dependency. Most of the steps that I followed were similar to my earlier post except the change in version number.

Listing the deprecation warnings that need to be fixed that I received during the migration :-

(1) DEPRECATION WARNING: Giving :session_key to SessionStore is deprecated, please
use :key instead. (called from new at app_root/vendor/rails/actionpack/lib/action_controller/middleware_stack.rb:72)

Change :session_key to :key in environment.rb file :-

config.action_controller.session = {
:key => '_radar_session',
:secret => '271c2f352da72.....'
}


(2) DEPRECATION WARNING: config.load_paths is deprecated and removed in Rails 3, please use autoload_paths instead

Change in the environment.rb file :-

From config.load_paths to config.autoload_paths

(3) DEPRECATION WARNING: Date#last_month is deprecated and has been removed in Rails 3, please use Date#prev_month instead. (called from ./spec/models/my_model_name_spec.rb:10)

(4) DEPRECATION WARNING: Time#last_month is deprecated and has been removed in Rails 3, please use Time#prev_month instead. (called from ./spec/models/my_model_name_spec.rb:41)

Here are the actual changes in rails 2.3.9 :-
app_root\vendor\rails\activesupport\lib\active_support\core_ext\date\calculations.rb

(5) DEPRECATION WARNING: Rake tasks in vendor/plugins/xss_terminate/tasks are deprecated. Use lib/tasks instead. (called from app_root/vendor/rails/railties/lib/tasks/rails.rb:10)

Move the tasks folder inside the lib folder of the plugin directory and the deprecation warning should go away. But you may need to change some require file paths according to new placement of tasks folder.

(6) DEPRECATION WARNING: Object#returning has been deprecated in favor of Object#tap. (called from helper at app_root/vendor/gems/rspec-rails-1.3.0/lib/spec/rails/example/helper_example_group.rb:59)

I googled around for this deprecation warning and found some useful articles. Sharing one of those articles :-

Upgrading to Rails 3: Beware of the Object#tap pattern
http://feeds.simonecarletti.com/simonecarletti/en


I also tried with rspec-rails-1.3.2 gem, but it also threw same deprecation warning. So I just opened the corresponding class and extended the implementation as :-

From
@helper_object ||= returning HelperObject.new do |helper_object|

To
@helper_object ||= HelperObject.new.tap do |helper_object|

I made similar change for app_root\vendor\gems\rspec-rails-1.3.0\lib\spec\rails\mocks.rb :-

From
returning model_class.new do |model|

To
model_class.new.tap do |model|

Now, I am much closer to moving my app to Rails 3.0 version :-)

Tuesday, September 28, 2010

Migrating rails app from 2.2.2 to 2.3.5

I recently migrated my rails app from rails version 2.2.2 to 2.3.5. I enjoyed the work as it was a good learning for me. Listing the steps that I followed and some issues that I faced during the migration.

Gems installed on my windows machine :-

mongrel (1.1.5 x86-mingw32)
rake (0.8.7)
rack (1.0.1)
rubygems-update (1.3.7)


(1) I installed rails :-

gem install rails -v=2.3.5

(2) Deleted rails folder (2.2.2) from app/vendor.

environment.rb file -

RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION


Go to the project root and execute :-

rake rails:update --trace

It will update some js files and also renames application.rb to application_controller.rb

** Invoke rails:update (first_time)
** Invoke rails:update:scripts (first_time)
** Execute rails:update:scripts
** Invoke rails:update:javascripts (first_time)
** Execute rails:update:javascripts
** Invoke rails:update:configs (first_time)
** Execute rails:update:configs
** Invoke rails:update:application_controller (first_time)
** Execute rails:update:application_controller
app_root/app/controllers/application.rb has been renamed to app_root/app/controllers/application_controller.rb, update your SCM as necessary
** Execute rails:update

(3) Rename application_spec.rb to application_controller_spec.rb

(4) Localize rails version into the app.

rake rails:freeze:gems

(5) I was having rspec(1.2.3) and rspec-rails(1.2.3) plugins which I removed and installed rspec(1.3.0) and rspec-rails(1.3.0) gems instead and then localized in the app/vendor.

gem install rspec -v 1.3.0
gem install rspec-rails -v 1.3.0


environment.rb file :-

config.gem "rack", :version => "1.0.1"
config.gem "rspec-rails", :lib => false, :version => ">= 1.3.0"
config.gem "rspec", :lib => false, :version => ">= 1.3.0"


Go to the project root and execute :-
rake gems:unpack

(6) I kept a copy of my customized spec/spec_helper.rb file because I wanted to run "ruby script/generate rspec" for upgrading rspec-rails in the app which overwrites these three files :-

* spec/spec.opts
* spec/rcov.opts
* spec/spec_helper.rb

(7) While running the specs, I got flash.now[:message] specs failing.

I added following in the spec/spec_helper.rb file to fix the issue with that spec.

module DisableFlashSweeping
def sweep
end
end


module EnableFlashSweeping
def sweep
keys.each do |k|
unless @used[k]
use(k)
else
delete(k)
@used.delete(k)
end
end
# clean up after keys that could have been left over by calling reject! or shift on the flash
(@used.keys - keys).each{ |k| @used.delete(k) }
end
end


And I used the calls in the spec before the action as :-

it 'should retain the flash message only for the current request' do
controller.instance_eval { flash.extend(DisableFlashSweeping) }
post :create, :work_loc => {:country => "India", :state => "Maharashtra"}
response.should be_success
flash[:message].should == "Error in Loc mapping!"
end


(8)
I changed in rspec from

response.should render_template(:states) '
TO
controller.should_receive(:render).with(:partial => 'states')

(9) Changed response.headers["Status"] to response.status in some of the controller specs.

(10) I got one deprecation warning about formatted_url :-

DEPRECATION WARNING: formatted_action_name_controller_name_url() has been deprecated. Please pass format to the standard action_name_controller_name_url method instead..

So changed :action from
:action => formatted_action_name_controller_name_url('js')
to
:action => action_name_controller_name_url(:format => 'js')

Now, along with the rspecs, my app is looking good with rails 2.3.5

Planning to go ahead with migrating to 2.3.9 to make the app more closer to Rails 3.0.

Tuesday, September 21, 2010

The Last Lecture by Randy Pausch

Today I received an email from my uncle about The Last Lecture by Randy Pausch. I was wondering why I did not get this email from anyone else before as it seems a quite old email. It's ok ! The important thing is I got a chance to read Randy's simple but succinct view points about life. I can not resist myself to share those with all.
----
On September 18, 2007, Carnegie Mellon professor and alumnus Randy Pausch delivered a one-of-a-kind last lecture that made the world stop and pay attention. It became an Internet sensation viewed by millions, an international media story, and a best-selling book "The Last Lecture" that has been published in 35 languages. To this day, people everywhere continue to talk about Randy, share his message and put his life lessons into action in their own lives.

In a letter to his wife Jai and his children, Dylan, Logan , and Chloe, he wrote this beautiful "guide to a better life" for his wife and children to follow. May you be blessed by his insight.

POINTS ON HOW TO IMPROVE YOUR LIFE

Personality:
1. Don't compare your life to others'. You have no idea what their journey is all about.
2. Don't have negative thoughts of things you cannot control. Instead invest your energy in the positive present moment
3. Don't over do; keep your limits
4. Don't take yourself so seriously; no one else does
5. Don't waste your precious energy on gossip
6. Dream more while you are awake
7. Envy is a waste of time. You already have all you need..
8. Forget issues of the past. Don't remind your partner of his/her mistakes of the past. That will ruin your present happiness.
9. Life is too short to waste time hating anyone. Don't hate others.
10. Make peace with your past so it won't spoil the present
11. No one is in charge of your happiness except you
12. Realize that life is a school and you are here to learn.
Problems are simply part of the curriculum that appear and fade away like algebra class but the lessons you learn will last a lifetime.
13. Smile and laugh more
14. You don't have to win every argument. Agree to disagree.

Community:
15. Call your family often
16. Each day give something good to others
17. Forgive everyone for everything
18. Spend time with people over the age of 70 & under the age of 6
19. Try to make at least three people smile each day
20. What other people think of you is none of your business
21. Your job will not take care of you when you are sick. Your family and friends will. Stay in touch.

Life:
22. Put GOD first in anything and everything that you think, say and do.
23. GOD heals everything
24. Do the right things
25. However good or bad a situation is, it will change
26. No matter how you feel, get up, dress up and show up
27. The best is yet to come
28. Get rid of anything that isn't useful, beautiful or joyful
29. When you’re awake alive in the morning, thank GOD for it
30. If you know GOD you will always be happy. So, be happy.

Here is the video link as well :-
http://video.google.com/videoplay?docid=-5700431505846055184#