Sha256: b6c1cdb138e6d3a4ff273a8500de540ef595b84eb27324be5203e0351461b3c2

Contents?: true

Size: 871 Bytes

Versions: 6

Compression:

Stored size: 871 Bytes

Contents

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

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

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

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

    def associations
      all_associations - [query_info.model.name.tableize.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

6 entries across 6 versions & 1 rubygems

Version Path
search_cop-1.0.9 lib/search_cop/query_builder.rb
search_cop-1.0.8 lib/search_cop/query_builder.rb
search_cop-1.0.7 lib/search_cop/query_builder.rb
search_cop-1.0.6 lib/search_cop/query_builder.rb
search_cop-1.0.5 lib/search_cop/query_builder.rb
search_cop-1.0.4 lib/search_cop/query_builder.rb