app/models/calagator/event/search_engine/sql.rb in calagator-1.1.0 vs app/models/calagator/event/search_engine/sql.rb in calagator-2.0.0.pre.0

- old
+ new

@@ -1,7 +1,9 @@ +# frozen_string_literal: true + module Calagator - class Event < ActiveRecord::Base + class Event < ApplicationRecord class SearchEngine class Sql < Struct.new(:query, :opts) # Return an Array of non-duplicate Event instances matching the search +query+.. # # Options: @@ -57,20 +59,14 @@ .references(:venues, :tags) self end def keywords - query_conditions = @scope.where('LOWER(events.title) LIKE ?', "%#{query.downcase}%") - .where('LOWER(events.description) LIKE ?', "%#{query.downcase}%") - - query_conditions = query.split.inject(query_conditions) do |query_conditions, keyword| - like = "%#{keyword.downcase}%" - query_conditions - .where(['LOWER(events.url) LIKE ?', like]) - .where(['LOWER(tags.name) = ?', keyword]) - end - @scope = @scope.where(query_conditions.where_values.join(' OR ')) + @scope = @scope.where('LOWER(events.title) LIKE ?', "%#{query.downcase}%") + .or(@scope.where('LOWER(events.description) LIKE ?', "%#{query.downcase}%")) + .or(@scope.where(['LOWER(events.url) LIKE ?', "%#{query.downcase}%"])) + .or(@scope.where(['LOWER(tags.name) = ?', query])) self end def order order = case opts[:order].try(:to_sym) @@ -79,10 +75,10 @@ when :location, :venue 'LOWER(venues.title) ASC' else 'events.start_time DESC' end - @scope = @scope.order(order) + @scope = @scope.order(Arel.sql(order)) self end def limit limit = opts.fetch(:limit, 50)