searchgasm 0.9.6 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/CHANGELOG +2 -0
  2. data/Manifest +34 -21
  3. data/{README.mdown → README.rdoc} +96 -63
  4. data/Rakefile +1 -1
  5. data/examples/README.rdoc +4 -0
  6. data/lib/searchgasm/active_record/associations.rb +40 -42
  7. data/lib/searchgasm/active_record/base.rb +75 -61
  8. data/lib/searchgasm/condition/base.rb +127 -0
  9. data/lib/searchgasm/condition/begins_with.rb +20 -0
  10. data/lib/searchgasm/condition/child_of.rb +11 -0
  11. data/lib/searchgasm/condition/contains.rb +20 -0
  12. data/lib/searchgasm/condition/descendant_of.rb +24 -0
  13. data/lib/searchgasm/condition/does_not_equal.rb +28 -0
  14. data/lib/searchgasm/condition/ends_with.rb +20 -0
  15. data/lib/searchgasm/condition/equals.rb +20 -0
  16. data/lib/searchgasm/condition/greater_than.rb +25 -0
  17. data/lib/searchgasm/condition/greater_than_or_equal_to.rb +20 -0
  18. data/lib/searchgasm/condition/inclusive_descendant_of.rb +13 -0
  19. data/lib/searchgasm/condition/keywords.rb +33 -0
  20. data/lib/searchgasm/condition/less_than.rb +25 -0
  21. data/lib/searchgasm/condition/less_than_or_equal_to.rb +20 -0
  22. data/lib/searchgasm/condition/sibling_of.rb +16 -0
  23. data/lib/searchgasm/condition/tree.rb +16 -0
  24. data/lib/searchgasm/conditions/base.rb +221 -0
  25. data/lib/searchgasm/conditions/protection.rb +30 -0
  26. data/lib/searchgasm/config.rb +137 -0
  27. data/lib/searchgasm/helpers/form_helper.rb +159 -0
  28. data/lib/searchgasm/helpers/search_helper.rb +178 -0
  29. data/lib/searchgasm/helpers/utilities_helper.rb +125 -0
  30. data/lib/searchgasm/search/base.rb +73 -179
  31. data/lib/searchgasm/search/conditions.rb +42 -166
  32. data/lib/searchgasm/search/ordering.rb +149 -0
  33. data/lib/searchgasm/search/pagination.rb +69 -0
  34. data/lib/searchgasm/search/protection.rb +61 -0
  35. data/lib/searchgasm/utilities.rb +30 -0
  36. data/lib/searchgasm/version.rb +44 -47
  37. data/lib/searchgasm.rb +57 -21
  38. data/searchgasm.gemspec +71 -46
  39. data/test/test_active_record_associations.rb +1 -1
  40. data/test/test_active_record_base.rb +4 -4
  41. data/test/test_condition.rb +143 -0
  42. data/test/{test_searchgasm_conditions.rb → test_conditions_base.rb} +43 -33
  43. data/test/test_search_base.rb +189 -0
  44. data/test/test_search_ordering.rb +91 -0
  45. data/test/test_search_pagination.rb +56 -0
  46. data/test/test_search_protection.rb +35 -0
  47. metadata +70 -45
  48. data/lib/searchgasm/search/condition.rb +0 -105
  49. data/lib/searchgasm/search/condition_types/begins_with_condition.rb +0 -26
  50. data/lib/searchgasm/search/condition_types/child_of_condition.rb +0 -17
  51. data/lib/searchgasm/search/condition_types/contains_condition.rb +0 -26
  52. data/lib/searchgasm/search/condition_types/descendant_of_condition.rb +0 -30
  53. data/lib/searchgasm/search/condition_types/does_not_equal_condition.rb +0 -34
  54. data/lib/searchgasm/search/condition_types/ends_with_condition.rb +0 -26
  55. data/lib/searchgasm/search/condition_types/equals_condition.rb +0 -26
  56. data/lib/searchgasm/search/condition_types/greater_than_condition.rb +0 -31
  57. data/lib/searchgasm/search/condition_types/greater_than_or_equal_to_condition.rb +0 -26
  58. data/lib/searchgasm/search/condition_types/inclusive_descendant_of_condition.rb +0 -19
  59. data/lib/searchgasm/search/condition_types/keywords_condition.rb +0 -39
  60. data/lib/searchgasm/search/condition_types/less_than_condition.rb +0 -31
  61. data/lib/searchgasm/search/condition_types/less_than_or_equal_to_condition.rb +0 -26
  62. data/lib/searchgasm/search/condition_types/sibling_of_condition.rb +0 -22
  63. data/lib/searchgasm/search/condition_types/tree_condition.rb +0 -20
  64. data/lib/searchgasm/search/utilities.rb +0 -34
  65. data/test/test_searchgasm_base.rb +0 -185
  66. data/test/test_searchgasm_condition_types.rb +0 -143
