diff --git a/Gemfile.lock b/Gemfile.lock index 9f01039..d5a6e1d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - will_filter (3.1.3) + will_filter (3.1.4) coffee-script kaminari rails (>= 3.1.0) diff --git a/app/models/will_filter/filter.rb b/app/models/will_filter/filter.rb index ca9f5be..88d89b1 100644 --- a/app/models/will_filter/filter.rb +++ b/app/models/will_filter/filter.rb @@ -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 @@ -685,7 +686,49 @@ 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! @@ -693,6 +736,12 @@ def results 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 diff --git a/test/dummy/app/models/user_filter.rb b/test/dummy/app/models/user_filter.rb index 4c2ac1e..be919cc 100755 --- a/test/dummy/app/models/user_filter.rb +++ b/test/dummy/app/models/user_filter.rb @@ -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 @@ -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