Skip to content

Commit

Permalink
added custom conditions - see user filter example
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Berkovich committed Feb 24, 2012
1 parent 8916fa1 commit 3113761
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
will_filter (3.1.3)
will_filter (3.1.4)
coffee-script
kaminari
rails (>= 3.1.0)
Expand Down
51 changes: 50 additions & 1 deletion app/models/will_filter/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def sql_conditions
all_sql_conditions = [""]
0.upto(size - 1) do |index|
condition = condition_at(index)
next if custom_condition?(condition)

sql_condition = condition.container.sql_condition

Expand Down Expand Up @@ -685,14 +686,62 @@ def joins
"INNER JOIN #{join_table_name} ON #{join_table_name}.id = #{table_name}.#{join_on_field}"
end
end


# overload this method to indicate which conditions are custom
def custom_conditions
[]
end

# overload this method to evaluate a custom condition on an object
def custom_condition_met?(condition, object)
false
end

def custom_condition?(condition)
custom_conditions.include?(condition.key)
end

def custom_conditions?
return if custom_conditions.empty?
conditions.each do |condition|
return true if custom_condition?(condition)
end
end

def process_custom_conditions(objects)
filtered = []
objects.each do |obj|
condition_flags = []

0.upto(size - 1) do |index|
condition = condition_at(index)
next unless custom_condition?(condition)
condition_flags << custom_condition_met?(condition, obj)
end

if condition_flags.size > 0
next if match.to_s == "all" and condition_flags.include?(false)
next unless condition_flags.include?(true)
end

filtered << obj
end
filtered
end

def results
@results ||= begin
handle_empty_filter!
recs = model_class.where(sql_conditions).order(order_clause)
inner_joins.each do |inner_join|
recs = recs.joins(association_name(inner_join))
end

if custom_conditions?
recs = process_custom_conditions(recs.all)
recs = Kaminari.paginate_array(recs)
end

recs = recs.page(page).per(per_page)
recs.wf_filter = self
recs
Expand Down
29 changes: 27 additions & 2 deletions test/dummy/app/models/user_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ def definition
defs = super
defs[:sex][:is] = :list
defs[:sex][:is_not] = :list
defs[:custom_condition] = {
:is => :list
}
defs
end

def value_options_for(criteria_key)
if criteria_key == :sex
def value_options_for(condition_key)
if condition_key == :sex
return ["male", "female"]
end

if condition_key == :custom_condition
return [["Third letter in the First Name is an 'a'", 'a'], ['Id is divisible by 3', '3']]
end

return []
end

Expand Down Expand Up @@ -54,4 +61,22 @@ def default_filter_if_empty
"male_only"
end

def custom_conditions
[:custom_condition]
end

def custom_condition_met?(condition, object)
if condition.key == :custom_condition
if condition.operator == :is
if (condition.container.value == 'a')
return (object.first_name[2..2] == 'a')
elsif (condition.container.value == '3')
return (object.id % 3 == 0)
end
end
end

return false
end

end

0 comments on commit 3113761

Please sign in to comment.