data/lib/searchgasm.rb CHANGED
@@ -1,31 +1,67 @@
1
1
  require "active_record"
2
+ require "active_support"
2
3
 
4
+ # Utilties
5
+ require "searchgasm/version"
6
+ require "searchgasm/config"
7
+ require "searchgasm/utilities"
8
+
9
+ # ActiveRecord
3
10
  require "searchgasm/active_record/base"
4
11
  require "searchgasm/active_record/associations"
5
12
 
6
- require "searchgasm/version"
7
- require "searchgasm/search/utilities"
8
- require "searchgasm/search/condition"
13
+ # Search
14
+ require "searchgasm/search/ordering"
15
+ require "searchgasm/search/pagination"
9
16
  require "searchgasm/search/conditions"
10
17
  require "searchgasm/search/base"
18
+ require "searchgasm/search/protection"
19
+
20
+ # Conditions
21
+ require "searchgasm/conditions/protection"
22
+ require "searchgasm/conditions/base"
11
23
 
12
- # Regular conidtion types
13
- require "searchgasm/search/condition_types/begins_with_condition"
14
- require "searchgasm/search/condition_types/contains_condition"
15
- require "searchgasm/search/condition_types/does_not_equal_condition"
16
- require "searchgasm/search/condition_types/ends_with_condition"
17
- require "searchgasm/search/condition_types/equals_condition"
18
- require "searchgasm/search/condition_types/greater_than_condition"
19
- require "searchgasm/search/condition_types/greater_than_or_equal_to_condition"
20
- require "searchgasm/search/condition_types/keywords_condition"
21
- require "searchgasm/search/condition_types/less_than_condition"
22
- require "searchgasm/search/condition_types/less_than_or_equal_to_condition"
24
+ # Condition
25
+ require "searchgasm/condition/base"
26
+ require "searchgasm/condition/begins_with"
27
+ require "searchgasm/condition/contains"
28
+ require "searchgasm/condition/does_not_equal"
29
+ require "searchgasm/condition/ends_with"
30
+ require "searchgasm/condition/equals"
31
+ require "searchgasm/condition/greater_than"
32
+ require "searchgasm/condition/greater_than_or_equal_to"
33
+ require "searchgasm/condition/keywords"
34
+ require "searchgasm/condition/less_than"
35
+ require "searchgasm/condition/less_than_or_equal_to"
36
+ require "searchgasm/condition/tree"
37
+ require "searchgasm/condition/child_of"
38
+ require "searchgasm/condition/descendant_of"
39
+ require "searchgasm/condition/inclusive_descendant_of"
40
+ require "searchgasm/condition/sibling_of"
23
41
 
24
- # Tree condition types
25
- require "searchgasm/search/condition_types/tree_condition"
26
- require "searchgasm/search/condition_types/child_of_condition"
27
- require "searchgasm/search/condition_types/descendant_of_condition"
28
- require "searchgasm/search/condition_types/inclusive_descendant_of_condition"
29
- require "searchgasm/search/condition_types/sibling_of_condition"
42
+ # Helpers
43
+ require "searchgasm/helpers/utilities_helper"
44
+ require "searchgasm/helpers/form_helper"
45
+ require "searchgasm/helpers/search_helper"
30
46
 
31
- Searchgasm = BinaryLogic::Searchgasm::Search
47
+ # Lets do it!
48
+ module Searchgasm
49
+ module Search
50
+ class Base
51
+ include Conditions
52
+ include Ordering
53
+ include Pagination
54
+ include Protection
55
+ end
56
+ end
57
+
58
+ module Conditions
59
+ class Base
60
+ include Protection
61
+ end
62
+
63
+ [:begins_with, :child_of, :contains, :descendant_of, :does_not_equal, :ends_with, :equals, :greater_than, :greater_than_or_equal_to, :inclusive_descendant_of, :keywords, :less_than, :less_than_or_equal_to, :sibling_of].each do |condition|
64
+ Base.register_condition("Searchgasm::Condition::#{condition.to_s.camelize}".constantize)
65
+ end
66
+ end
67
+ end
data/searchgasm.gemspec CHANGED
@@ -1,18 +1,18 @@
1
1
 
