Commit a06eff3

mokha <mokha@cisco.com>
2018-01-26 20:28:19
map to scim in repository.
1 parent 721a221
Changed files (3)
app/controllers/scim/v2/users_controller.rb
@@ -11,20 +11,20 @@ module Scim
 
       def show
         user = repository.find!(params[:id])
-        response.headers['Location'] = scim_v2_users_url(user)
-        render json: user.to_scim.to_json, status: :ok
+        response.headers['Location'] = user.meta.location
+        render json: user.to_json, status: :ok
       end
 
       def create
         user = repository.create!(user_params)
-        response.headers['Location'] = scim_v2_users_url(user)
-        render json: user.to_scim.to_json, status: :created
+        response.headers['Location'] = user.meta.location
+        render json: user.to_json, status: :created
       end
 
       def update
         user = repository.update!(params[:id], user_params)
-        response.headers['Location'] = scim_v2_users_url(user)
-        render json: user.to_scim.to_json, status: :ok
+        response.headers['Location'] = user.meta.location
+        render json: user.to_json, status: :ok
       end
 
       def destroy
app/models/user.rb
@@ -23,20 +23,6 @@ class User < ApplicationRecord
     nil
   end
 
-  def to_scim(url_helpers = Rails.application.routes.url_helpers)
-    Scim::Shady::User.build do |x|
-      x.id = uuid
-      x.username = email
-      x.created_at = created_at
-      x.updated_at = updated_at
-      x.location = url_helpers.scim_v2_users_url(self)
-      x.version = lock_version
-      x.emails do |y|
-        y.add(email, primary: true)
-      end
-    end
-  end
-
   private
 
   def access_token
app/models/user_repository.rb
@@ -1,20 +1,36 @@
 class UserRepository
   def find!(id)
-    User.find_by!(uuid: id)
+    map_from(User.find_by!(uuid: id))
   end
 
   def create!(params)
     password = SecureRandom.hex(32)
-    User.create!(email: params[:userName], password: password)
+    map_from(User.create!(email: params[:userName], password: password))
   end
 
   def update!(id, params)
-    user = find!(id)
+    user = User.find_by!(uuid: id)
     user.update!(email: params[:userName])
-    user
+    map_from(user)
   end
 
   def destroy!(id)
-    find!(id).destroy!
+    User.find_by!(uuid: id).destroy!
+  end
+
+  private
+
+  def map_from(user, url = Rails.application.routes.url_helpers)
+    Scim::Shady::User.build do |x|
+      x.id = user.uuid
+      x.username = user.email
+      x.created_at = user.created_at
+      x.updated_at = user.updated_at
+      x.location = url.scim_v2_users_url(user)
+      x.version = user.lock_version
+      x.emails do |y|
+        y.add(user.email, primary: true)
+      end
+    end
   end
 end