Oh, how I love the conference season!
After attending the SFI conference in Krakow (which was great, BTW!) I'm ready to go to Prague.I'm going to be at the Euruko 2008 conference. Their program looks really interesting! All things that I like: Ruby, JRuby, testing. Hey, there's even a talk about AOP in Ruby!
I'll be in Prague from Friday afternoon till Sunday evening, and I'm always happy to chat about Rails, TDD, Agile, JRuby.
I'll try to comment about the conference on my Twitter. Peter Cooper is also going to update his twitter. Any other Twitterers in Prague?
See you in Prague!
Thursday, March 27, 2008
Andrzej's Rails tips #9
link_to_remote and GET request
If you're working with link_to_remote and you are surprised with a message like the following:
Only get, put, and delete requests are allowed.
then you just need to know that link_to_remote uses POST request by default. All you need is to do is to add :method => :get to the method call.
Use schema.rb to create a new database
Keep the schema.rb file in your Subversion/Git/Mercurial repository and make sure it's up-to-date. It's very useful when you want to create a database without using migrations. You just call rake db:schema:load.
As the documentation states:
"Note that this schema.rb definition is the authoritative source for your database schema. If you need to create the application database on another system, you should be using db:schema:load, not running all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations you'll amass, the slower it'll run and the greater likelihood for issues)."
If you're working with link_to_remote and you are surprised with a message like the following:
Only get, put, and delete requests are allowed.
then you just need to know that link_to_remote uses POST request by default. All you need is to do is to add :method => :get to the method call.
Use schema.rb to create a new database
Keep the schema.rb file in your Subversion/Git/Mercurial repository and make sure it's up-to-date. It's very useful when you want to create a database without using migrations. You just call rake db:schema:load.
As the documentation states:
"Note that this schema.rb definition is the authoritative source for your database schema. If you need to create the application database on another system, you should be using db:schema:load, not running all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations you'll amass, the slower it'll run and the greater likelihood for issues)."
Monday, February 25, 2008
Andrzej's Rails tips #8
Resource controller and 'new' action
If you want to refer to the 'new' action using resource controller, you have to use the 'new_action' method.
BTW, if you want to do the same operation before both 'new' and 'edit' actions in a resource controller, you can do it like that:
Migrations and removing defaults
If you have some default values set on some columns, then you can remove them using ':default => nil'.
Like that:
If you want to refer to the 'new' action using resource controller, you have to use the 'new_action' method.
BTW, if you want to do the same operation before both 'new' and 'edit' actions in a resource controller, you can do it like that:
[new_action, edit].each do |action|
action.before { @product_types = ProductType.find(:all) }
end
Migrations and removing defaults
If you have some default values set on some columns, then you can remove them using ':default => nil'.
Like that:
change_column :users, :name, :string, :default => nil
Tuesday, February 19, 2008
Andrzej's Rails tips #7
Dumping and loading data
Recently, I was in a need to dump data from a sqlite database and load it to a mysql database. There are many ways of doing such a task. One of them is using a plugin released by the Heroku people - YamlDb.
You can install it with the following:
attachment-fu and capistrano
I use attachment-fu for one of my projects. One of the open questions here is how to deal with the files that our users uploaded to the server. I use capistrano 2, and ideally I prefer to have everything automated. The way you can use capistrano to deal with uploaded files is to have a directory which will be shared across different releases (the same as logs).
First, we need to tell attachment-fu that the upload directory is going to be public/uploads. Then, we need to tell Capistrano that public/uploads is going to be a shared directory. Both those things are nicely explained in the following article:
Working with attachment_fu
Recently, I was in a need to dump data from a sqlite database and load it to a mysql database. There are many ways of doing such a task. One of them is using a plugin released by the Heroku people - YamlDb.
rake db:data:dump
rake db:data:load
You can install it with the following:
script/plugin install http://opensource.heroku.com/svn/rails_plugins/yaml_db
attachment-fu and capistrano
I use attachment-fu for one of my projects. One of the open questions here is how to deal with the files that our users uploaded to the server. I use capistrano 2, and ideally I prefer to have everything automated. The way you can use capistrano to deal with uploaded files is to have a directory which will be shared across different releases (the same as logs).
First, we need to tell attachment-fu that the upload directory is going to be public/uploads. Then, we need to tell Capistrano that public/uploads is going to be a shared directory. Both those things are nicely explained in the following article:
Working with attachment_fu
Monday, February 18, 2008
Andrzej's Rails tips #6
Redo a migration
There is a new rake task for redoing a migration:
BTW, if you need to do it on a production server, then just append RAILS_ENV:
and of course, if you want to list all rake tasks:
Migrating to Rails 2.0 and mass-assignment warning
I was upgrading one application from Rails 1.2.3 to Rails 2.0. The application uses restful_authentication. One of the problems we have seen, was a warning in the log, saying something like 'mass-assignment no longer supported'. After investigation we found out that the problem was updating the user object. The User model was generated with restful_authentication, but later there were some application specific attributes added by a developer. Since restful_authentication generates a line like that:
it means other attributes are not accessible. Adding the new attributes to this list solves the problem.
There is a new rake task for redoing a migration:
rake db:migrate:redo
BTW, if you need to do it on a production server, then just append RAILS_ENV:
rake db:migrate:redo RAILS_ENV=production
and of course, if you want to list all rake tasks:
rake -T
Migrating to Rails 2.0 and mass-assignment warning
I was upgrading one application from Rails 1.2.3 to Rails 2.0. The application uses restful_authentication. One of the problems we have seen, was a warning in the log, saying something like 'mass-assignment no longer supported'. After investigation we found out that the problem was updating the user object. The User model was generated with restful_authentication, but later there were some application specific attributes added by a developer. Since restful_authentication generates a line like that:
attr_accessible :email
it means other attributes are not accessible. Adding the new attributes to this list solves the problem.
atttr_accessible :email, :birthdate
Monday, February 11, 2008
Andrzej's Rails tips #5
Two things today, both related to RSpec stories: webrat, and using regexps in RSpec stories.
Webrat with RSpec stories
What is Webrat? "Webrat lets you quickly write robust and thorough acceptance tests for a Ruby web application". It uses Hpricot under the hood and is very easy to understand just by looking at the code.
It took me only 30 minutes to turn most of my tests in one of my applications from a classic IntegrationTest-based RSpec story to Webrat. Here is one example:
Thanks to Ben, for his great article describing RSpec stories with Webrat.
RSpec, response.should have_text
Sometimes, all you need is just a check whether there is a certain message visible on a page. One way of doing that is with regexps. Here is an example step that checks for the message:
Webrat with RSpec stories
What is Webrat? "Webrat lets you quickly write robust and thorough acceptance tests for a Ruby web application". It uses Hpricot under the hood and is very easy to understand just by looking at the code.
It took me only 30 minutes to turn most of my tests in one of my applications from a classic IntegrationTest-based RSpec story to Webrat. Here is one example:
When "he creates an order" do
visits '/'
clicks_link "New order"
fills_in "Nr", :with => 'abc/2008'
fills_in "Company", :with => 'ABC company'
selects 'New'
clicks_button 'Create'
end
Thanks to Ben, for his great article describing RSpec stories with Webrat.
RSpec, response.should have_text
Sometimes, all you need is just a check whether there is a certain message visible on a page. One way of doing that is with regexps. Here is an example step that checks for the message:
Then "he sees a $message" do |message|
response.should have_text(Regexp.new(message))
end
Thursday, February 7, 2008
Andrzej's Rails tips #4
will_paginate in other languages
The default labels for the will_paginate are 'Next' and 'previous'. It's trivial to change them like that:
Rails plugins, Piston and URLs
I use piston for plugins management. It's a nice tool that allows you to call 'piston up' on any of the plugins, at any time to update. What it does worse than 'script/plugin' command is managing different repositories. With the 'plugin' command you can say:
What I use with Piston instead, is storing the urls as environment variables. Here is an excerpt of my ~/.bash_login file:
So I just type 'piston import $ATTA', press TAB, and the shell autocompletion does it for me.
The default labels for the will_paginate are 'Next' and 'previous'. It's trivial to change them like that:
<%= will_paginate :prev_label => 'Wstecz', :next_label => 'Dalej'%>
Rails plugins, Piston and URLs
I use piston for plugins management. It's a nice tool that allows you to call 'piston up' on any of the plugins, at any time to update. What it does worse than 'script/plugin' command is managing different repositories. With the 'plugin' command you can say:
script/plugin source URLand it adds the URL to the list of known repositories.
What I use with Piston instead, is storing the urls as environment variables. Here is an excerpt of my ~/.bash_login file:
export ATTACHMENT_FU=http://svn.techno-weenie.net/projects/plugins/attachment_fu/
export RESOURCE_CONTROLLER=http://svn.jamesgolick.com/resource_controller/tags/stable/
export ATTRIBUTE_FU=http://svn.jamesgolick.com/attribute_fu/tags/stable
export RESTFUL_AUTHENTICATION=http://svn.techno-weenie.net/projects/plugins/restful_authentication/
export RSPEC=http://rspec.rubyforge.org/svn/tags/CURRENT/rspec
export RSPEC_ON_RAILS=http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails
So I just type 'piston import $ATTA', press TAB, and the shell autocompletion does it for me.
Subscribe to:
Posts (Atom)