Tuesday, February 26, 2013

Wrap Long Strings in Rails


Below is a helper method to wrap long strings.

def wrap(content)
    sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' ')))
end

private

    def wrap_long_string(text, max_width = 30)
      zero_width_space = "​"
      regex = /.{1,#{max_width}}/
      (text.length < max_width) ? text : 
                                  text.scan(regex).join(zero_width_space)
    end

Pushing to Heroku after Model Migration

Just some stuff to remember after after pushing a rails project to Heroku or some other PaaS when it contains a migration.
In the Terminal:
$ git push heroku
$ heroku pg:reset DATABASE
$ heroku run rake db:migrate
$ heroku run rake db:populate # if you have a db rake utility to fill your database with sample data