Module: RPlatform::Rails::ModelExtensions::ActsAsFacebookUser::ClassMethods

Defined in:
lib/rplatform_rails/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#find_or_create_by_facebook_session(options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rplatform_rails/model_extensions.rb', line 118

def find_or_create_by_facebook_session(options={})
  RAILS_DEFAULT_LOGGER.info "** RFACEBOOK DEPRECATION WARNING: acts_as_facebook_user will probably be deprecated in a future version of the RFacebook plugin"
  
  # parse the options (for backwards compatibility, options MIGHT be a FacebookWebSession)
  if options.is_a?(RPlatform::FacebookWebSession)
    fbsession = options
    options = {}
  else
    fbsession = options[:facebook_session]
  end
  
  # check that we have an fbsession
  unless fbsession.is_a?(Rplatform::FacebookWebSession)
    RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: find_or_create_by_facebook_session needs a :facebook_session specified"
    return nil
  end
  
  # if the session is ready to use...
  if fbsession.ready?
    
    # find or create a user
    instance = find_by_facebook_uid(fbsession.session_user_id) || self.new(options)
    
    # update session info
    instance.facebook_session = fbsession
    
    # update (or create) the object and return it
    if !instance.save
      RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: failed to update or create the Facebook user object in the database"
      return nil
    end
    return instance

  # session was not ready
  else
    RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: tried to use an inactive session for acts_as_facebook_user (in find_or_create_by_facebook_session)"
    return nil
  end
    
end