2
- # Gem::Specification for Searchgasm-0.9.6
2
+ # Gem::Specification for Searchgasm-0.9.7
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.6
8
+ version: 0.9.7
9
9
  platform: ruby
10
10
  authors:
11
11
  - Ben Johnson of Binary Logic
12
12
  autorequire:
13
13
  bindir: bin
14
14
 
15
- date: 2008-09-03 00:00:00 -04:00
15
+ date: 2008-09-06 00:00:00 -04:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -55,58 +55,77 @@ extra_rdoc_files:
55
55
  - CHANGELOG
56
56
  - lib/searchgasm/active_record/associations.rb
57
57
  - lib/searchgasm/active_record/base.rb
58
+ - lib/searchgasm/condition/base.rb
59
+ - lib/searchgasm/condition/begins_with.rb
60
+ - lib/searchgasm/condition/child_of.rb
61
+ - lib/searchgasm/condition/contains.rb
62
+ - lib/searchgasm/condition/descendant_of.rb
63
+ - lib/searchgasm/condition/does_not_equal.rb
64
+ - lib/searchgasm/condition/ends_with.rb
65
+ - lib/searchgasm/condition/equals.rb
66
+ - lib/searchgasm/condition/greater_than.rb
67
+ - lib/searchgasm/condition/greater_than_or_equal_to.rb
68
+ - lib/searchgasm/condition/inclusive_descendant_of.rb
69
+ - lib/searchgasm/condition/keywords.rb
70
+ - lib/searchgasm/condition/less_than.rb
71
+ - lib/searchgasm/condition/less_than_or_equal_to.rb
72
+ - lib/searchgasm/condition/sibling_of.rb
73
+ - lib/searchgasm/condition/tree.rb
74
+ - lib/searchgasm/conditions/base.rb
75
+ - lib/searchgasm/conditions/protection.rb
76
+ - lib/searchgasm/config.rb
77
+ - lib/searchgasm/helpers/form_helper.rb
78
+ - lib/searchgasm/helpers/search_helper.rb
79
+ - lib/searchgasm/helpers/utilities_helper.rb
58
80
  - lib/searchgasm/search/base.rb
59
- - lib/searchgasm/search/condition.rb
60
- - lib/searchgasm/search/condition_types/begins_with_condition.rb
61
- - lib/searchgasm/search/condition_types/child_of_condition.rb
62
- - lib/searchgasm/search/condition_types/contains_condition.rb
63
- - lib/searchgasm/search/condition_types/descendant_of_condition.rb
64
- - lib/searchgasm/search/condition_types/does_not_equal_condition.rb
65
- - lib/searchgasm/search/condition_types/ends_with_condition.rb
66
- - lib/searchgasm/search/condition_types/equals_condition.rb
67
- - lib/searchgasm/search/condition_types/greater_than_condition.rb
68
- - lib/searchgasm/search/condition_types/greater_than_or_equal_to_condition.rb
69
- - lib/searchgasm/search/condition_types/inclusive_descendant_of_condition.rb
70
- - lib/searchgasm/search/condition_types/keywords_condition.rb
71
- - lib/searchgasm/search/condition_types/less_than_condition.rb
72
- - lib/searchgasm/search/condition_types/less_than_or_equal_to_condition.rb
73
- - lib/searchgasm/search/condition_types/sibling_of_condition.rb
74
- - lib/searchgasm/search/condition_types/tree_condition.rb
75
81
  - lib/searchgasm/search/conditions.rb
76
- - lib/searchgasm/search/utilities.rb
82
+ - lib/searchgasm/search/ordering.rb
83
+ - lib/searchgasm/search/pagination.rb
84
+ - lib/searchgasm/search/protection.rb
85
+ - lib/searchgasm/utilities.rb
77
86
  - lib/searchgasm/version.rb
78
87
  - lib/searchgasm.rb
79
- - README.mdown
88
+ - README.rdoc
80
89
  files:
81
90
  - CHANGELOG
91
+ - examples/README.rdoc
82
92
  - init.rb
83
93
  - lib/searchgasm/active_record/associations.rb
84
94
  - lib/searchgasm/active_record/base.rb
95
+ - lib/searchgasm/condition/base.rb
96
+ - lib/searchgasm/condition/begins_with.rb
97
+ - lib/searchgasm/condition/child_of.rb
98
+ - lib/searchgasm/condition/contains.rb
99
+ - lib/searchgasm/condition/descendant_of.rb
100
+ - lib/searchgasm/condition/does_not_equal.rb
101
+ - lib/searchgasm/condition/ends_with.rb
102
+ - lib/searchgasm/condition/equals.rb
103
+ - lib/searchgasm/condition/greater_than.rb
104
+ - lib/searchgasm/condition/greater_than_or_equal_to.rb
105
+ - lib/searchgasm/condition/inclusive_descendant_of.rb
106
+ - lib/searchgasm/condition/keywords.rb
107
+ - lib/searchgasm/condition/less_than.rb
108
+ - lib/searchgasm/condition/less_than_or_equal_to.rb
109
+ - lib/searchgasm/condition/sibling_of.rb
110
+ - lib/searchgasm/condition/tree.rb
111
+ - lib/searchgasm/conditions/base.rb
112
+ - lib/searchgasm/conditions/protection.rb
113
+ - lib/searchgasm/config.rb
114
+ - lib/searchgasm/helpers/form_helper.rb
115
+ - lib/searchgasm/helpers/search_helper.rb
116
+ - lib/searchgasm/helpers/utilities_helper.rb
85
117
  - lib/searchgasm/search/base.rb
86
- - lib/searchgasm/search/condition.rb
87
- - lib/searchgasm/search/condition_types/begins_with_condition.rb
88
- - lib/searchgasm/search/condition_types/child_of_condition.rb
89
- - lib/searchgasm/search/condition_types/contains_condition.rb
90
- - lib/searchgasm/search/condition_types/descendant_of_condition.rb
91
- - lib/searchgasm/search/condition_types/does_not_equal_condition.rb
92
- - lib/searchgasm/search/condition_types/ends_with_condition.rb
93
- - lib/searchgasm/search/condition_types/equals_condition.rb
94
- - lib/searchgasm/search/condition_types/greater_than_condition.rb
95
- - lib/searchgasm/search/condition_types/greater_than_or_equal_to_condition.rb
96
- - lib/searchgasm/search/condition_types/inclusive_descendant_of_condition.rb
97
- - lib/searchgasm/search/condition_types/keywords_condition.rb
98
- - lib/searchgasm/search/condition_types/less_than_condition.rb
99
- - lib/searchgasm/search/condition_types/less_than_or_equal_to_condition.rb
100
- - lib/searchgasm/search/condition_types/sibling_of_condition.rb
101
- - lib/searchgasm/search/condition_types/tree_condition.rb
102
118
  - lib/searchgasm/search/conditions.rb
103
- - lib/searchgasm/search/utilities.rb
119
+ - lib/searchgasm/search/ordering.rb
120
+ - lib/searchgasm/search/pagination.rb
121
+ - lib/searchgasm/search/protection.rb
122
+ - lib/searchgasm/utilities.rb
104
123
  - lib/searchgasm/version.rb
105
124
  - lib/searchgasm.rb
106
125
  - Manifest
107
126
  - MIT-LICENSE
108
127
  - Rakefile
109
- - README.mdown
128
+ - README.rdoc
110
129
  - test/fixtures/accounts.yml
111
130
  - test/fixtures/orders.yml
112
131
  - test/fixtures/users.yml
@@ -114,10 +133,13 @@ files:
114
133
  - test/libs/rexml_fix.rb
115
134
  - test/test_active_record_associations.rb
116
135
  - test/test_active_record_base.rb
136
+ - test/test_condition.rb
137
+ - test/test_conditions_base.rb
117
138
  - test/test_helper.rb
118
- - test/test_searchgasm_base.rb
119
- - test/test_searchgasm_condition_types.rb
120
- - test/test_searchgasm_conditions.rb
139
+ - test/test_search_base.rb
140
+ - test/test_search_ordering.rb
141
+ - test/test_search_pagination.rb
142
+ - test/test_search_protection.rb
121
143
  - searchgasm.gemspec
122
144
  has_rdoc: true
123
145
  homepage: http://github.com/binarylogic/searchgasm
