Skip to content

Commit

Permalink
value defaults to '0' when sql statement is given but no param data i…
Browse files Browse the repository at this point in the history
…s avaiable
  • Loading branch information
Matt Zukowski committed May 17, 2010
1 parent c07150c commit 969fb25
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/list_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
# Have a look at the #filter_by docs for details on the kinds of options
# you can specify.
class ListFilter
# Adds sql constraints that will always be applied to the filter query, regardless of filter values.
# Example: "group = 'foo'". This constraint will be appended to the rest of the filter query using AND.
attr_accessor :constraints

# Initialize the ListFilter. Needs access to the current controller, so
# inside a controller action initialization will also look like this:
#
Expand All @@ -85,6 +89,7 @@ def initialize(controller)
@controller = controller

@sql = ''
@constraints = ''
@values = []
@values_hash = {}
end
Expand Down Expand Up @@ -125,8 +130,11 @@ def filter_by(field, options = {})
end
end

if val
unless sql.blank?
@sql << " #{'AND' unless @sql.blank?} #{sql}"
val = '0' if val.blank?
end
unless val.blank?
@values << val
@values_hash[field] = val
end
Expand All @@ -137,7 +145,9 @@ def filter_by(field, options = {})
# Book.find(:all, :conditions => @filter_by.conditions)
def conditions
return "1" if no_conditions?
["(#{@sql})"] + @values
sql = "(#{@sql})"
sql << "AND (#{@constraints})" unless @constraints.blank?
["(#{sql})"] + @values
end

# Returns true if the filter has no conditions (i.e. the generated SQL
Expand Down

0 comments on commit 969fb25

Please sign in to comment.