Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.76 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # PATCH HERE IS NOT OFFICIAL.
  2.     # Perform RFM find using complex boolean logic (multiple value options for a single field)
  3.     # Mimics creation of multiple find requests for "or" logic
  4.     # Use: rfm_layout_object.query({'fieldOne'=>['val1','val2','val3'], 'fieldTwo'=>'someValue', ...})
  5.     def query(hash_or_recid, options = {})
  6.       if hash_or_recid.kind_of? Hash
  7.         get_records('-findquery', assemble_query(hash_or_recid), options)
  8.       else
  9.         get_records('-find', {'-recid' => hash_or_recid.to_s}, options)
  10.       end
  11.     end
  12.  
  13.     # Build ruby params to send to -query action via RFM
  14.     def assemble_query(query_hash)
  15.       key_values, query_map = build_key_values(query_hash)
  16.       key_values.merge("-query"=>query_translate(array_mix(query_map)))
  17.     end
  18.  
  19.     # Build key-value definitions and query map  '-q1...'
  20.     def build_key_values(qh)
  21.       key_values = {}
  22.       query_map = []
  23.       counter = 0
  24.       qh.each_with_index do |ha,i|
  25.         ha[1] = ha[1].to_a
  26.         query_tag = []
  27.         ha[1].each do |v|
  28.           key_values["-q#{counter}"] = ha[0]
  29.           key_values["-q#{counter}.value"] = v
  30.           query_tag << "q#{counter}"
  31.           counter += 1
  32.         end
  33.         query_map << query_tag
  34.       end
  35.       return key_values, query_map
  36.     end
  37.  
  38.     # Build query request logic for FMP requests  '-query...'
  39.     def array_mix(ary, line=[], rslt=[])
  40.       ary[0].to_a.each_with_index do |v,i|
  41.         array_mix(ary[1,ary.size], (line + [v]), rslt)
  42.         rslt << (line + [v]) if ary.size == 1
  43.       end
  44.       return rslt
  45.     end
  46.  
  47.     # Translate query request logic to string
  48.     def query_translate(mixed_ary)
  49.       rslt = ""
  50.       sub = mixed_ary.collect {|a| "(#{a.join(',')})"}
  51.       sub.join(";")
  52.     end