User.init method inside User.rb. Is this good practice?

18 views
Skip to first unread message

Brandon

unread,
Apr 2, 2014, 3:20:15 PM4/2/14
to rubyonra...@googlegroups.com
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


Reply all
Reply to author
Forward
0 new messages