searchgasm 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.9.6. Fix bug when instantiating with nil options
2
+
1
3
  v0.9.5. Enhanced searching with conditions only. Updated read me to include example on adding your own conditions.
2
4
 
3
5
  v0.9.4. Cleaned up search methods, removed reset! method for base and conditions.
data/README.mdown CHANGED
@@ -273,6 +273,9 @@ I want to use it, so let's add it:
273
273
  end
274
274
  end
275
275
 
276
+ # You can return an array or a string. NOT a hash, because all of these conditions
277
+ # need to eventually get merged together. The array or string can be anything you would put in
278
+ # the :conditions option for ActiveRecord::Base.find()
276
279
  def to_conditions(value)
277
280
  ["#{quoted_table_name}.#{quoted_column_name} SOUNDS LIKE ?", value]
278
281
  end
@@ -23,7 +23,7 @@ module BinaryLogic
23
23
  end
24
24
 
25
25
  def build_conditions(values = {}, &block)
26
- conditions = searchgasm_conditions({})
26
+ conditions = searchgasm_conditions
27
27
  conditions.protect = true
28
28
  conditions.value = values
29
29
  yield conditions if block_given?
@@ -37,8 +37,9 @@ module BinaryLogic
37
37
  end
38
38
 
39
39
  def build_search(options = {}, &block)
40
- options[:protect] = true
41
- search = searchgasm_searcher(options)
40
+ search = searchgasm_searcher
41
+ search.protect = true
42
+ search.options = options
42
43
  yield search if block_given?
43
44
  search
44
45
  end
@@ -50,15 +51,16 @@ module BinaryLogic
50
51
  end
51
52
 
52
53
  private
53
- def sanitize_options_with_searchgasm(options)
54
+ def sanitize_options_with_searchgasm(options = {})
55
+ return options unless BinaryLogic::Searchgasm::Search::Base.needed?(self, options)
54
56
  searchgasm_searcher(options).sanitize
55
57
  end
56
58
 
57
- def searchgasm_conditions(options)
59
+ def searchgasm_conditions(options = {})
58
60
  BinaryLogic::Searchgasm::Search::Conditions.new(self, options)
59
61
  end
60
62
 
61
- def searchgasm_searcher(options)
63
+ def searchgasm_searcher(options = {})
62
64
  BinaryLogic::Searchgasm::Search::Base.new(self, options)
63
65
  end
64
66
  end
@@ -13,6 +13,14 @@ module BinaryLogic
13
13
  attr_reader :conditions, :protect
14
14
  attr_writer :options
15
15
 
16
+ def self.needed?(klass, options)
17
+ SPECIAL_FIND_OPTIONS.each do |option|
18
+ return true if options.symbolize_keys.keys.include?(option)
19
+ end
20
+
21
+ Conditions.needed?(klass, options[:conditions])
22
+ end
23
+
16
24
  def initialize(klass, options = {})
17
25
  self.klass = klass
18
26
  self.conditions = Conditions.new(klass)
@@ -21,13 +21,13 @@ module BinaryLogic
21
21
  subs = []
22
22
 
23
23
  search_parts = value.gsub(/,/, " ").split(/ /).collect { |word| word.downcase.gsub(/[^[:alnum:]]/, ''); }.uniq.select { |word| !BLACKLISTED_WORDS.include?(word.downcase) && !word.blank? }
24
+ return if search_parts.blank?
25
+
24
26
  search_parts.each do |search_part|
25
27
  strs << "#{quoted_table_name}.#{quoted_column_name} LIKE ?"
26
28
  subs << "%#{search_part}%"
27
29
  end
28
30
 
29
- return if strs.blank?
30
-
31
31
  [strs.join(" AND "), *subs]
32
32
  end
33
33
  end
@@ -15,6 +15,16 @@ module BinaryLogic
15
15
  def conditions
16
16
  @@conditions ||= []
17
17
  end
18
+
19
+ def needed?(klass, conditions)
20
+ if conditions.is_a?(Hash)
21
+ conditions.stringify_keys.keys.each do |condition|
22
+ return true unless klass.column_names.include?(condition)
23
+ end
24
+ end
25
+
26
+ false
27
+ end
18
28
  end
19
29
 
20
30
  def initialize(klass, init_values = {})
@@ -68,7 +68,7 @@ module BinaryLogic
68
68
 
69
69
  MAJOR = 0
70
70
  MINOR = 9