@@ -128,7 +150,7 @@ rdoc_options:
128
150
  - --title
129
151
  - Searchgasm
130
152
  - --main
131
- - README.mdown
153
+ - README.rdoc
132
154
  require_paths:
133
155
  - lib
134
156
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -152,7 +174,10 @@ summary: Orgasmic ActiveRecord searching
152
174
  test_files:
153
175
  - test/test_active_record_associations.rb
154
176
  - test/test_active_record_base.rb
177
+ - test/test_condition.rb
178
+ - test/test_conditions_base.rb
155
179
  - test/test_helper.rb
156
- - test/test_searchgasm_base.rb
157
- - test/test_searchgasm_condition_types.rb
158
- - test/test_searchgasm_conditions.rb
180
+ - test/test_search_base.rb
181
+ - test/test_search_ordering.rb
182
+ - test/test_search_pagination.rb
183
+ - test/test_search_protection.rb
@@ -14,7 +14,7 @@ class TestActiveRecordAssociations < Test::Unit::TestCase
14
14
 
15
15
  def test_build_search
16
16
  search = Account.find(1).users.build_search
17
- assert_kind_of BinaryLogic::Searchgasm::Search::Base, search
17
+ assert_kind_of Searchgasm::Search::Base, search
18
18
  assert_equal User, search.klass
19
19
  assert_equal "\"users\".account_id = 1", search.conditions.scope
20
20
 
@@ -34,10 +34,10 @@ class TestActiveRecordBase < Test::Unit::TestCase
34
34
 
35
35
  def test_build_search
36
36
  search = Account.new_search
37
- assert_kind_of BinaryLogic::Searchgasm::Search::Base, search
37
+ assert_kind_of Searchgasm::Search::Base, search
38
38
 
39
39
  search = Account.build_search(:conditions => {:name_keywords => "awesome"}, :page => 2, :per_page => 15)
40
- assert_kind_of BinaryLogic::Searchgasm::Search::Base, search
40
+ assert_kind_of Searchgasm::Search::Base, search
41
41
  assert_equal Account, search.klass
42
42
  assert_equal "awesome", search.conditions.name_keywords
43
43
  assert_equal 2, search.page
@@ -49,10 +49,10 @@ class TestActiveRecordBase < Test::Unit::TestCase
49
49
 
50
50
  def test_build_conditions
51
51
  search = Account.new_conditions
52
- assert_kind_of BinaryLogic::Searchgasm::Search::Conditions, search
52
+ assert_kind_of Searchgasm::Conditions::Base, search
53
53
 
54
54
  search = Account.build_conditions(:name_keywords => "awesome")
55
- assert_kind_of BinaryLogic::Searchgasm::Search::Conditions, search
55
+ assert_kind_of Searchgasm::Conditions::Base, search
56
56
  assert_equal Account, search.klass
57
57
  assert_equal "awesome", search.name_keywords
58
58
 
