Skip to content

Instantly share code, notes, and snippets.

@manlycode
Created November 7, 2014 14:22
Show Gist options
  • Save manlycode/3bec474cf97c6a50c772 to your computer and use it in GitHub Desktop.
Save manlycode/3bec474cf97c6a50c772 to your computer and use it in GitHub Desktop.
class Filter
attr_reader :rows
def initialize(filtered_class)
@filtered_class = filtered_class
@rows = []
end
def add_row(row)
@rows << row
end
def results
@filtered_class.where(row_to_arel(@rows[0]))
end
private
def table
@table ||= Arel::Table.new(@filtered_class.table_name)
end
def row_to_arel(row = {})
return if row.nil?
columns = row.collect do |k, v|
column = k
operator, value = v.flatten
table[column].send(operator, value)
end
columns.reduce(:and)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment