Sha256: a4fe18fe3c5be43d9cd62847998f73007a2d855e8598283f9f5798fe73f22a6c

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module OpenStax::Connect

  class SearchUsers
    include Lev::Algorithm

  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 = 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
        raise IllegalArgument, "Unknown user search type: #{type.to_s}"
      end

      return users
    end

    def self.default_transaction_isolation
      Lev::TransactionIsolation.serializable
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
openstax_connect-0.0.1 app/algorithms/openstax/connect/search_users.rb