@@ -0,0 +1,143 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestCondition < Test::Unit::TestCase
4
+ fixtures :accounts, :users, :orders
5
+
6
+ def setup
7
+ setup_db
8
+ load_fixtures
9
+ end
10
+
11
+ def teardown
12
+ teardown_db
13
+ end
14
+
15
+ def test_condition_name
16
+ assert_equal "equals", Searchgasm::Condition::Equals.condition_name
17
+ assert_equal "keywords", Searchgasm::Condition::Keywords.condition_name
18
+ assert_equal "greater_than_or_equal_to", Searchgasm::Condition::GreaterThanOrEqualTo.condition_name
19
+ end
20
+
21
+ def test_string_column
22
+
23
+ end
24
+
25
+ def test_comparable_column
26
+
27
+ end
28
+
29
+ def test_initialize
30
+ condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
31
+ assert_equal condition.klass, Account
32
+ assert_equal condition.column, Account.columns_hash["name"]
33
+
34
+ condition = Searchgasm::Condition::GreaterThan.new(Account, "id")
35
+ assert_equal condition.column, Account.columns_hash["id"]
36
+ end
37
+
38
+ def test_explicitly_set_value
39
+ condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["name"])
40
+ assert !condition.explicitly_set_value?
41
+ condition.value = nil
42
+ assert condition.explicitly_set_value?
43
+
44
+ condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
45
+ assert !condition.explicitly_set_value?
46
+ condition.value = nil
47
+ assert !condition.explicitly_set_value?
48
+ end
49
+
50
+ def test_ignore_blanks?
51
+ condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
52
+ assert !condition.ignore_blanks?
53
+
54
+ condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
55
+ assert condition.ignore_blanks?
56
+ end
57
+
58
+ def test_sanitize
59
+ condition = Searchgasm::Condition::BeginsWith.new(Account, Account.columns_hash["name"])
60
+ condition.value = "Binary"
61
+ assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "Binary%"]
62
+
63
+ condition = Searchgasm::Condition::ChildOf.new(User)
64
+ condition.value = User.first.id
65
+ assert_equal condition.sanitize, ["\"users\".\"parent_id\" = ?", User.first.id]
66
+
67
+ condition = Searchgasm::Condition::ChildOf.new(User)
68
+ condition.value = User.first
69
+ assert_equal condition.sanitize, ["\"users\".\"parent_id\" = ?", User.first.id]
70
+
71
+ condition = Searchgasm::Condition::Contains.new(Account, Account.columns_hash["name"])
72
+ condition.value = "Binary and blah"
73
+ assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "%Binary and blah%"]
74
+
75
+ condition = Searchgasm::Condition::DescendantOf.new(User)
76
+ condition.value = User.find(1)
77
+ assert_equal condition.sanitize, ["\"users\".\"id\" = ? OR \"users\".\"id\" = ?", 2, 3]
78
+
79
+ condition = Searchgasm::Condition::DoesNotEqual.new(Account, Account.columns_hash["id"])
80
+ condition.value = 12
81
+ assert_equal condition.sanitize, "\"accounts\".\"id\" != 12"
82
+
83
+ condition = Searchgasm::Condition::DoesNotEqual.new(Account, Account.columns_hash["id"])
84
+ condition.value = [1,2,3,4]
85
+ assert_equal condition.sanitize, "\"accounts\".\"id\" NOT IN (1,2,3,4)"
86
+
87
+ condition = Searchgasm::Condition::DoesNotEqual.new(Account, Account.columns_hash["id"])
88
+ condition.value = (1..10)
89
+ assert_equal condition.sanitize, "\"accounts\".\"id\" NOT BETWEEN 1 AND 10"
90
+
91
+ condition = Searchgasm::Condition::EndsWith.new(Account, Account.columns_hash["name"])
92
+ condition.value = "Binary"
93
+ assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "%Binary"]
94
+
95
+ condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
96
+ condition.value = 12
97
+ assert_equal condition.sanitize, "\"accounts\".\"id\" = 12"
98
+
99
+ condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
100
+ condition.value = [1,2,3,4]
101
+ assert_equal condition.sanitize, "\"accounts\".\"id\" IN (1,2,3,4)"
102
+
103
+ condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
104
+ condition.value = (1..10)
105
+ assert_equal condition.sanitize, "\"accounts\".\"id\" BETWEEN 1 AND 10"
106
+
107
+ condition = Searchgasm::Condition::GreaterThan.new(Account, Account.columns_hash["id"])
108
+ condition.value = 2
109
+ assert_equal condition.sanitize, ["\"accounts\".\"id\" > ?", 2]
110
+
111
+ condition = Searchgasm::Condition::GreaterThanOrEqualTo.new(Account, Account.columns_hash["id"])
112
+ condition.value = 2
113
+ assert_equal condition.sanitize, ["\"accounts\".\"id\" >= ?", 2]
114
+
115
+ condition = Searchgasm::Condition::InclusiveDescendantOf.new(User)
116
+ condition.value = User.find(1)
117
+ assert_equal condition.sanitize, ["(\"users\".\"id\" = ?) OR (\"users\".\"id\" = ? OR \"users\".\"id\" = ?)", 1, 2, 3]
118
+
119
+ condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
120
+ condition.value = "freedom yeah, freedom YEAH right"
121
+ assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ?", "%freedom%", "%yeah%", "%right%"]
122
+
123
+ condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
124
+ condition.value = "%^$*(^$)"
125
+ assert_equal condition.sanitize, nil
126
+
127
+ condition = Searchgasm::Condition::LessThan.new(Account, Account.columns_hash["id"])
128
+ condition.value = 2
129
+ assert_equal condition.sanitize, ["\"accounts\".\"id\" < ?", 2]
130
+
131
+ condition = Searchgasm::Condition::LessThanOrEqualTo.new(Account, Account.columns_hash["id"])
132
+ condition.value = 2
133
+ assert_equal condition.sanitize, ["\"accounts\".\"id\" <= ?", 2]
134
+
135
+ condition = Searchgasm::Condition::SiblingOf.new(User)
136
+ condition.value = User.find(2)
137
+ assert_equal condition.sanitize, ["(\"users\".\"id\" != ?) AND (\"users\".\"parent_id\" = ?)", 2, 1]
138
+ end
139
+
140
+ def test_value
141
+
142
+ end
143
+ end
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
- class TestSearchgasmConditions < Test::Unit::TestCase
3
+ class TestConditionsBase < Test::Unit::TestCase
4
4
  fixtures :accounts, :users, :orders
