Sha256: dde56a8d6b2beeaa13a9b7ba2c1c096a72bed53815da0c559ab4f78e5939df8a

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Calagator
  class Venue < ActiveRecord::Base
    class SearchEngine
      class Sql < Struct.new(:query, :opts)
        def self.search(*args)
          new(*args).search
        end

        def search
          base.keywords.non_duplicates.with_wifi.in_business.order.limit.scope.to_a
        end

        protected

        attr_accessor :scope

        def base
          column_names = Venue.column_names.map { |name| "venues.#{name}" }
          @scope = Venue.all
                        .group(column_names)
                        .joins("LEFT OUTER JOIN taggings on taggings.taggable_id = venues.id AND taggings.taggable_type LIKE '%Venue'")
                        .joins('LEFT OUTER JOIN tags ON tags.id = taggings.tag_id')
          self
        end

        def keywords
          query_conditions = @scope
                             .where(['LOWER(title) LIKE ?', "%#{query.downcase}%"])
                             .where(['LOWER(description) LIKE ?', "%#{query.downcase}%"])

          query_conditions = query.split.inject(query_conditions) do |query_conditions, keyword|
            query_conditions.where(['LOWER(tags.name) = ?', keyword])
          end

          @scope = @scope.where(query_conditions.where_values.join(' OR '))
          self
        end

        def non_duplicates
          @scope = @scope.non_duplicates
          self
        end

        def order
          @scope = @scope.order('LOWER(venues.title) ASC')
          self
        end

        def limit
          @scope = @scope.limit(opts[:limit] || 50)
          self
        end

        def with_wifi
          @scope = @scope.with_public_wifi if opts[:wifi]
          self
        end

        def in_business
          @scope = @scope.in_business unless opts[:include_closed]
          self
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
calagator-1.1.0 app/models/calagator/venue/search_engine/sql.rb