Tag: Ruby on Rails

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

Easy way to extend and overwrite bb-ruby bbcode gem translations in Ruby on Rails

When you go to bb-ruby gem readme, there is an instruction how to extend it with your own translations:

my_block = {
  'Quote' => [
    /\[quote(:.*)?=(.*?)\](.*?)\[\/quote\1?\]/mi,
    '<div class="quote"><p>\2</p><block>\3</block></div>',
    'Quote with citation',
    '[quote=mike]please quote me[/quote]',
    :quote
  ],      
}

text.bbcode_to_html(my_blockquote)

This method works quite good but well... it's way to inconvenient. Where should we put our translation? In our model? Should we create a different model which would store our costume translations? Nah... There is really easy and clean way to add new (and overwrite already existing) translations into a bbcode parser.

Create in your app initializers directory (/config/initializers/) a file called bb-ruby.rb. Below you can see an example with [spoiler][/spoiler] tag:

module BBRuby
  if @@tags
    @@tags['Spoiler'] = [
      /\[spoiler\](.*?)\[\/spoiler\1?\]/mi,
      '<span class="spoiler">\1</span>',
      'spoiler',
      '[spoiler]spoiler[/spoiler]',
      :spoiler]
  end
end

And how to overwrite existing translation? Just replace it with new one. Below example which replaces standard "Link" rule, with same rule but with rel="nofollow":

@@tags['Link'] = [
  /\[url=(.*?)\](.*?)\[\/url\]/mi,
  '<a href="\1" rel="nofollow">\2</a>',
  'Hyperlink to somewhere else',
  'Maybe try looking on [url=http://google.com]Google[/url]?',
  :link]

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