5
5
 
6
6
  def setup
@@ -13,16 +13,16 @@ class TestSearchgasmConditions < Test::Unit::TestCase
13
13
  end
14
14
 
15
15
  def test_register_conditions
16
- BinaryLogic::Searchgasm::Search::Conditions.register_condition(BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition)
17
- assert [BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition], BinaryLogic::Searchgasm::Search::Conditions.conditions
16
+ Searchgasm::Conditions::Base.register_condition(Searchgasm::Condition::Keywords)
17
+ assert [Searchgasm::Condition::Keywords], Searchgasm::Conditions::Base.conditions
18
18
 
19
- BinaryLogic::Searchgasm::Search::Conditions.register_condition(BinaryLogic::Searchgasm::Search::ConditionTypes::ContainsCondition)
20
- assert [BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition, BinaryLogic::Searchgasm::Search::ConditionTypes::ContainsCondition], BinaryLogic::Searchgasm::Search::Conditions.conditions
19
+ Searchgasm::Conditions::Base.register_condition(Searchgasm::Condition::Contains)
20
+ assert [Searchgasm::Condition::Keywords, Searchgasm::Condition::Contains], Searchgasm::Conditions::Base.conditions
21
21
 
22
22
  end
23
23
 
24
24
  def test_initialize
25
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account, :name_contains => "Binary")
25
+ conditions = Searchgasm::Conditions::Base.new(Account, :name_contains => "Binary")
26
26
  assert_equal conditions.klass, Account
27
27
  assert_equal conditions.name_contains, "Binary"
28
28
  end
@@ -34,19 +34,29 @@ class TestSearchgasmConditions < Test::Unit::TestCase
34
34
  def test_setting_conditions
35
35
  [Account, User, Order].each do |klass|
36
36
  conditions = klass.new_conditions
37
- conditions.condition_names.each do |condition_name|
37
+ conditions.send(:condition_names).each do |condition_name|
38
38
  conditions.send("#{condition_name}=", 1)
39
39
  assert_equal 1, conditions.send(condition_name)
40
40
  end
41
41
  end
42
42
  end
43
43
 
44
+ def test_accessible_protected_conditions
45
+ #Account.conditions_accessible << :name_contains
46
+ #conditions = Searchgasm::Conditions::Base.new(Account)
47
+ #conditions.conditions = {:created_after => Time.now, :name_contains => "Binary"}
48
+ #assert({:name_contains => "Binary"}, conditions.value)
49
+ end
50
+
44
51
  def test_assert_valid_values
45
-
52
+ conditions = User.new_conditions
53
+ assert_raise(ArgumentError) { conditions.conditions = {:unknown => "blah"} }
54
+ assert_nothing_raised { conditions.conditions = {:first_name => "blah"} }
55
+ assert_nothing_raised { conditions.conditions = {:first_name_contains => "blah"} }
46
56
  end
47
57
 
48
58
  def test_setting_associations
49
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account, :users => {:first_name_like => "Ben"})
59
+ conditions = Searchgasm::Conditions::Base.new(Account, :users => {:first_name_like => "Ben"})
50
60
  assert_equal conditions.users.first_name_like, "Ben"
51
61
 
52
62
  conditions.users.last_name_begins_with = "Ben"
@@ -54,7 +64,7 @@ class TestSearchgasmConditions < Test::Unit::TestCase
54
64
  end
55
65
 
56
66
  def test_includes
57
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
67
+ conditions = Searchgasm::Conditions::Base.new(Account)
58
68
  assert_equal conditions.includes, nil
59
69
 
60
70
  conditions.name_like = "Binary"
