Sha256: 0f4fadddf6f8f87e77a919793bcbb9cce4e2e9c65cf9e9e6d1455c4af917b590

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

module Inquery
  class Query::Chainable < Query
    include Inquery::Mixins::RelationValidation

    # Allows using this class as an AR scope.
    def self.call(*args)
      return new(*args).call
    end

    def call(*args)
      fail args.inspect
    end

    attr_reader :relation

    def initialize(*args)
      relation, params = parse_init_args(*args)
      @relation = validate_relation!(relation)
      super(params)
    end

    # Override the connection method to (re-)use the connection of the relation
    def connection
      @relation.connection
    end

    private

    def parse_init_args(*args)
      # new(relation)
      if (args[0].is_a?(ActiveRecord::Relation) || args[0].class < ActiveRecord::Base) && args[1].nil?
        relation = args[0]
        params = {}

      # new(params)
      elsif args[0].is_a?(Hash) && args[1].nil?
        relation = nil
        params = args[0]

      # new(relation, params)
      elsif (args[0].is_a?(ActiveRecord::Relation) || args[0].class < ActiveRecord::Base) && args[1].is_a?(Hash)
        relation = args[0]
        params = args[1]

      # new()
      elsif args.empty?
        relation = nil
        params = {}

      # Unknown
      else
        fail Inquery::Exceptions::UnknownCallSignature, "Unknown call signature for the query constructor: #{args.collect(&:class)}."
      end

      return relation, params
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
inquery-1.0.11 lib/inquery/query/chainable.rb
inquery-1.0.10 lib/inquery/query/chainable.rb
inquery-1.0.9 lib/inquery/query/chainable.rb
inquery-1.0.8 lib/inquery/query/chainable.rb
inquery-1.0.7 lib/inquery/query/chainable.rb
inquery-1.0.6 lib/inquery/query/chainable.rb
inquery-1.0.5 lib/inquery/query/chainable.rb
inquery-1.0.4 lib/inquery/query/chainable.rb
inquery-1.0.3 lib/inquery/query/chainable.rb
inquery-1.0.2 lib/inquery/query/chainable.rb