Sha256: 0e79c1c8ea058a5f0560cca268e2e2255903f2e29951a707fe70b7bbe7ba1a38

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

module Services
  class BaseFinder < Services::Base
    def call(ids = [], conditions = {})
      ids, conditions = Array(ids), conditions.symbolize_keys
      special_conditions = conditions.extract!(:order, :limit, :page, :per_page)
      scope = object_class
        .select("DISTINCT #{object_class.table_name}.id")
        .order("#{object_class.table_name}.id")
      scope = scope.where(id: ids) unless ids.empty?

      scope = process(scope, conditions)

      scope = object_class.where(id: scope)
      special_conditions.each do |k, v|
        case k
        when :order
          order = if v == 'random'
            'RANDOM()'
          else
            "#{object_class.table_name}.#{v}"
          end
          scope = scope.order(order)
        when :limit
          scope = scope.limit(v)
        when :page
          scope = scope.page(v)
        when :per_page
          scope = scope.per(v)
        else
          raise ArgumentError, "Unexpected special condition: #{k}"
        end
      end
      scope
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
services-1.3.0 lib/services/base_finder.rb
services-1.2.0 lib/services/base_finder.rb
services-1.1.1 lib/services/base_finder.rb
services-1.1.0 lib/services/base_finder.rb
services-1.0.0 lib/services/base_finder.rb
services-0.4.0 lib/services/base_finder.rb