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
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.
|
|
4
|
+
version: 0.9.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ben Johnson of Binary Logic
|
|
@@ -52,9 +52,23 @@ extra_rdoc_files:
|
|
|
52
52
|
- CHANGELOG
|
|
53
53
|
- lib/searchgasm/active_record/associations.rb
|
|
54
54
|
- lib/searchgasm/active_record/base.rb
|
|
55
|
-
- lib/searchgasm/helpers.rb
|
|
56
55
|
- lib/searchgasm/search/base.rb
|
|
57
|
-
- lib/searchgasm/search/
|
|
56
|
+
- lib/searchgasm/search/condition_types/begins_with_condition.rb
|
|
57
|
+
- lib/searchgasm/search/condition_types/child_of_condition.rb
|
|
58
|
+
- lib/searchgasm/search/condition_types/condition.rb
|
|
59
|
+
- lib/searchgasm/search/condition_types/contains_condition.rb
|
|
60
|
+
- lib/searchgasm/search/condition_types/descendant_of_condition.rb
|
|
61
|
+
- lib/searchgasm/search/condition_types/does_not_equal_condition.rb
|
|
62
|
+
- lib/searchgasm/search/condition_types/ends_with_condition.rb
|
|
63
|
+
- lib/searchgasm/search/condition_types/equals_condition.rb
|
|
64
|
+
- lib/searchgasm/search/condition_types/greater_than_condition.rb
|
|
65
|
+
- lib/searchgasm/search/condition_types/greater_than_or_equal_to_condition.rb
|
|
66
|
+
- lib/searchgasm/search/condition_types/inclusive_descendant_of_condition.rb
|
|
67
|
+
- lib/searchgasm/search/condition_types/keywords_condition.rb
|
|
68
|
+
- lib/searchgasm/search/condition_types/less_than_condition.rb
|
|
69
|
+
- lib/searchgasm/search/condition_types/less_than_or_equal_to_condition.rb
|
|
70
|
+
- lib/searchgasm/search/condition_types/sibling_of_condition.rb
|
|
71
|
+
- lib/searchgasm/search/condition_types/tree_condition.rb
|
|
58
72
|
- lib/searchgasm/search/conditions.rb
|
|
59
73
|
- lib/searchgasm/search/utilities.rb
|
|
60
74
|
- lib/searchgasm/version.rb
|
|
@@ -65,9 +79,23 @@ files:
|
|
|
65
79
|
- init.rb
|
|
66
80
|
- lib/searchgasm/active_record/associations.rb
|
|
67
81
|
- lib/searchgasm/active_record/base.rb
|
|
68
|
-
- lib/searchgasm/helpers.rb
|
|
69
82
|
- lib/searchgasm/search/base.rb
|
|
70
|
-
- lib/searchgasm/search/
|
|
83
|
+
- lib/searchgasm/search/condition_types/begins_with_condition.rb
|
|
84
|
+
- lib/searchgasm/search/condition_types/child_of_condition.rb
|
|
85
|
+
- lib/searchgasm/search/condition_types/condition.rb
|
|
86
|
+
- lib/searchgasm/search/condition_types/contains_condition.rb
|
|
87
|
+
- lib/searchgasm/search/condition_types/descendant_of_condition.rb
|
|
88
|
+
- lib/searchgasm/search/condition_types/does_not_equal_condition.rb
|
|
89
|
+
- lib/searchgasm/search/condition_types/ends_with_condition.rb
|
|
90
|
+
- lib/searchgasm/search/condition_types/equals_condition.rb
|
|
91
|
+
- lib/searchgasm/search/condition_types/greater_than_condition.rb
|
|
92
|
+
- lib/searchgasm/search/condition_types/greater_than_or_equal_to_condition.rb
|
|
93
|
+
- lib/searchgasm/search/condition_types/inclusive_descendant_of_condition.rb
|
|
94
|
+
- lib/searchgasm/search/condition_types/keywords_condition.rb
|
|
95
|
+
- lib/searchgasm/search/condition_types/less_than_condition.rb
|
|
96
|
+
- lib/searchgasm/search/condition_types/less_than_or_equal_to_condition.rb
|
|
97
|
+
- lib/searchgasm/search/condition_types/sibling_of_condition.rb
|
|
98
|
+
- lib/searchgasm/search/condition_types/tree_condition.rb
|
|
71
99
|
- lib/searchgasm/search/conditions.rb
|
|
72
100
|
- lib/searchgasm/search/utilities.rb
|
|
73
101
|
- lib/searchgasm/version.rb
|
|
@@ -83,10 +111,9 @@ files:
|
|
|
83
111
|
- test/libs/rexml_fix.rb
|
|
84
112
|
- test/test_active_record_associations.rb
|
|
85
113
|
- test/test_active_record_base.rb
|
|
86
|
-
- test/test_active_record_protection.rb
|
|
87
114
|
- test/test_helper.rb
|
|
88
115
|
- test/test_searchgasm_base.rb
|
|
89
|
-
- test/
|
|
116
|
+
- test/test_searchgasm_condition_types.rb
|
|
90
117
|
- test/test_searchgasm_conditions.rb
|
|
91
118
|
- searchgasm.gemspec
|
|
92
119
|
has_rdoc: true
|
|
@@ -123,8 +150,7 @@ summary: Orgasmic ActiveRecord searching
|
|
|
123
150
|
test_files:
|
|
124
151
|
- test/test_active_record_associations.rb
|
|
125
152
|
- test/test_active_record_base.rb
|
|
126
|
-
- test/test_active_record_protection.rb
|
|
127
153
|
- test/test_helper.rb
|
|
128
154
|
- test/test_searchgasm_base.rb
|
|
129
|
-
- test/
|
|
155
|
+
- test/test_searchgasm_condition_types.rb
|
|
130
156
|
- test/test_searchgasm_conditions.rb
|
data/lib/searchgasm/helpers.rb
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
require "base64"
|
|
2
|
-
|
|
3
|
-
module BinaryLogic
|
|
4
|
-
module SearchGasm
|
|
5
|
-
module Helpers
|
|
6
|
-
def order_by_link(text, searcher, options = {})
|
|
7
|
-
options[:order_by] ||= text.underscore.gsub(/ /, "_")
|
|
8
|
-
options[:order_as] ||= "ASC"
|
|
9
|
-
options[:form_prefix] ||= determine_form_prefix(searcher)
|
|
10
|
-
|
|
11
|
-
if searcher.order_by == options[:order_by]
|
|
12
|
-
options[:order_as] = searcher.order_as == "ASC" ? "DESC" : "ASC"
|
|
13
|
-
|
|
14
|
-
text = content_tag("span", text + (searcher.order_as == "ASC" ? " ▲" : " ▼"), :class => searcher.order_as == "ASC" ? "ordering asc" : "ordering desc")
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
options[:order_by] = Base64.encode64(Marshal.dump(options[:order_by])) if !options[:order_by].nil? && !options[:order_by].is_a?(String) && !options[:order_by].is_a?(Symbol)
|
|
18
|
-
|
|
19
|
-
link_to_function(text, "submit_form({form_prefix: '#{options[:form_prefix]}', dont_reset: true, fields: {order_by: '#{escape_javascript(options[:order_by])}', order_as: '#{options[:order_as]}'}});")
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
# tag methods
|
|
23
|
-
#------------------------------------------------------------------------------
|
|
24
|
-
def page_select_tag(name, items_count, searcher, options = {})
|
|
25
|
-
options = options.dup
|
|
26
|
-
form_prefix = options.delete(:form_prefix) || determine_form_prefix(searcher)
|
|
27
|
-
options[:id] ||= "select_tag_#{unique_id}"
|
|
28
|
-
options[:onchange] ||= "submit_form({form_prefix: '#{form_prefix}', dont_reset: true, fields: {page: this.value}});"
|
|
29
|
-
items_count = items_count.to_i
|
|
30
|
-
per_page = searcher.per_page.to_i
|
|
31
|
-
page = searcher.page.to_i
|
|
32
|
-
page = 1 if page < 1
|
|
33
|
-
page_options = page_options_for_select(items_count, per_page)
|
|
34
|
-
|
|
35
|
-
html = ""
|
|
36
|
-
if page_options.size > 0
|
|
37
|
-
html << (button_to_function("Prev", "sel = $('#{options[:id]}'); sel.value = '#{page-1}'; sel.onchange();", :class => "prev_page") + " ") if page > 1
|
|
38
|
-
html << select_tag(name, options_for_select(page_options, page), options)
|
|
39
|
-
html << (" " + button_to_function("Next", "sel = $('#{options[:id]}'); sel.value = '#{page+1}'; sel.onchange();", :class => "next_page")) if page < page_options.size
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
html
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def per_page_select_tag(name, items_count, searcher, options = {})
|
|
46
|
-
options = options.dup
|
|
47
|
-
form_prefix = options.delete(:form_prefix) || determine_form_prefix(searcher)
|
|
48
|
-
options[:onchange] ||= "submit_form({form_prefix: '#{form_prefix}', dont_reset: true, fields: {per_page: this.value}});"
|
|
49
|
-
items_count = items_count.to_i
|
|
50
|
-
per_page = searcher.per_page.to_i
|
|
51
|
-
per_page = 0 if items_count <= per_page
|
|
52
|
-
|
|
53
|
-
# set up per page options
|
|
54
|
-
per_page_options = per_page_options_for_select(items_count)
|
|
55
|
-
|
|
56
|
-
return select_tag(name, options_for_select(per_page_options, per_page), options) if per_page_options.size > 0
|
|
57
|
-
|
|
58
|
-
""
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# utility methods
|
|
62
|
-
#------------------------------------------------------------------------------
|
|
63
|
-
def page_options_for_select(items_count, per_page)
|
|
64
|
-
page_count = per_page > 0 ? (items_count.to_f / per_page.to_f).ceil : 1
|
|
65
|
-
page_options = []
|
|
66
|
-
if page_count > 1
|
|
67
|
-
page_count.times do |page|
|
|
68
|
-
page_number = page + 1
|
|
69
|
-
page_options << ["Page #{page_number} of #{page_count}", page_number]
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
page_options
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
def per_page_options_for_select(items_count)
|
|
76
|
-
per_page_options = []
|
|
77
|
-
per_page_values = [10, 20, 30, 40, 50, 75, 100, 125, 150, 175, 200, 300, 400, 500, 1000, 1500, 2000]
|
|
78
|
-
per_page_values.each do |per_page_num|
|
|
79
|
-
if items_count > per_page_num
|
|
80
|
-
per_page_options << ["#{per_page_num} per page", per_page_num] if per_page_num > 0
|
|
81
|
-
else
|
|
82
|
-
break
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
if per_page_options.size > 0
|
|
87
|
-
per_page_options << ["Show all #{items_count}", 0]
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
per_page_options
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def determine_form_prefix(searcher)
|
|
94
|
-
"#{searcher.class.name.underscore.gsub(/_searcher/, "").pluralize}_search_"
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
ActionController::Base.helper BinaryLogic::SearchGasm::Helpers
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
module BinaryLogic
|
|
2
|
-
module Searchgasm
|
|
3
|
-
module Search
|
|
4
|
-
class Condition
|
|
5
|
-
include Utilities
|
|
6
|
-
|
|
7
|
-
BLACKLISTED_WORDS = ('a'..'z').to_a + ["about", "an", "are", "as", "at", "be", "by", "com", "de", "en", "for", "from", "how", "in", "is", "it", "la", "of", "on", "or", "that", "the", "the", "this", "to", "und", "was", "what", "when", "where", "who", "will", "with", "www"] # from ranks.nl
|
|
8
|
-
attr_accessor :column, :condition, :name, :klass
|
|
9
|
-
attr_reader :value
|
|
10
|
-
|
|
11
|
-
class << self
|
|
12
|
-
def generate_name(column, condition)
|
|
13
|
-
name_parts = []
|
|
14
|
-
name_parts << (column.is_a?(String) ? column : column.name) unless column.blank?
|
|
15
|
-
name_parts << condition unless condition.blank?
|
|
16
|
-
name_parts.join("_")
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def initialize(condition, klass, column = nil)
|
|
21
|
-
raise(ArgumentError, "#{klass.name} must acts_as_tree to use the :#{condition} condition") if requires_tree?(condition) && !has_tree?(klass)
|
|
22
|
-
|
|
23
|
-
self.condition = condition
|
|
24
|
-
self.name = self.class.generate_name(column, condition)
|
|
25
|
-
self.klass = klass
|
|
26
|
-
self.column = column.is_a?(String) ? klass.columns_hash[column] : column
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def explicitly_set_value=(value)
|
|
30
|
-
@explicitly_set_value = value
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Need this if someone wants to actually use nil in a meaningful way
|
|
34
|
-
def explicitly_set_value?
|
|
35
|
-
@explicitly_set_value == true
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def has_tree?(klass = klass)
|
|
39
|
-
!klass.reflect_on_association(:parent).nil?
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def ignore_blanks?
|
|
43
|
-
![:equals, :does_not_equal].include?(condition)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def quote_column_name(column_name)
|
|
47
|
-
klass.connection.quote_column_name(column_name)
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def quoted_column_name
|
|
51
|
-
quote_column_name(column.name)
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def requires_tree?(condition = condition)
|
|
55
|
-
[:child_of, :sibling_of, :descendent_of, :inclusive_descendent_of].include?(condition)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def sanitize
|
|
59
|
-
return unless explicitly_set_value?
|
|
60
|
-
v = value
|
|
61
|
-
v = v.utc if false && [:time, :timestamp, :datetime].include?(column.type) && klass.time_zone_aware_attributes && !klass.skip_time_zone_conversion_for_attributes.include?(column.name.to_sym)
|
|
62
|
-
generate_conditions(condition, v)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def table_name
|
|
66
|
-
klass.connection.quote_table_name(klass.table_name)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def value
|
|
70
|
-
@value.is_a?(String) ? column.type_cast(@value) : @value
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def value=(v)
|
|
74
|
-
return if ignore_blanks? && v.blank?
|
|
75
|
-
self.explicitly_set_value = true
|
|
76
|
-
@value = v
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
private
|
|
80
|
-
def generate_conditions(condition, value)
|
|
81
|
-
if [:equals, :does_not_equal].include?(condition)
|
|
82
|
-
# Let ActiveRecord handle this
|
|
83
|
-
sql = klass.send(:sanitize_sql_hash_for_conditions, {column.name => value})
|
|
84
|
-
if condition == :does_not_equal
|
|
85
|
-
sql.gsub!(/ IS /, " IS NOT ")
|
|
86
|
-
sql.gsub!(/ BETWEEN /, " NOT BETWEEN ")
|
|
87
|
-
sql.gsub!(/ IN /, " NOT IN ")
|
|
88
|
-
sql.gsub!(/=/, "!=")
|
|
89
|
-
end
|
|
90
|
-
return [sql]
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
strs = []
|
|
94
|
-
subs = []
|
|
95
|
-
|
|
96
|
-
if value.is_a?(Array)
|
|
97
|
-
merge_conditions(*value.collect { |v| generate_conditions(condition, v) })
|
|
98
|
-
else
|
|
99
|
-
case condition
|
|
100
|
-
when :begins_with
|
|
101
|
-
search_parts = value.split(/ /)
|
|
102
|
-
search_parts.each do |search_part|
|
|
103
|
-
strs << "#{table_name}.#{quoted_column_name} LIKE ?"
|
|
104
|
-
subs << "#{search_part}%"
|
|
105
|
-
end
|
|
106
|
-
when :contains
|
|
107
|
-
strs << "#{table_name}.#{quoted_column_name} LIKE ?"
|
|
108
|
-
subs << "%#{value}%"
|
|
109
|
-
when :ends_with
|
|
110
|
-
search_parts = value.split(/ /)
|
|
111
|
-
search_parts.each do |search_part|
|
|
112
|
-
strs << "#{table_name}.#{quoted_column_name} LIKE ?"
|
|
113
|
-
subs << "%#{search_part}"
|
|
114
|
-
end
|
|
115
|
-
when :greater_than
|
|
116
|
-
strs << "#{table_name}.#{quoted_column_name} > ?"
|
|
117
|
-
subs << value
|
|
118
|
-
when :greater_than_or_equal_to
|
|
119
|
-
strs << "#{table_name}.#{quoted_column_name} >= ?"
|
|
120
|
-
subs << value
|
|
121
|
-
when :keywords
|
|
122
|
-
search_parts = value.gsub(/,/, " ").split(/ /).collect { |word| word.downcase.gsub(/[^[:alnum:]]/, ''); }.uniq.select { |word| !BLACKLISTED_WORDS.include?(word.downcase) && !word.blank? }
|
|
123
|
-
search_parts.each do |search_part|
|
|
124
|
-
strs << "#{table_name}.#{quoted_column_name} LIKE ?"
|
|
125
|
-
subs << "%#{search_part}%"
|
|
126
|
-
end
|
|
127
|
-
when :less_than
|
|
128
|
-
strs << "#{table_name}.#{quoted_column_name} < ?"
|
|
129
|
-
subs << value
|
|
130
|
-
when :less_than_or_equal_to
|
|
131
|
-
strs << "#{table_name}.#{quoted_column_name} <= ?"
|
|
132
|
-
subs << value
|
|
133
|
-
when :child_of
|
|
134
|
-
parent_association = klass.reflect_on_association(:parent)
|
|
135
|
-
foreign_key_name = (parent_association && parent_association.options[:foreign_key]) || "parent_id"
|
|
136
|
-
strs << "#{table_name}.#{quote_column_name(foreign_key_name)} = ?"
|
|
137
|
-
subs << value
|
|
138
|
-
when :sibling_of
|
|
139
|
-
parent_association = klass.reflect_on_association(:parent)
|
|
140
|
-
foreign_key_name = (parent_association && parent_association.options[:foreign_key]) || "parent_id"
|
|
141
|
-
parent_id = klass.find(value).send(foreign_key_name)
|
|
142
|
-
return generate_conditions(:child_of, parent_id)
|
|
143
|
-
when :descendent_of
|
|
144
|
-
# Wish I knew how to do this in SQL
|
|
145
|
-
root = klass.find(value) rescue return
|
|
146
|
-
condition_strs = []
|
|
147
|
-
all_children_ids(root).each do |child_id|
|
|
148
|
-
condition_strs << "#{table_name}.#{quote_column_name(klass.primary_key)} = ?"
|
|
149
|
-
subs << child_id
|
|
150
|
-
end
|
|
151
|
-
strs << condition_strs.join(" OR ")
|
|
152
|
-
when :inclusive_descendent_of
|
|
153
|
-
return merge_conditions(["#{table_name}.#{quote_column_name(klass.primary_key)} = ?", value], generate_conditions(:descendent_of, value), :any => true)
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
[strs.join(" AND "), *subs]
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def all_children_ids(record)
|
|
161
|
-
ids = record.children.collect { |child| child.send(klass.primary_key) }
|
|
162
|
-
record.children.each { |child| ids += all_children_ids(child) }
|
|
163
|
-
ids
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
end
|
|
File without changes
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper.rb'
|
|
2
|
-
|
|
3
|
-
class TestSearchgasmCondition < 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_generate_name
|
|
16
|
-
name = BinaryLogic::Searchgasm::Search::Condition.generate_name(Account.columns_hash["id"], :equals)
|
|
17
|
-
assert_equal name, "id_equals"
|
|
18
|
-
|
|
19
|
-
name = BinaryLogic::Searchgasm::Search::Condition.generate_name("test", :equals)
|
|
20
|
-
assert_equal name, "test_equals"
|
|
21
|
-
|
|
22
|
-
name = BinaryLogic::Searchgasm::Search::Condition.generate_name("test", "")
|
|
23
|
-
assert_equal name, "test"
|
|
24
|
-
|
|
25
|
-
name = BinaryLogic::Searchgasm::Search::Condition.generate_name("test", nil)
|
|
26
|
-
assert_equal name, "test"
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def test_initialize
|
|
30
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
31
|
-
assert_equal condition.condition, :equals
|
|
32
|
-
assert_equal condition.name, "id_equals"
|
|
33
|
-
assert_equal condition.klass, Account
|
|
34
|
-
assert_equal condition.column, Account.columns_hash["id"]
|
|
35
|
-
|
|
36
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, "id")
|
|
37
|
-
assert_equal condition.column, Account.columns_hash["id"]
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def test_explicitly_set_value
|
|
41
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
42
|
-
assert !condition.explicitly_set_value?
|
|
43
|
-
condition.value = nil
|
|
44
|
-
assert condition.explicitly_set_value?
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def test_ignore_blanks?
|
|
48
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
49
|
-
assert !condition.ignore_blanks?
|
|
50
|
-
|
|
51
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:greater_than, Account, Account.columns_hash["id"])
|
|
52
|
-
assert condition.ignore_blanks?
|
|
53
|
-
|
|
54
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:contains, Account, Account.columns_hash["name"])
|
|
55
|
-
assert condition.ignore_blanks?
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def test_sanitize
|
|
59
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
60
|
-
condition.value = 12
|
|
61
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" = 12"]
|
|
62
|
-
|
|
63
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
64
|
-
condition.value = nil
|
|
65
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" IS NULL"]
|
|
66
|
-
|
|
67
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
68
|
-
condition.value = (1..100)
|
|
69
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" BETWEEN 1 AND 100"]
|
|
70
|
-
|
|
71
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:equals, Account, Account.columns_hash["id"])
|
|
72
|
-
condition.value = [1,2,3,4,5]
|
|
73
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" IN (1,2,3,4,5)"]
|
|
74
|
-
|
|
75
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:does_not_equal, Account, Account.columns_hash["id"])
|
|
76
|
-
condition.value = 12
|
|
77
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" != 12"]
|
|
78
|
-
|
|
79
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:does_not_equal, Account, Account.columns_hash["id"])
|
|
80
|
-
condition.value = nil
|
|
81
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" IS NOT NULL"]
|
|
82
|
-
|
|
83
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:does_not_equal, Account, Account.columns_hash["id"])
|
|
84
|
-
condition.value = (1..100)
|
|
85
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" NOT BETWEEN 1 AND 100"]
|
|
86
|
-
|
|
87
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:does_not_equal, Account, Account.columns_hash["id"])
|
|
88
|
-
condition.value = [1,2,3,4,5]
|
|
89
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" NOT IN (1,2,3,4,5)"]
|
|
90
|
-
|
|
91
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:begins_with, Account, Account.columns_hash["name"])
|
|
92
|
-
condition.value = "Binary"
|
|
93
|
-
assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "Binary%"]
|
|
94
|
-
|
|
95
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:contains, Account, Account.columns_hash["name"])
|
|
96
|
-
condition.value = "Binary"
|
|
97
|
-
assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "%Binary%"]
|
|
98
|
-
|
|
99
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:ends_with, Account, Account.columns_hash["name"])
|
|
100
|
-
condition.value = "Binary"
|
|
101
|
-
assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ?", "%Binary"]
|
|
102
|
-
|
|
103
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:greater_than, Account, Account.columns_hash["id"])
|
|
104
|
-
condition.value = 2
|
|
105
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" > ?", 2]
|
|
106
|
-
|
|
107
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:greater_than_or_equal_to, Account, Account.columns_hash["id"])
|
|
108
|
-
condition.value = 2
|
|
109
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" >= ?", 2]
|
|
110
|
-
|
|
111
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:keywords, Account, Account.columns_hash["name"])
|
|
112
|
-
condition.value = "freedom yeah, freedom YEAH right"
|
|
113
|
-
assert_equal condition.sanitize, ["\"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ?", "%freedom%", "%yeah%", "%right%"]
|
|
114
|
-
|
|
115
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:keywords, Account, Account.columns_hash["name"])
|
|
116
|
-
condition.value = "$^&*()!"
|
|
117
|
-
assert_equal condition.sanitize, [""]
|
|
118
|
-
|
|
119
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:less_than, Account, Account.columns_hash["id"])
|
|
120
|
-
condition.value = 2
|
|
121
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" < ?", 2]
|
|
122
|
-
|
|
123
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:less_than_or_equal_to, Account, Account.columns_hash["id"])
|
|
124
|
-
condition.value = 2
|
|
125
|
-
assert_equal condition.sanitize, ["\"accounts\".\"id\" <= ?", 2]
|
|
126
|
-
|
|
127
|
-
assert_raise(ArgumentError) { BinaryLogic::Searchgasm::Search::Condition.new(:descendent_of, Account, nil) }
|
|
128
|
-
|
|
129
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:child_of, User, nil)
|
|
130
|
-
condition.value = 1
|
|
131
|
-
assert_equal condition.sanitize, ["\"users\".\"parent_id\" = ?", 1]
|
|
132
|
-
|
|
133
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:sibling_of, User, nil)
|
|
134
|
-
condition.value = 2
|
|
135
|
-
assert_equal condition.sanitize, ["\"users\".\"parent_id\" = ?", 1]
|
|
136
|
-
|
|
137
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:descendent_of, User, nil)
|
|
138
|
-
condition.value = 1
|
|
139
|
-
assert_equal condition.sanitize, ["\"users\".\"id\" = ? OR \"users\".\"id\" = ?", 2, 3]
|
|
140
|
-
|
|
141
|
-
condition = BinaryLogic::Searchgasm::Search::Condition.new(:inclusive_descendent_of, User, nil)
|
|
142
|
-
condition.value = 1
|
|
143
|
-
assert_equal condition.sanitize, ["(\"users\".\"id\" = ?) OR (\"users\".\"id\" = ? OR \"users\".\"id\" = ?)", 1, 2, 3]
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def test_value
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
end
|