Page 96 of 170

Upgrading to Rails 3.2.0 from Rails 3.1.3

Yup, finally it's here! :) Switching Susanoo (Senpuu v5) to 3.2 wasn't so hard. Took me about 5 hours. Why so long? Well because I had to fix my "special" Paperclip version and make so additional table names changes. All the steps are presented below. Enjoy.

Gemfile

gem "rails", '3.2.0'

group :assets do
  gem 'sass-rails', '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3', :require => 'uglifier'
end

Weird stuff

rename hash column hash is defined by ActiveRecord

I've got some problems with my attributes names. Looks like, you cannot use "hash" as an attribute name. Also weird stuff was happening with my "email_change" attribute. I could write to it but I could not retrieve values (always nil). Renaming those two attributes fixed problems. So just:

        rename_column :users, :email_change, :changed_at

Small fixes

Instead of:

set_table_name :table

use:

self.table_name= :table

Templates

Passing the format in the template name is deprecated. 
Please pass render with :formats => [:html] instead

When using render with a template we've been passing a template format. For example:

render "/shared/404.html", :status => 404, :layout => false

Now we don't need to provide the file format, instead we just do it like this:

render "/shared/404", :formats => [:html], :status => 404, :layout => false

Form builder doesn't accept range as a param :(

Don't know why this is not supported. When building forms, we cannot pass a range. Instead we must pass an array. So to fix it just perform to_a on a range:

# Won't work:
1..31
# Works like a charm:
(1..31).to_a

Write inheritable attribute

Last but not least, write_inheritable_attribute was removed so now instead of:

write_inheritable_attribute(:attribute_name, value)

we should use:

class_attribute :attribute_name
self.attribute_name = value

Converting nested hash into HTTP url params hash version in Ruby

We cannot send nested hash as a param in HTTP requests. For example, when we would like to send something like this:

{:key => "value", :nested => {:nest => "value"}}

It would be (or should be) mapped like this:

/url/?key=value&nested=%7B%3Anest%3D%3E%5C%22value%5C%22%7D

Doesn't look to good ;) However there is a simple way to convert a nested hash into a params acceptable form. We can convert it to a form, that can be mapped into params like this:

/url/?key=value&nested[nest]=value

Here is method to convert any nested hash to a "one level" equivalent:

  module HashConverter

    def self.encode(value, key = nil, out_hash = {})
      case value
      when Hash  then 
        value.each { |k,v| encode(v, append_key(key,k), out_hash) }
        out_hash
      when Array then 
        value.each { |v| encode(v, "#{key}[]", out_hash) }
        out_hash
      when nil   then ''
      else
        out_hash[key] = value
        out_hash
      end
    end

    private

    def self.append_key(root_key, key)
      root_key.nil? ? :"#{key}" : :"#{root_key}[#{key.to_s}]"
    end

  end

And usage example:

hash = {:level0 => 'value', :nested => {:nest1 => "val1", :nest2 => "val2"}}
HashConverter.encode(hash)
#=> {:level0=>"value", :"nested[nest1]"=>"val1", :"nested[nest2]"=>"val2"}

This form can be easily mapped into a HTTP url params

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