Commits

Brian Wisti committed 96a4c3d

push client filter into model

  • Participants
  • Parent commits f3da53d

Comments (0)

Files changed (2)

File app/controllers/clients_controller.rb

 
   def load_clients 
     sort_rule = "#{sort_column} #{sort_direction}"
-    @clients = load_filtered_clients
+    @clients = Client.filtered_by(params[:filters])
         .order(sort_rule).page params[:page]
   end
 
-  def load_filtered_clients
-    filtered = Client
-
-    requested = params[:filters]
-
-    if is_active = requested[:is_active]
-      filtered = filtered.where(is_active: is_active)
-    else
-      filtered = filtered.where(is_active: true)
-    end
-
-    if has_picture = requested[:has_picture]
-      if has_picture == "true"
-        filtered = filtered.where('picture_file_name is not null')
-      elsif has_picture == "false"
-        filtered = filtered.where('picture_file_name is null')
-      end
-    end
-
-    return filtered
-  end
-
   def sort_column
     default = "current_alias"
     return default unless params[:sort]

File app/models/client.rb

     self.update_attributes(point_balance: new_balance)
   end
 
+  def self.filtered_by filters
+    if filters.nil? 
+      filters = { is_active: true }
+    end
+
+    filtered = self
+    if is_active = filters[:is_active]
+      filtered = filtered.where(is_active: is_active)
+    else
+      filtered = filtered.where(is_active: true)
+    end
+
+    if has_picture = filters[:has_picture]
+      if has_picture == "true"
+        filtered = filtered.where('picture_file_name is not null')
+      elsif has_picture == "false"
+        filtered = filtered.where('picture_file_name is null')
+      end
+    end
+
+    return filtered
+  end
+
   def self.quicksearch(query)
     unless query
       return []