Commit 91d1d15

mo <mo@mokhan.ca>
2019-05-29 01:21:39
support a basic or query
1 parent 0c66c67
Changed files (3)
app
spec
app/models/scim/visitor.rb
@@ -0,0 +1,17 @@
+module Scim
+  class Visitor
+    def self.result_for(tree)
+      attr = SCIM::User::ATTRIBUTES[tree[:attribute].to_s] || tree[:attribute].to_s
+      case tree[:operator].to_s
+      when 'or'
+        result_for(tree[:left]).or(result_for(tree[:right]))
+      when 'eq'
+        User.where(attr => tree[:value].to_s[1..-2])
+      when 'ne'
+        User.where.not(attr => tree[:value].to_s[1..-2])
+      else
+        User.none
+      end
+    end
+  end
+end
app/models/user.rb
@@ -13,17 +13,7 @@ class User < ApplicationRecord
   validates :timezone, inclusion: VALID_TIMEZONES
   validates :locale, inclusion: VALID_LOCALES
 
-  scope :scim_filter_for, -> (tree) do
-    attr = SCIM::User::ATTRIBUTES[tree[:attribute].to_s] || tree[:attribute].to_s
-    case tree[:operator].to_s
-    when 'eq'
-      where(attr => tree[:value].to_s[1..-2])
-    when 'ne'
-      where.not(attr => tree[:value].to_s[1..-2])
-    else
-      self
-    end
-  end
+  scope :scim_filter_for, -> (tree) { Scim::Visitor.result_for(tree) }
 
   def name_id_for(name_id_format)
     Saml::Kit::Namespaces::PERSISTENT == name_id_format ? id : email
spec/models/user_spec.rb
@@ -36,6 +36,7 @@ RSpec.describe User do
       parse_tree = tree_for(%(userName eq "#{first_user.email}" or userName eq "#{second_user.email}"))
       puts parse_tree
       results = User.scim_filter_for(parse_tree)
+      puts results.to_sql
       expect(results.pluck(:email)).to match_array([first_user.email, second_user.email])
     end
   end