71
- TINY = 5
71
+ TINY = 6
72
72
 
73
73
  # The current version as a Version instance
74
74
  CURRENT = new(MAJOR, MINOR, TINY)
data/searchgasm.gemspec CHANGED
@@ -1,11 +1,11 @@
1
1
 
2
- # Gem::Specification for Searchgasm-0.9.5
2
+ # Gem::Specification for Searchgasm-0.9.6
3
3
  # Originally generated by Echoe
4
4
 
5
5
  --- !ruby/object:Gem::Specification
6
6
  name: searchgasm
7
7
  version: !ruby/object:Gem::Version
8
- version: 0.9.5
8
+ version: 0.9.6
9
9
  platform: ruby
10
10
  authors:
11
11
  - Ben Johnson of Binary Logic
@@ -16,6 +16,9 @@ class TestActiveRecordBase < Test::Unit::TestCase
16
16
  assert_nothing_raised { Account.all }
17
17
  assert_nothing_raised { Account.first }
18
18
  assert_nothing_raised { Account.find(:all) }
19
+ assert_nothing_raised { Account.find(:all, :conditions => {:name => "Ben"}) }
20
+ assert_nothing_raised { Account.find(:all, :conditions => ["name = ?", "Ben"]) }
21
+ assert_nothing_raised { Account.find(:all, :conditions => "name = 'Ben'") }
19
22
  assert_nothing_raised { Account.find(:first) }
20
23
  assert_nothing_raised { Account.find(:all, nil) }
21
24
  assert_nothing_raised { Account.find(:all, {}) }
@@ -30,6 +33,9 @@ class TestActiveRecordBase < Test::Unit::TestCase
30
33
  end
31
34
 
32
35
  def test_build_search
36
+ search = Account.new_search
37
+ assert_kind_of BinaryLogic::Searchgasm::Search::Base, search
38
+
33
39
  search = Account.build_search(:conditions => {:name_keywords => "awesome"}, :page => 2, :per_page => 15)
34
40
  assert_kind_of BinaryLogic::Searchgasm::Search::Base, search
35
41
  assert_equal Account, search.klass
@@ -41,6 +47,19 @@ class TestActiveRecordBase < Test::Unit::TestCase
41
47
  assert_equal Account, search.klass
42
48
  end
43
49
 
50
+ def test_build_conditions
51
+ search = Account.new_conditions
52
+ assert_kind_of BinaryLogic::Searchgasm::Search::Conditions, search
53
+
54
+ search = Account.build_conditions(:name_keywords => "awesome")
55
+ assert_kind_of BinaryLogic::Searchgasm::Search::Conditions, search
56
+ assert_equal Account, search.klass
57
+ assert_equal "awesome", search.name_keywords
58
+
59
+ search = Account.new_conditions(:name_keywords => "awesome")
60
+ assert_equal Account, search.klass
61
+ end
62
+
44
63
  def test_searching
45
64
  assert_equal Account.find(1, 3), Account.all(:conditions => {:name_contains => "Binary"})
46
65
  assert_equal [Account.find(1)], Account.all(:conditions => {:name_contains => "Binary", :users => {:first_name_starts_with => "Ben"}})
@@ -12,7 +12,16 @@ class TestSearchgasmBase < Test::Unit::TestCase
12
12
  teardown_db
13
13
  end
14
14
 
15
+ def test_needed
16
+ assert BinaryLogic::Searchgasm::Search::Base.needed?(Account, :page => 2, :conditions => {:name => "Ben"})
17
+ assert !BinaryLogic::Searchgasm::Search::Base.needed?(Account, :conditions => {:name => "Ben"})
18
+ assert BinaryLogic::Searchgasm::Search::Base.needed?(Account, :limit => 2, :conditions => {:name_contains => "Ben"})
19
+ assert !BinaryLogic::Searchgasm::Search::Base.needed?(Account, :limit => 2)
20
+ assert BinaryLogic::Searchgasm::Search::Base.needed?(Account, :per_page => 2)
21
+ end
22
+
15
23
  def test_initialize
24
+ assert_nothing_raised { BinaryLogic::Searchgasm::Search::Base.new(Account) }
16
25
  search = BinaryLogic::Searchgasm::Search::Base.new(Account, :conditions => {:name_like => "binary"}, :page => 2, :limit => 10, :readonly => true)
17
26
  assert_equal Account, search.klass
18
27
  assert_equal "binary", search.conditions.name_like
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchgasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic