Method: Zena::Use::QueryNode::Compiler#resolve_scope_idx_fields

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

#resolve_scope_idx_fields(arg1, arg2) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/zena/use/query_node.rb', line 347

def resolve_scope_idx_fields(arg1, arg2)
  if arg1.first == :function
    # contact.log_at.year
    # arg1 = [:function, [:field, "tag"], [:method, "created_at"]]
    # arg2 = [:method, "year"]
    class_name = arg1[1][1]
    field_name = arg1[2][1]
    function  = arg2
  elsif arg1[0] == :field && arg2[0] == :method
    # contact.log_at  or  log_at.year
    # arg1 = [:field, "contact"]
    class_name = arg1[1]
    # arg2 = [:method, "name"]
    field_name = arg2[1]
    function   = nil
  else
    return [arg1, arg2]
  end

  scope_idx_field = "#{class_name}_#{field_name}"
  if fld = get_scope_index_field(scope_idx_field)
    return [[:idx_field, fld], function]
  else
    # 2. Try to use relation as scope filter 'hot.title = "xxx"', 'hot.title = "xxx"'
    # Multiple links with hot.id = 334 and hot2.id = 323...
    rel = class_name.sub(/\d$/,'')

    source_kpath = @query.main_class.kpath

    if rel = RelationProxy.find_by_role(rel.singularize, source_kpath)
      table_to_use = add_key_value_table('jnode', 'nodes', class_name) do |tbl_name|
        # This block is only executed once per relation name (once for 'hot', once for 'hot2')
        # TODO: Can we remove this ?
        distinct!
        lnk = add_key_value_table('links', 'links', class_name) do |lt|
          "#{lt}.#{rel.link_side} = #{table(main_table)}.id AND #{lt}.relation_id = #{rel.id}"
        end
        
        %Q{#{tbl_name}.id = #{lnk}.#{rel.other_side}}
      end
      
      # Temporarily move to the remote class
      tbl_alias_bak = context[:table_alias]
      context[:table_alias] = table_to_use
      main_class_bak = @query.main_class
      
        @query.main_class = rel.other_vclass
        fld = process_field(field_name)
        
      # Move back
      @query.main_class = main_class_bak
      context[:table_alias] = tbl_alias_bak
      if fld
        [[:idx_field, fld], function]
      else
        nil
      end
    else
      # not a scope index field
      return [arg1, arg2]
    end
  end
end