Sha256: 2f56c9890bbe426fedf15086074c298bfc73c11bf8324cf27cb3125a7d280211

Contents?: true

Size: 878 Bytes

Versions: 6

Compression:

Stored size: 878 Bytes

Contents

module OpenStax::Connect::Dev
  class CreateUser
    lev_routine

  protected

    def exec(inputs={})

      username = inputs[:username]

      if username.nil? || inputs[:ensure_no_errors]
        loop do 
          break if !username.nil? && OpenStax::Connect::User.where(username: username).none?
          username = "#{inputs[:username] || 'user'}#{rand(1000000)}"
        end
      end

      outputs[:user] = OpenStax::Connect::User.create do |user|
        user.first_name = inputs[:first_name]
        user.last_name = inputs[:last_name]
        user.username = username
        user.openstax_uid = available_negative_openstax_uid
      end
    
      transfer_errors_from(outputs[:user], {type: :verbatim})
    end

    def available_negative_openstax_uid
      (OpenStax::Connect::User.order("openstax_uid DESC").last.try(:openstax_uid) || 0) - 1
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
openstax_connect-0.1.0 app/routines/openstax/connect/dev/create_user.rb
openstax_connect-0.0.10 app/routines/openstax/connect/dev/create_user.rb
openstax_connect-0.0.9 app/routines/openstax/connect/dev/create_user.rb
openstax_connect-0.0.8 app/routines/openstax/connect/dev/create_user.rb
openstax_connect-0.0.6 app/routines/openstax/connect/dev/create_user.rb
openstax_connect-0.0.5 app/routines/openstax/connect/dev/create_user.rb