searchgasm 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +2 -0
- data/Manifest +17 -4
- data/README.mdown +19 -16
- data/lib/searchgasm/search/base.rb +2 -4
- data/lib/searchgasm/search/condition_types/begins_with_condition.rb +28 -0
- data/lib/searchgasm/search/condition_types/child_of_condition.rb +17 -0
- data/lib/searchgasm/search/condition_types/condition.rb +107 -0
- data/lib/searchgasm/search/condition_types/contains_condition.rb +26 -0
- data/lib/searchgasm/search/condition_types/descendant_of_condition.rb +30 -0
- data/lib/searchgasm/search/condition_types/does_not_equal_condition.rb +34 -0
- data/lib/searchgasm/search/condition_types/ends_with_condition.rb +26 -0
- data/lib/searchgasm/search/condition_types/equals_condition.rb +26 -0
- data/lib/searchgasm/search/condition_types/greater_than_condition.rb +31 -0
- data/lib/searchgasm/search/condition_types/greater_than_or_equal_to_condition.rb +26 -0
- data/lib/searchgasm/search/condition_types/inclusive_descendant_of_condition.rb +19 -0
- data/lib/searchgasm/search/condition_types/keywords_condition.rb +39 -0
- data/lib/searchgasm/search/condition_types/less_than_condition.rb +31 -0
- data/lib/searchgasm/search/condition_types/less_than_or_equal_to_condition.rb +26 -0
- data/lib/searchgasm/search/condition_types/sibling_of_condition.rb +22 -0
- data/lib/searchgasm/search/condition_types/tree_condition.rb +20 -0
- data/lib/searchgasm/search/conditions.rb +55 -82
- data/lib/searchgasm/version.rb +1 -1
- data/lib/searchgasm.rb +23 -2
- data/searchgasm.gemspec +36 -10
- data/test/test_helper.rb +1 -0
- data/test/test_searchgasm_base.rb +32 -0
- data/test/test_searchgasm_condition_types.rb +143 -0
- data/test/test_searchgasm_conditions.rb +21 -29
- metadata +35 -9
- data/lib/searchgasm/helpers.rb +0 -100
- data/lib/searchgasm/search/condition.rb +0 -168
- data/test/test_active_record_protection.rb +0 -0
- data/test/test_searchgasm_condition.rb +0 -149
@@ -0,0 +1,20 @@
|
|
1
|
+
module BinaryLogic
|
2
|
+
module Searchgasm
|
3
|
+
module Search
|
4
|
+
module ConditionTypes
|
5
|
+
class TreeCondition < Condition
|
6
|
+
class << self
|
7
|
+
def name_for_column(column)
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def name_for_klass(klass)
|
12
|
+
return unless klass.reflect_on_association(:parent) && klass.reflect_on_association(:children)
|
13
|
+
condition_name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -7,58 +7,21 @@ module BinaryLogic
|
|
7
7
|
attr_accessor :klass, :protect, :relationship_name, :scope
|
8
8
|
|
9
9
|
class << self
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
when :string, :text
|
14
|
-
condition_types += [:begins_with, :contains, :keywords, :ends_with]
|
15
|
-
when :integer, :float, :decimal, :datetime, :timestamp, :time, :date
|
16
|
-
condition_types += [:greater_than, :greater_than_or_equal_to, :less_than, :less_than_or_equal_to]
|
17
|
-
end
|
18
|
-
condition_types
|
10
|
+
def register_condition(klass)
|
11
|
+
raise(ArgumentError, "You can only register conditions that extend BinaryLogic::Searchgasm::Search::ConditionTypes::Condition") unless klass.ancestors.include?(ConditionTypes::Condition)
|
12
|
+
conditions << klass
|
19
13
|
end
|
20
14
|
|
21
|
-
def
|
22
|
-
|
23
|
-
when :equals then ["", :is]
|
24
|
-
when :does_not_equal then [:is_not, :not]
|
25
|
-
when :begins_with then [:starts_with]
|
26
|
-
when :contains then [:like]
|
27
|
-
when :greater_than then [:gt, :after]
|
28
|
-
when :greater_than_or_equal_to then [:at_least, :gte]
|
29
|
-
when :less_than then [:lt, :before]
|
30
|
-
when :less_than_or_equal_to then [:at_most, :lte]
|
31
|
-
else []
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def aliases_for_condition(*args)
|
36
|
-
column, condition_type = nil, nil
|
37
|
-
|
38
|
-
# Allow a condition object or the column and condition type to be passed
|
39
|
-
if args.size == 1
|
40
|
-
column, condition_type = condition.column, condition.condition
|
41
|
-
else
|
42
|
-
column, condition_type = args.first, args[1]
|
43
|
-
end
|
44
|
-
|
45
|
-
name = Condition.generate_name(column, condition_type)
|
46
|
-
alias_condition_types = aliases_for_condition_type(condition_type)
|
47
|
-
column_names = [column.name]
|
48
|
-
column_names << column.name.gsub(/_at$/, "") if [:datetime, :timestamp, :time, :date].include?(column.type) && column.name =~ /_at$/
|
49
|
-
|
50
|
-
aliases = []
|
51
|
-
column_names.each do |column_name|
|
52
|
-
alias_condition_types.each { |alias_condition_type| aliases << Condition.generate_name(column_name, alias_condition_type) }
|
53
|
-
end
|
54
|
-
aliases
|
15
|
+
def conditions
|
16
|
+
@@conditions ||= []
|
55
17
|
end
|
56
18
|
end
|
57
19
|
|
58
20
|
def initialize(klass, init_values = {})
|
59
21
|
self.klass = klass
|
60
|
-
|
61
|
-
|
22
|
+
add_klass_conditions!
|
23
|
+
add_column_conditions!
|
24
|
+
add_associations!
|
62
25
|
self.value = init_values
|
63
26
|
end
|
64
27
|
|
@@ -106,9 +69,9 @@ module BinaryLogic
|
|
106
69
|
end
|
107
70
|
|
108
71
|
def sanitize
|
109
|
-
|
110
|
-
return scope if
|
111
|
-
merge_conditions(
|
72
|
+
conditions = merge_conditions(*objects.collect { |object| object.sanitize })
|
73
|
+
return scope if conditions.blank?
|
74
|
+
merge_conditions(conditions, scope)
|
112
75
|
end
|
113
76
|
|
114
77
|
def value=(values)
|
@@ -135,57 +98,67 @@ module BinaryLogic
|
|
135
98
|
end
|
136
99
|
|
137
100
|
private
|
138
|
-
def
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
@#{association.name}
|
143
|
-
|
144
|
-
|
101
|
+
def add_associations!
|
102
|
+
klass.reflect_on_all_associations.each do |association|
|
103
|
+
self.class.class_eval <<-end_eval
|
104
|
+
def #{association.name}
|
105
|
+
if @#{association.name}.nil?
|
106
|
+
@#{association.name} = self.class.new(#{association.class_name})
|
107
|
+
@#{association.name}.relationship_name = "#{association.name}"
|
108
|
+
self.objects << @#{association.name}
|
109
|
+
end
|
110
|
+
@#{association.name}
|
145
111
|
end
|
146
|
-
@#{association.name}
|
147
|
-
end
|
148
112
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
association.name
|
113
|
+
def #{association.name}=(value); #{association.name}.value = value; end
|
114
|
+
def reset_#{association.name}!; objects.delete(#{association.name}); @#{association.name} = nil; end
|
115
|
+
end_eval
|
116
|
+
end
|
154
117
|
end
|
155
118
|
|
156
|
-
def
|
157
|
-
|
119
|
+
def add_column_conditions!
|
120
|
+
klass.columns.each do |column|
|
121
|
+
self.class.conditions.each do |condition|
|
122
|
+
name = condition.name_for_column(column)
|
123
|
+
next if name.blank?
|
124
|
+
add_condition!(condition, name, column)
|
125
|
+
condition.aliases_for_column(column).each { |alias_name| add_condition_alias!(alias_name, name) }
|
126
|
+
end
|
127
|
+
end
|
158
128
|
end
|
159
129
|
|
160
|
-
def add_condition!(
|
161
|
-
name = Condition.generate_name(column, condition_type)
|
130
|
+
def add_condition!(condition, name, column = nil)
|
162
131
|
self.condition_names << name
|
163
|
-
|
164
|
-
# Define accessor methods
|
165
|
-
self.class.class_eval <<-SRC
|
132
|
+
self.class.class_eval <<-end_eval
|
166
133
|
def #{name}_object
|
167
134
|
if @#{name}.nil?
|
168
|
-
@#{name} =
|
135
|
+
@#{name} = #{condition.name}.new(klass#{column.nil? ? "" : ", \"#{column.name}\""})
|
169
136
|
self.objects << @#{name}
|
170
137
|
end
|
171
138
|
@#{name}
|
172
139
|
end
|
173
|
-
|
140
|
+
|
174
141
|
def #{name}; #{name}_object.value; end
|
175
142
|
def #{name}=(value); #{name}_object.value = value; end
|
176
143
|
def reset_#{name}!; objects.delete(#{name}_object); @#{name} = nil; end
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
144
|
+
end_eval
|
145
|
+
end
|
146
|
+
|
147
|
+
def add_condition_alias!(alias_name, name)
|
148
|
+
self.condition_names << alias_name
|
149
|
+
self.class.class_eval do
|
150
|
+
alias_method alias_name, name
|
151
|
+
alias_method "#{alias_name}=", "#{name}="
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def add_klass_conditions!
|
156
|
+
self.class.conditions.each do |condition|
|
157
|
+
name = condition.name_for_klass(klass)
|
158
|
+
next if name.blank?
|
159
|
+
add_condition!(condition, name)
|
160
|
+
condition.aliases_for_klass(klass).each { |alias_name| add_condition_alias!(alias_name, name) }
|
186
161
|
end
|
187
|
-
|
188
|
-
name
|
189
162
|
end
|
190
163
|
end
|
191
164
|
end
|
data/lib/searchgasm/version.rb
CHANGED
data/lib/searchgasm.rb
CHANGED
@@ -1,8 +1,29 @@
|
|
1
1
|
require "active_record"
|
2
|
+
|
2
3
|
require "searchgasm/active_record/base"
|
3
4
|
require "searchgasm/active_record/associations"
|
5
|
+
|
4
6
|
require "searchgasm/version"
|
5
7
|
require "searchgasm/search/utilities"
|
6
|
-
require "searchgasm/search/condition"
|
7
8
|
require "searchgasm/search/conditions"
|
8
|
-
require "searchgasm/search/base"
|
9
|
+
require "searchgasm/search/base"
|
10
|
+
|
11
|
+
# Regular conidtion types
|
12
|
+
require "searchgasm/search/condition_types/condition"
|
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"
|
23
|
+
|
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"
|
data/searchgasm.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
|
-
# Gem::Specification for Searchgasm-0.9.
|
2
|
+
# Gem::Specification for Searchgasm-0.9.3
|
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.
|
8
|
+
version: 0.9.3
|
9
9
|
platform: ruby
|
10
10
|
authors:
|
11
11
|
- Ben Johnson of Binary Logic
|
@@ -55,9 +55,23 @@ 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/helpers.rb
|
59
58
|
- lib/searchgasm/search/base.rb
|
60
|
-
- lib/searchgasm/search/
|
59
|
+
- lib/searchgasm/search/condition_types/begins_with_condition.rb
|
60
|
+
- lib/searchgasm/search/condition_types/child_of_condition.rb
|
61
|
+
- lib/searchgasm/search/condition_types/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
|
61
75
|
- lib/searchgasm/search/conditions.rb
|
62
76
|
- lib/searchgasm/search/utilities.rb
|
63
77
|
- lib/searchgasm/version.rb
|
@@ -68,9 +82,23 @@ files:
|
|
68
82
|
- init.rb
|
69
83
|
- lib/searchgasm/active_record/associations.rb
|
70
84
|
- lib/searchgasm/active_record/base.rb
|
71
|
-
- lib/searchgasm/helpers.rb
|
72
85
|
- lib/searchgasm/search/base.rb
|
73
|
-
- lib/searchgasm/search/
|
86
|
+
- lib/searchgasm/search/condition_types/begins_with_condition.rb
|
87
|
+
- lib/searchgasm/search/condition_types/child_of_condition.rb
|
88
|
+
- lib/searchgasm/search/condition_types/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
|
74
102
|
- lib/searchgasm/search/conditions.rb
|
75
103
|
- lib/searchgasm/search/utilities.rb
|
76
104
|
- lib/searchgasm/version.rb
|
@@ -86,10 +114,9 @@ files:
|
|
86
114
|
- test/libs/rexml_fix.rb
|
87
115
|
- test/test_active_record_associations.rb
|
88
116
|
- test/test_active_record_base.rb
|
89
|
-
- test/test_active_record_protection.rb
|
90
117
|
- test/test_helper.rb
|
91
118
|
- test/test_searchgasm_base.rb
|
92
|
-
- test/
|
119
|
+
- test/test_searchgasm_condition_types.rb
|
93
120
|
- test/test_searchgasm_conditions.rb
|
94
121
|
- searchgasm.gemspec
|
95
122
|
has_rdoc: true
|
@@ -125,8 +152,7 @@ summary: Orgasmic ActiveRecord searching
|
|
125
152
|
test_files:
|
126
153
|
- test/test_active_record_associations.rb
|
127
154
|
- test/test_active_record_base.rb
|
128
|
-
- test/test_active_record_protection.rb
|
129
155
|
- test/test_helper.rb
|
130
156
|
- test/test_searchgasm_base.rb
|
131
|
-
- test/
|
157
|
+
- test/test_searchgasm_condition_types.rb
|
132
158
|
- test/test_searchgasm_conditions.rb
|
data/test/test_helper.rb
CHANGED
@@ -78,6 +78,34 @@ class TestSearchgasmBase < Test::Unit::TestCase
|
|
78
78
|
assert_kind_of BinaryLogic::Searchgasm::Search::Conditions, search.conditions
|
79
79
|
end
|
80
80
|
|
81
|
+
def test_include
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_limit
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_options
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_order_as
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_order_by
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_page
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_reset
|
106
|
+
|
107
|
+
end
|
108
|
+
|
81
109
|
def test_sanitize
|
82
110
|
search = BinaryLogic::Searchgasm::Search::Base.new(Account)
|
83
111
|
search.per_page = 2
|
@@ -88,6 +116,10 @@ class TestSearchgasmBase < Test::Unit::TestCase
|
|
88
116
|
assert_equal search.sanitize, {:include => :users, :offset => 6, :readonly => true, :conditions => ["(\"accounts\".\"name\" LIKE ?) AND (\"users\".\"id\" > ?)", "%Binary%", 2], :limit => 2 }
|
89
117
|
end
|
90
118
|
|
119
|
+
def test_scope
|
120
|
+
|
121
|
+
end
|
122
|
+
|
91
123
|
def test_searching
|
92
124
|
search = BinaryLogic::Searchgasm::Search::Base.new(Account)
|
93
125
|
search.conditions.name_like = "Binary"
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestSearchgasmConditionTypes < 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", BinaryLogic::Searchgasm::Search::ConditionTypes::EqualsCondition.condition_name
|
17
|
+
assert_equal "keywords", BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition.condition_name
|
18
|
+
assert_equal "greater_than_or_equal_to", BinaryLogic::Searchgasm::Search::ConditionTypes::GreaterThanOrEqualToCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition.new(Account, Account.columns_hash["name"])
|
31
|
+
assert_equal condition.klass, Account
|
32
|
+
assert_equal condition.column, Account.columns_hash["name"]
|
33
|
+
|
34
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::GreaterThanCondition.new(Account, "id")
|
35
|
+
assert_equal condition.column, Account.columns_hash["id"]
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_explicitly_set_value
|
39
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::EqualsCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::EqualsCondition.new(Account, Account.columns_hash["id"])
|
52
|
+
assert !condition.ignore_blanks?
|
53
|
+
|
54
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition.new(Account, Account.columns_hash["name"])
|
55
|
+
assert condition.ignore_blanks?
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_sanitize
|
59
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::BeginsWithCondition.new(Account, Account.columns_hash["name"])
|
60
|
+
condition.value = "Binary"
|
61
|
+
assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "Binary%"]
|
62
|
+
|
63
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::ChildOfCondition.new(User)
|
64
|
+
condition.value = User.first.id
|
65
|
+
assert_equal condition.sanitize, ["\"users\".\"parent_id\" = ?", User.first.id]
|
66
|
+
|
67
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::ChildOfCondition.new(User)
|
68
|
+
condition.value = User.first
|
69
|
+
assert_equal condition.sanitize, ["\"users\".\"parent_id\" = ?", User.first.id]
|
70
|
+
|
71
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::ContainsCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::DescendantOfCondition.new(User)
|
76
|
+
condition.value = User.find(1)
|
77
|
+
assert_equal condition.sanitize, ["\"users\".\"id\" = ? OR \"users\".\"id\" = ?", 2, 3]
|
78
|
+
|
79
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::DoesNotEqualCondition.new(Account, Account.columns_hash["id"])
|
80
|
+
condition.value = 12
|
81
|
+
assert_equal condition.sanitize, "\"accounts\".\"id\" != 12"
|
82
|
+
|
83
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::DoesNotEqualCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::DoesNotEqualCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::EndsWithCondition.new(Account, Account.columns_hash["name"])
|
92
|
+
condition.value = "Binary"
|
93
|
+
assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "%Binary"]
|
94
|
+
|
95
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::EqualsCondition.new(Account, Account.columns_hash["id"])
|
96
|
+
condition.value = 12
|
97
|
+
assert_equal condition.sanitize, "\"accounts\".\"id\" = 12"
|
98
|
+
|
99
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::EqualsCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::EqualsCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::GreaterThanCondition.new(Account, Account.columns_hash["id"])
|
108
|
+
condition.value = 2
|
109
|
+
assert_equal condition.sanitize, ["\"accounts\".\"id\" > ?", 2]
|
110
|
+
|
111
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::GreaterThanOrEqualToCondition.new(Account, Account.columns_hash["id"])
|
112
|
+
condition.value = 2
|
113
|
+
assert_equal condition.sanitize, ["\"accounts\".\"id\" >= ?", 2]
|
114
|
+
|
115
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::InclusiveDescendantOfCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition.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 = BinaryLogic::Searchgasm::Search::ConditionTypes::KeywordsCondition.new(Account, Account.columns_hash["name"])
|
124
|
+
condition.value = "%^$*(^$)"
|
125
|
+
assert_equal condition.sanitize, nil
|
126
|
+
|
127
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::LessThanCondition.new(Account, Account.columns_hash["id"])
|
128
|
+
condition.value = 2
|
129
|
+
assert_equal condition.sanitize, ["\"accounts\".\"id\" < ?", 2]
|
130
|
+
|
131
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::LessThanOrEqualToCondition.new(Account, Account.columns_hash["id"])
|
132
|
+
condition.value = 2
|
133
|
+
assert_equal condition.sanitize, ["\"accounts\".\"id\" <= ?", 2]
|
134
|
+
|
135
|
+
condition = BinaryLogic::Searchgasm::Search::ConditionTypes::SiblingOfCondition.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
|
@@ -12,31 +12,39 @@ class TestSearchgasmConditions < Test::Unit::TestCase
|
|
12
12
|
teardown_db
|
13
13
|
end
|
14
14
|
|
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
|
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
|
21
|
+
|
22
|
+
end
|
23
|
+
|
15
24
|
def test_initialize
|
16
25
|
conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account, :name_contains => "Binary")
|
17
26
|
assert_equal conditions.klass, Account
|
18
27
|
assert_equal conditions.name_contains, "Binary"
|
19
28
|
end
|
20
29
|
|
30
|
+
def test_conditions_added
|
31
|
+
# test to make sure all of the proper methods were add, testing condition_names basically
|
32
|
+
end
|
33
|
+
|
21
34
|
def test_setting_conditions
|
22
35
|
[Account, User, Order].each do |klass|
|
23
|
-
conditions =
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
BinaryLogic::Searchgasm::Search::Conditions.condition_types_for_column_type(column.type).each do |condition_type|
|
28
|
-
name = BinaryLogic::Searchgasm::Search::Condition.generate_name(column, condition_type)
|
29
|
-
conditions.send("#{name}=", value)
|
30
|
-
assert_equal conditions.send(name), value
|
31
|
-
BinaryLogic::Searchgasm::Search::Conditions.aliases_for_condition(column, condition_type).each do |alias_condition|
|
32
|
-
conditions.send("#{alias_condition}=", value)
|
33
|
-
assert_equal conditions.send(alias_condition), value
|
34
|
-
end
|
35
|
-
end
|
36
|
+
conditions = klass.new_conditions
|
37
|
+
conditions.condition_names.each do |condition_name|
|
38
|
+
conditions.send("#{condition_name}=", 1)
|
39
|
+
assert_equal 1, conditions.send(condition_name)
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
44
|
+
def test_assert_valid_values
|
45
|
+
|
46
|
+
end
|
47
|
+
|
40
48
|
def test_setting_associations
|
41
49
|
conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account, :users => {:first_name_like => "Ben"})
|
42
50
|
assert_equal conditions.users.first_name_like, "Ben"
|
@@ -127,20 +135,4 @@ class TestSearchgasmConditions < Test::Unit::TestCase
|
|
127
135
|
assert_raise(ArgumentError) { account.users.build_conditions("(DELETE FROM users)") }
|
128
136
|
assert_nothing_raised { account.users.build_conditions!("(DELETE FROM users)") }
|
129
137
|
end
|
130
|
-
|
131
|
-
private
|
132
|
-
def column_value(column)
|
133
|
-
case column.type
|
134
|
-
when :string, :text, :binary
|
135
|
-
Array.new(50) { (rand(122-97) + 97).chr }.join
|
136
|
-
when :integer
|
137
|
-
rand(99999)
|
138
|
-
when :float, :decimal
|
139
|
-
rand * 100
|
140
|
-
when :datetime, :timestamp, :time
|
141
|
-
Time.now
|
142
|
-
when :date
|
143
|
-
Date.today
|
144
|
-
end
|
145
|
-
end
|
146
138
|
end
|