Archive for category Articles
TDD on Rails #4: RSpec, Authlogic, Factory_girl and resource_controller
So well, I decided to use RSpec, Authlogic, Factory_Girl and resource_controller on my new application! But I’ve encountered some problems making all these working together. I mean I did not find any good tutorial showing how to do it step by step, so there it is!
Rails 2.3 on Debian
I decided to install a new Rails environment in a VM using Debian (testing) to be able to code when I don’t have my Macbook.
This is my experience. Read the whole article before running any command you find in here.
I thought that it would be as easy as:
1 2 3 4 5 | debian~:# aptitude install ruby rubygems debian~:# gem install rails debian~:# rails myapp debian~:# cd myapp debian~:# ./script/server |
But the result was not as expected:
1 | Rails requires RubyGems >= 1.3.1 (you have 1.2.0). Please `gem update --system` and try again. |
Well, let’s do as told:
1 2 3 | debian:~/myapp# gem update --system ERROR: While executing gem ... (RuntimeError) gem update --system is disabled on Debian. RubyGems can be updated using the official Debian repositories by aptitude or apt-get. |
No good… What now? Let’s google it. Yeah there is a gem that allows you to update rubygems to the latest version:
1 2 3 4 | debian:~/myapp# gem install rubygems-update debian:~/myapp# update_rubygems debian:~/myapp# gem install rails debian:~/myapp# ./script/server |
Voila! (almost)
Did we need to reinstall the Rails gem? Well, yeah… Debian’s version of rubygems stores his stuff in the /var/lig/gems folder while this new updated version stores in /usr/lib/ruby(…).
A good thing with this rubygems installation is that we do not need to edit the path anymore as the bin (rails…) are now in the /usr/bin folder.
Now that we have solved the rubygems version problem, let see what problem is showing now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | debian:~/myapp# ./script/server => Booting WEBrick => Rails 2.3.2 application starting on http://0.0.0.0:3000 /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:269:in `require_frameworks': no such file to load -- net/https (RuntimeError) from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:134:in `process' from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in `send' from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in `run' from /root/myapp/config/environment.rb:9 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:521:in `new_constants_in' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:156:in `require' from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:84 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from ./script/server:3 |
net/http??? Google, my friend, what’s going on? After finding articles about recompiling ssl, i found this solution, much more convinient:
1 2 | debian:~myapp/# aptitude install libopenssl-ruby1.8 debian:~/myapp# ./script/server |
Voilà!!! It’s working at last! I must admit that I am a little desapointed. I thought it would be much easier to have rails 2.3 running on debian.
My next step is to have Vim configured so it would be very close to using Textmate!
TDD on Rails #1
Back here, talking about my journey learning Ruby and Rails.
What would be Rails without TDD? And what is TDD? BDD? Test Driven Development and Behaviour Driven Development… Well, I won’t explain these 2 concept here, there is Pliny of articles about it: http://en.wikipedia.org/wiki/Test-driven_development
So I began learning about TDD and BDD and what is the common way to do this in Rails… RSpec is the answer!
As I’ve been working with traditional Java/J2EE/RUP way of life, when I first read: write your test first, I thought: what the fuck? And my first steps (beside learning how works RSpec) were:
- Create a test! Done! It fails! Great!!
- Write some code (a rails model)! Done! The test succeeded! Great!!!
- Think about what must be the behaviour of the model! Great!!
- Write the code before the test! DAMN!!!!
So It’s gonna be hard… Bad habits.. But This is the way to go, trust me!
Namespaced resource generation with resource_controller!
Most of the time, in my projects, I need a separated administration area: http://teste.local/admin/*
For those who doesn’t know resource_controller (r_c) yet, please visit: Restful Controllers with resource_controller. I’ll never have a Rails app not using it, that’s for sure!
The goal here is to create a resource called Post and access it using http://teste.local/admin/posts .
My first thought was to try:
1 | script/generate scaffold_resource admin/Posts title:string content:text |
But it created things in a way I did not like:
- app/models/admin/posts.rb ( I really do not want my model to be in the admin folder)
- db/migrate/20090119032314_create_admin_posts.rb / create_table :admin_posts (In fact it created a admin_posts table … :-( )
I then resigned myself to the fact that I’ll have to generate in the “normal” way and then hack the generated files to adapt to what I wanted to do. Here we go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | script/generate scaffold_resource Posts title:string content:text exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/posts exists test/functional/ exists test/unit/ create app/views/posts/index.html.erb create app/views/posts/show.html.erb create app/views/posts/new.html.erb create app/views/posts/edit.html.erb create app/views/posts/_form.html.erb create app/models/posts.rb create app/controllers/posts_controller.rb create app/helpers/posts_helper.rb create test/functional/posts_controller_test.rb create test/unit/posts_test.rb create test/fixtures/posts.yml create db/migrate create db/migrate/20090119032942_create_posts.rb route map.resources :posts |
I then changed the following:
1 2 3 4 | app/views/posts to app/views/admin/posts app/controllers/posts_controller.rb to app/controllers/admin/posts_controller.rb app/helpers/posts_helper.rb to app/helpers/admin/posts_helper.rb test/functional/posts_controller_test.rb to test/functional/admin/posts_controller_test.rb |
Don’t forget to change add Admin:: in all the .rb files that you moved.
Last thing to change is the route to the Post resource:
1 2 3 4 5 | map.resources :posts #Has to be replaced by: map.namespace :admin do |admin| admin.resources :posts #Directs /admin/posts/* to Admin::PostsController (app/controllers/admin/posts_controller.rb) end |
Namespaces configuration is pretty straight forward and there is loads of articles and websites explaining it. So if you’re confused about this, google it! ;-)
That should do it. Now you can access http://teste.local/admin/posts