@@ -68,44 +78,44 @@ class TestSearchgasmConditions < Test::Unit::TestCase
68
78
  end
69
79
 
70
80
  def test_objects
71
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
72
- assert_equal conditions.objects, []
81
+ conditions = Searchgasm::Conditions::Base.new(Account)
82
+ assert_equal conditions.send(:objects), []
73
83
 
74
84
  conditions.name_contains = "Binary"
75
- assert_equal conditions.objects.size, 1
85
+ assert_equal conditions.send(:objects).size, 1
76
86
 
77
87
  conditions.users.first_name_contains = "Ben"
78
- assert_equal conditions.objects.size, 2
88
+ assert_equal conditions.send(:objects).size, 2
79
89
  end
80
90
 
81
91
  def test_reset
82
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
92
+ conditions = Searchgasm::Conditions::Base.new(Account)
83
93
 
84
94
  conditions.name_contains = "Binary"
85
- assert_equal 1, conditions.objects.size
95
+ assert_equal 1, conditions.send(:objects).size
86
96
 
87
97
  conditions.reset_name_contains!
88
- assert_equal [], conditions.objects
98
+ assert_equal [], conditions.send(:objects)
89
99
 
90
100
  conditions.users.first_name_like = "Ben"
91
- assert_equal 1, conditions.objects.size
101
+ assert_equal 1, conditions.send(:objects).size
92
102
 
93
103
  conditions.reset_users!
94
- assert_equal [], conditions.objects
104
+ assert_equal [], conditions.send(:objects)
95
105
 
96
106
  conditions.name_begins_with ="Binary"
97
107
  conditions.users.orders.total_gt = 200
98
- assert_equal 2, conditions.objects.size
108
+ assert_equal 2, conditions.send(:objects).size
99
109
 
100
110
  conditions.reset_name_begins_with!
101
- assert_equal 1, conditions.objects.size
111
+ assert_equal 1, conditions.send(:objects).size
102
112
 
103
113
  conditions.reset_users!
104
- assert_equal [], conditions.objects
114
+ assert_equal [], conditions.send(:objects)
105
115
  end
106
116
 
107
117
  def test_sanitize
108
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
118
+ conditions = Searchgasm::Conditions::Base.new(Account)
109
119
  conditions.name_contains = "Binary"
110
120
  conditions.id_gt = 5
111
121
  now = Time.now
@@ -119,29 +129,29 @@ class TestSearchgasmConditions < Test::Unit::TestCase
119
129
  assert_equal conditions.sanitize, ["(\"accounts\".\"name\" LIKE ?) AND (\"accounts\".\"id\" > ?) AND (\"accounts\".\"created_at\" > ?) AND ((\"users\".\"first_name\" LIKE ?) AND (\"users\".\"id\" > ?) AND (\"orders\".\"total\" < ?))", "%Binary%", 5, now, "%Ben%", 10, 500]
120
130
  end
121
131
 
122
- def test_value
123
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
132
+ def test_conditions
133
+ conditions = Searchgasm::Conditions::Base.new(Account)
124
134
  now = Time.now
125
135
  v = {:name_contains => "Binary", :created_at_greater_than => now}
126
- conditions.value = v
127
- assert_equal v, conditions.value
136
+ conditions.conditions = v
137
+ assert_equal v, conditions.conditions
128
138
 
129
139
  scope = "id in (1,2,3,4)"
130
- conditions.value = scope
131
- assert_equal v, conditions.value, v
140
+ conditions.conditions = scope
141
+ assert_equal v, conditions.conditions, v
132
142
  assert_equal scope, conditions.scope
133
143
 
134
144
  v2 = {:id_less_than => 5, :name_begins_with => "Beginning of string"}
135
- conditions.value = v2
136
- assert_equal v.merge(v2), conditions.value
145
+ conditions.conditions = v2
146
+ assert_equal v.merge(v2), conditions.conditions
137
147
 
138
148
  scope2 = "id > 5 and name = 'Test'"
139
- conditions.value = scope2
149
+ conditions.conditions = scope2
140
150
  assert_equal scope2, conditions.scope
141
151
  end
142
152
 
143
153
  def test_searching
144
- conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
154
+ conditions = Searchgasm::Conditions::Base.new(Account)
145
155
  conditions.name_contains = "Binary"
146
156
  assert_equal Account.find(1, 3), conditions.all
147
157
  assert_equal Account.find(1, 3), conditions.find(:all)