Method: Zena::Use::QueryNode::Compiler#get_scope_index_field

Defined in:
lib/zena/use/query_node.rb

#get_scope_index_field(field_name) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/zena/use/query_node.rb', line 178

def get_scope_index_field(field_name)
  return nil if @query.main_class.real_class.column_names.include?(field_name)
  # 1. Try scope index
  klass = @query.main_class
  if index_model = klass.kind_of?(VirtualClass) ? klass.idx_class : nil
    index_model = Zena.resolve_const(index_model) rescue NilClass
    if index_model < Zena::Use::ScopeIndex::IndexMethods && index_model.column_names.include?(field_name)
      table_to_use = add_key_value_table('scope_index', index_model.table_name) do |tbl_name|
        # This block is only executed once (ON clause)
        "#{table('nodes')}.id = #{tbl_name}.node_id"
      end
      "#{table_to_use}.#{field_name}"
    else
      # invalid field_name: ignore
      nil
    end
  else
    nil
  end
end