glebm/order_query

View on GitHub
lib/order_query/sql/column.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

module OrderQuery
  module SQL
    # A column in the given scope.
    class Column
      attr_reader :scope, :column

      def initialize(scope, column)
        @scope = scope
        @column = column
      end

      def column_name
        @column_name ||= begin
          sql = column.custom_sql
          if sql
            sql.respond_to?(:call) ? sql.call : sql
          else
            "#{connection.quote_table_name(scope.table_name)}."\
            "#{connection.quote_column_name(column.name)}"
          end
        end
      end

      def quote(value)
        connection.quote value
      end

      protected

      def connection
        scope.connection
      end
    end
  end
end