Ruby on Rails Wednesday, April 2, 2014

I'm sort of conflicted here. I'm using CanCan and it automatically load and authorize User/Create.

My Facebook/Init is as follows. I'm tempted to do something similar and call User/Init from User/Create method. This way I hide the implementation in the UserController so User/Create and User/Update will still call the User/Init method. The problem starts when I need the Controller to know whether it is a new record or just an update.

Would you do this differently?

class Facebook < ActiveRecord::Base
  belongs_to :user

  def self.init(fb_user)
    user = User.find_by_identifier(fb_user.try(:identifier).try(:to_s))
    if user
      user.identifier   = fb_user.try(:identifier).try(:to_s)
      user.access_token = fb_user.access_token.try(:to_s)
    else
      user = User.new
      user.updating_password = true
      user.identifier   = fb_user.try(:identifier).try(:to_s)
      user.access_token = fb_user.try(:access_token).try(:to_s)
      user.username     = fb_user.try(:username).try(:to_s)

      user.send_registration_confirmation
    end
    user
  end


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/88b9f718-57e4-44ca-8abb-7e9edbc38788%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment