module ESA::Filters::AccountableFilter

Public Class Methods

make_fragments(type, accountable) click to toggle source
# File lib/esa/filters/accountable_filter.rb, line 16
def self.make_fragments(type, accountable)
  if accountable.is_a? ActiveRecord::Relation
    [accountable.select("`#{accountable.table_name}`.`#{accountable.primary_key}` AS id, '#{type}' AS type").to_sql.squish]
  elsif accountable.is_a? ActiveRecord::Base
    ["SELECT #{accountable.id} AS id, '#{type}' AS type"]
  elsif accountable.is_a? Integer
    ["SELECT #{accountable} AS id, '#{type}' AS type"]
  elsif accountable.respond_to? :each
    accountable.map{|a| make_fragments(type, a)}.flatten
  else
    []
  end
end
make_union_query(definitions = {}) click to toggle source
# File lib/esa/filters/accountable_filter.rb, line 4
def self.make_union_query(definitions = {})
  fragments = definitions.map do |type,accountable|
    make_fragments(type, accountable)
  end.flatten

  if fragments.count > 0
    fragments.join(' UNION ')
  else
    "SELECT -1 AS id, 'Nothing' AS type"
  end
end