Permalink
Browse files

Add "any" option for conditions to join with "or" instead of "and"

  • Loading branch information...
binarylogic committed Sep 18, 2008
1 parent 76e572a commit fa5f3b78007ea7767569432f7785c5942dea3b12
View
@@ -1,3 +1,9 @@
+== 1.1.0 released 2008-09-18
+
+* Added the options :inner_spread and :outer_spread to the page_links helper. Also added various config options for setting page_links defaults.
+* Updated calculation methods to ignore :limit and :offset. AR returns 0 or nil on calculations that provide an offset.
+* Added support to allow for "any" of the conditions, instead of all of them. Joins conditions with "or" instead of "and". See Searchgasm::Conditions::Base or the readme
+
== 1.0.4 released 2008-09-18
* Fixed bugs when performing calculations and searches on has_many through relationships.
View
@@ -189,6 +189,16 @@ Don't need pagination, ordering, or any of the other options? Search with condit
Pass a conditions object right into ActiveRecord:
User.all(:conditions => conditions)
+
+== Match ANY or ALL of the conditions
+
+As you saw above, the nice thing about Searchgasm is it's integration with forms. I designed the "any" option so that forms can set this as well, just like a condition.
+
+ search = User.new_search(:conditions => {:age_gt => 18})
+ search.conditions.first_name_contains = "Ben"
+ search.conditions.any = true # can set this to "true" or "1" or "yes"
+ search.all # will join all conditions with "or" instead of "and"
+ # ... all operations above are available
== Scoped searching
@@ -66,6 +66,7 @@ def condition_names
def needed?(model_class, conditions) # :nodoc:
if conditions.is_a?(Hash)
+ return true if conditions[:any]
column_names = model_class.column_names
conditions.stringify_keys.keys.each do |condition|
return true unless column_names.include?(condition)
@@ -83,8 +84,24 @@ def initialize(init_conditions = {})
self.conditions = init_conditions
end
+ # Determines if we should join the conditions with "AND" or "OR".
+ #
+ # === Examples
+ #
+ # search.conditions.any = true # will join all conditions with "or", you can also set this to "true", "1", or "yes"
+ # search.conditions.any = false # will join all conditions with "and"
+ def any=(value)
+ associations.each { |association| association.any = value }
+ @any = value
+ end
+
+ def any # :nodoc:
+ any?
+ end
+
+ # Convenience method for determining if we should join the conditions with "AND" or "OR".
def any?
- any == true
+ @any == true || @any == "true" || @any == "1" || @any == "yes"
end
# A list of includes to use when searching, includes relationships
@@ -222,7 +239,7 @@ def add_klass_conditions!
end
def assert_valid_conditions(conditions)
- conditions.stringify_keys.fast_assert_valid_keys(self.class.condition_names + self.class.association_names)
+ conditions.stringify_keys.fast_assert_valid_keys(self.class.condition_names + self.class.association_names + ["any"])
end
def associations
@@ -66,8 +66,8 @@ def to_a
end
MAJOR = 1
- MINOR = 0
- TINY = 4
+ MINOR = 1
+ TINY = 0
# The current version as a Version instance
CURRENT = new(MAJOR, MINOR, TINY)
@@ -84,6 +84,11 @@ def test_scoping
def test_count
assert_equal 3, Account.count
assert_equal 3, Account.count(:limit => 1)
- assert_equal 3, Account.count(:limit => 1, :offset => 1)
+ assert_equal 0, Account.count(:limit => 1, :offset => 1) # not sure why AR doesn't ignore offset like it does for limit
+ search = Account.new_search
+ assert_equal 3, search.count
+ search.per_page = 10
+ search.page = 10
+ assert_equal 3, search.count
end
end
@@ -173,4 +173,16 @@ def test_searching
assert_equal 1, conditions.minimum('id')
assert_equal 4, conditions.sum('id')
end
+
+ def test_any
+ conditions = Account.new_conditions
+ conditions.name_contains = "Binary"
+ assert_equal Account.find(1, 3), conditions.all
+ conditions.id = 1
+ assert_equal [Account.find(1)], conditions.all
+ conditions.any = true
+ assert_equal Account.find(1, 3), conditions.all
+ conditions.any = false
+ assert_equal [Account.find(1)], conditions.all
+ end
end

0 comments on commit fa5f3b7

Please sign in to comment.