Sha256: 756a856fdf69698a0b6872afdd218b4e69487a20a8760ee907276bfde1495e72

Contents?: true

Size: 938 Bytes

Versions: 3

Compression:

Stored size: 938 Bytes

Contents

module SearchCop
  class QueryBuilder
    attr_accessor :query_info, :scope, :sql

    def initialize(model, query, scope, query_options)
      self.scope = scope
      self.query_info = QueryInfo.new(model, scope)

      arel = SearchCop::Parser.parse(query, query_info, query_options).optimize!

      self.sql = SearchCop::Visitors::Visitor.new(model.connection).visit(arel)
    end

    def associations
      all_associations - [query_info.model.name.tableize.to_sym] - [query_info.model.table_name.to_sym]
    end

    private

    def all_associations
      scope.reflection.attributes.values.flatten.collect { |column| association_for column.split(".").first }.uniq
    end

    def association_for(column)
      alias_value = scope.reflection.aliases[column]

      association = alias_value.respond_to?(:table_name) ? alias_value.table_name : alias_value
      association ||= column

      association.to_sym
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
search_cop-1.4.0 lib/search_cop/query_builder.rb
search_cop-1.3.0 lib/search_cop/query_builder.rb
search_cop-1.2.3 lib/search_cop/query_builder.rb