Sha256: fd5c6f5ed8ab7165ee778c0c7faf7e298b04f6d46c57e5bff084dad6c94f7577

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module ActiveRecordSeek
  class Query

    include Concerns::InstanceVariableConcern

    delegate(*%w[ table_name arel_table reflect_on_association ], to: :model)
    attr_reader(*%w[ active_record_query ])

    def active_record_query=(new_active_record_query)
      instance_variable_reset(:@where_sql)
      @active_record_query = new_active_record_query
    end
    alias_method(:to_active_record_query, :active_record_query)

    def to_seek_query
      self
    end

    def model
      to_active_record_query.klass
    end

    def adapter_name
      model.connection.adapter_name
    end

    def arel_column(column_name)
      arel_table[column_name]
    end

    def merge_where_sql(other_query)
      to_active_record_query.where(other_query.to_seek_query.to_where_sql(enclose_with_parentheses: false))
    end

    def to_where_sql(enclose_with_parentheses: true)
      if !instance_variable_defined?(:@where_sql)
        @where_sql = active_record_query.reorder(nil).to_sql.split(" WHERE ", 2)[1].to_s.strip
      end
      return "" if @where_sql.blank?
      enclose_with_parentheses ? "(#{@where_sql})" : @where_sql
    end

    def has_where_sql?
      to_where_sql.present?
    end

    def apply(components)
      to_active_record_query.seek_or(self) do |this|
        components.group_by(&:namespace).each do |namespace, namespace_components|
          case namespace
          when "unscoped"
            namespace_components.each do |component|
              add_query { component.apply(self) }
            end
          else
            add_query do |namespace_query|
              namespace_components.each do |component|
                namespace_query = component.apply(namespace_query)
              end
              namespace_query
            end
          end
        end
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_record_seek-0.0.5 lib/active_record_seek/query.rb
active_record_seek-0.0.4 lib/active_record_seek/query.rb
active_record_seek-0.0.3 lib/active_record_seek/query.rb
active_record_seek-0.0.2 lib/active_record_seek/query.rb
active_record_seek-0.0.1 lib/active_record_seek/query.rb