Sha256: dd9f16e34029c8d0780d2354d64f2449faae4fc0b971961c1ee821a98cf7a019

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 KB

Contents

module OpenStax::Connect

  class SearchUsers
    lev_routine transaction: :no_transaction

  protected

    def exec(terms, type=:any)
      # Return empty results if no search terms
      return User.where{id == nil}.where{id != nil} if terms.blank?

      # Note: % is the wildcard. This allows the user to search
      # for stuff that "begins with" but not "ends with".
      case type
      when :name
        users = User.scoped
        terms.gsub(/[%,]/, '').split.each do |t|
          next if t.blank?
          query = t + '%'
          users = users.where{(first_name =~ query) | (last_name =~ query)}
        end
      when :username
        query = terms.gsub('%', '') + '%'
        users = User.where{username =~ query}
      when :any
        users = User.scoped
        terms.gsub(/[%,]/, '').split.each do |t|
          next if t.blank?
          query = t + '%'
          users = users.where{(first_name =~ query) | 
                      (last_name =~ query) |
                      (username =~ query)}
        end
      else
        fatal_error(:unknown_user_search_type, data: type)
      end

      outputs[:users] = users
    end

  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
openstax_connect-0.1.0 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.10 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.9 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.8 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.7 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.6 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.5 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.4 app/routines/openstax/connect/search_users.rb
openstax_connect-0.0.3 app/routines/openstax/connect/search_users.rb