Method: ActiveScaffold::Finder::ClassMethods#subquery_condition

Defined in:
lib/active_scaffold/finder.rb

#subquery_condition(column, sql, options, values) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/active_scaffold/finder.rb', line 140

def subquery_condition(column, sql, options, values)
  relation, *columns = options[:subquery]
  conditions = [columns.map { |search_sql| sql % {:search_sql => search_sql} }.join(' OR ')]
  conditions += values * columns.size if values.present?
  subquery = relation.where(conditions)
  subquery = subquery.select(relation.primary_key) if subquery.select_values.blank?

  conditions = [["#{options[:field] || column.field} IN (?)", options[:conditions]&.first].compact.join(' AND ')]
  conditions << subquery
  conditions.concat options[:conditions][1..-1] if options[:conditions]
  if column.association&.polymorphic?
    conditions[0] << " AND #{column.quoted_foreign_type} = ?"
    conditions << relation.base_class.sti_name
  end
  conditions
end