diff options
author | George Andrinopoulos <geoandri@gmail.com> | 2017-05-15 16:53:12 +0300 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2017-05-15 16:53:12 +0300 |
commit | 872e7b7efe923192d4ef90b01672038518ba66fc (patch) | |
tree | f4dfdf553d480d0d7539f9dbd2e5bb630860a844 /app | |
parent | f5e5afc7901b646ab26130893bcbccbe5452db18 (diff) |
Create a Users Finder
Diffstat (limited to 'app')
-rw-r--r-- | app/finders/users_finder.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/app/finders/users_finder.rb b/app/finders/users_finder.rb new file mode 100644 index 00000000000..dbd50d1db7c --- /dev/null +++ b/app/finders/users_finder.rb @@ -0,0 +1,74 @@ +# UsersFinder +# +# Used to filter users by set of params +# +# Arguments: +# current_user - which user use +# params: +# username: string +# extern_uid: string +# provider: string +# search: string +# active: boolean +# blocked: boolean +# external: boolean +# +class UsersFinder + attr_accessor :current_user, :params + + def initialize(current_user, params = {}) + @current_user = current_user + @params = params + end + + def execute + users = User.all + users = by_username(users) + users = by_search(users) + users = by_blocked(users) + users = by_active(users) + users = by_external_identity(users) + users = by_external(users) + + users + end + + private + + def by_username(users) + return users unless params[:username] + + users.where(username: params[:username]) + end + + def by_search(users) + return users unless params[:search].present? + + users.search(params[:search]) + end + + def by_blocked(users) + return users unless params[:blocked] + + users.blocked + end + + def by_active(users) + return users unless params[:active] + + users.active + end + + def by_external_identity(users) + return users unless current_user.admin? && params[:extern_uid] && params[:provider] + + users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid])) + end + + def by_external(users) + return users = users.where.not(external: true) unless current_user.admin? + return users unless params[:external] + + users.external + end +end |