Sha256: 1ff2d57016fdb73a9393938c6b64a631c0ca4d40f65f5a4afa888b2813d3150e

Contents?: true

Size: 640 Bytes

Versions: 2

Compression:

Stored size: 640 Bytes

Contents

class ResourceSearchService
  attr_reader :search_query, :model_klass, :model_attributes

  def initialize(search_query, model_klass, model_attributes)
    @search_query = search_query

    @model_klass = model_klass
    @model_attributes = model_attributes
  end

  def run
    return [] if model_attributes.empty?

    query = model_attributes.map { |model_attribute| "#{model_attribute} LIKE ?" }.join(' OR ')
    query_arguments = model_attributes.map { "%#{search_query}%" }

    OpenStruct.new(
      resources: model_klass.where(query, *query_arguments),
      total: model_klass.where(query, *query_arguments).count
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simpleadmin-1.4.0 app/services/resource_search_service.rb
simpleadmin-1.3.0 app/services/resource_search_service.rb