Class: Xsdk::Scaffold::Rails::BaseSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/xsdk/scaffold/rails/base_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(params, clazz) ⇒ BaseSearch

Returns a new instance of BaseSearch.



6
7
8
9
10
11
12
# File 'lib/xsdk/scaffold/rails/base_search.rb', line 6

def initialize(params, clazz)
  @clazz = clazz
  @query = "%#{params[:query]}%"
  @limit = params[:limit].try(:to_i) || 5
  @columns = params[:columns] || %w[id]
  @source = @clazz.connection.quote_column_name(params[:source])
end

Instance Method Details

#as_jsonObject



14
15
16
# File 'lib/xsdk/scaffold/rails/base_search.rb', line 14

def as_json(*)
  search.as_json(only: @columns, methods: @columns)
end

#searchObject



18
19
20
# File 'lib/xsdk/scaffold/rails/base_search.rb', line 18

def search
  @clazz.where(" #{@source}::text  ILIKE ? ", @query).limit(@limit)
end