Created
November 7, 2014 14:22
-
-
Save manlycode/3bec474cf97c6a50c772 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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