Wednesday, March 6, 2013

Saving User Location · intridea/omniauth Wiki · GitHub

Customizing the callback of an OAuth request using Omniauth is pretty simple.  In you sessions controller where you save the OAuth token, simply add a redirect to omniauth.origin or some other desired destination.  omniauth.origin saves the user's  last location using the http_referrer.



class SessionsController < ApplicationController
  def callback
    # do your authentication stuff here...
    redirect_to request.env['omniauth.origin'] || '/default'
  end
end
I ended up setting the origin in my urls directly.
/auth/twitter?origin=%2Fusers%2Fshow%2F24

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 = "&#8203;"
      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