Sha256: 65b851d3a1119af4b201c698cc2317e365e080266783c2218276594fd6e1f910

Contents?: true

Size: 942 Bytes

Versions: 13

Compression:

Stored size: 942 Bytes

Contents

module Virgo
  class User < ActiveRecord::Base
    module Search
      extend ActiveSupport::Concern

      included do
        scope :search, ->(filters=nil) {
          filters ||= {}

          items = all

          if filters[:term].present?
            parts = filters[:term].split(" ")
            queries = []
            vals = {}

            parts.each_with_index do |part, i|
              vals[:"term_#{i}"] = "%#{part}%"

              queries << \
                "(virgo_users.email ILIKE :term_#{i} OR " +
                "virgo_users.first_name ILIKE :term_#{i} OR " +
                "virgo_users.last_name ILIKE :term_#{i} OR " +
                "virgo_users.username ILIKE :term_#{i} OR " +
                "virgo_users.byline ILIKE :term_#{i})"
            end

            query  = queries.join(" OR ")

            items = items.where(query, vals)
          end


          items
        }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
virgo-0.3.17 app/models/concerns/virgo/user/search.rb
virgo-0.3.16 app/models/concerns/virgo/user/search.rb
virgo-0.3.15 app/models/concerns/virgo/user/search.rb
virgo-0.3.14 app/models/concerns/virgo/user/search.rb
virgo-0.3.13 app/models/concerns/virgo/user/search.rb
virgo-0.3.12 app/models/concerns/virgo/user/search.rb
virgo-0.3.11 app/models/concerns/virgo/user/search.rb
virgo-0.3.10 app/models/concerns/virgo/user/search.rb
virgo-0.3.9 app/models/concerns/virgo/user/search.rb
virgo-0.3.8 app/models/concerns/virgo/user/search.rb
virgo-0.3.7 app/models/concerns/virgo/user/search.rb
virgo-0.3.6 app/models/concerns/virgo/user/search.rb
virgo-0.3.4 app/models/concerns/virgo/user/search.rb