Tuesday, December 5, 2006

Ruby on Rails Deprecations Part 1: ActionController

Rails provides reasonable facilities for notifying you when deprecated code is encountered. However, I have been unable to find a centralized listing of deprecated features so I can avoid them prior to coding. So, I've examined the Rails 1.2 Release Candidate 1 code to put together such a list. This article contains a list of deprecated Controller items. Subsequent listings will cover Models and Views.

Items are listed alphabetically, followed (if applicable) by the appropriate replacement. If you are aware of omissions or see an error, leave a comment and I'll update the list.

Deprecated: General

  • Components are deprecated.
  • All dependency loaders in module Dependencies now belong to ActiveSupport instead of ActiveController (:depend_on, :dependencies_on, :model, :observer, :service)

Deprecated Controller Instance Variables

  • @cookies, @flash, @headers, @params, @request, @response, @session - use reader/writer methods

Deprecated Controller Methods

  • expire_matched_fragments - use expire_fragment
  • keep_flash - use flash.keep
  • parse_query_parameters - use parse_form_encoded_parameters
  • parse_request_parameters - use parse_form_encoded_parameters
  • redirect_to_path - use redirect_to(path)
  • redirect_to_url - use redirect_to(url)
  • render('#{options}') - use render :file => #{options}
  • url_for(:#{options}) - use url_for with a named route directly

Deprecated Assertions

  • assert_assigned_equal - use assert_equal(expected, @response.template.assigns[key.to_s])
  • assert_cookie_equal - use assert(@response.cookies.key?(key))
  • assert_flash_empty - use assert(!@response.has_flash_with_contents?)
  • assert_flash_equal - use assert_equal(expected, @response.flash[key])
  • assert_flash_exists - use assert(@response.has_flash?)
  • assert_flash_has - use assert(@response.has_flash_object?(key))
  • assert_flash_has_no - use assert(!@response.has_flash_object?(key))
  • assert_flash_not_empty - use assert(@response.has_flash_with_contents?)
  • assert_flash_not_exists - use assert(!@response.has_flash?)
  • assert_invalid_column_on_record - use assert(record.errors.invalid?(column))
  • assert_invalid_record - use assert(!assigns(key).valid?)
  • assert_no_cookie - use assert(!@response.cookies.key?(key))
  • assert_redirect - use assert_response(:redirect)
  • assert_redirect_url - use assert_equal(url, @response.redirect_url)
  • assert_redirect_url_match - use assert(@response.redirect_url_match?(pattern))
  • assert_rendered_file - use assert_template
  • assert_session_equal - use assert_equal(expected, @response[key])
  • assert_session_has - use use assert(@response.has_session_object?(key))
  • assert_session_has_no - use assert(!@response.has_session_object?(key))
  • assert_success - use assert_response(:success)
  • assert_template_equal - use assert_equal(expected, @response.template.assigns[key.to_s])
  • assert_template_has - use assert(@response.has_template_object?(key))
  • assert_template_has_no - use assert(!@response.has_template_object?(key))
  • assert_template_xpath_match - use assert_tag
  • assert_valid_record - use assert(assigns(key).valid?)
  • assert_valid_column_on_record - use assert(!record.errors.invalid?(column))


Anonymous said...

I'm not supposed to use @params in my controller? How exactly do I use the reader/writer method you mention?

rbaldwin said...

Just use params instead of @params.

Anonymous said...

what's confusing to me is that i'm not sure what to do instead of declaring model at the beginning of my controller classes

dba john said...

Hey, Thanks for this list of deprecated functions. As a total new ruby person this is fantastic. I managed to get all the deprecated functions fixed, with one exception. Here's the code snippet that works:

def test_bad_signup
@request.session['return-to'] = "/bogus/location"

post :signup, "user" => { "login" => "newbob", "password" => "newpassword", "password_confirmation" => "wrong" }
assert_invalid_column_on_record "user","password"

my feeble (probaby mis-directed) attempt at fixing this was:
record=User.new(attributes=>value) assert(record.errors.invalid?(["user", "password"]))

but "record" is undefined and my User.new doesn't work.

Can you give me any ideas?

Thanks again for the cool list.

I'll check back here our you can reply to jdanilson at google mail.