searchgasm 1.3.5 → 1.4.0

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.
Files changed (49) hide show
  1. data/CHANGELOG.rdoc +11 -0
  2. data/Manifest +12 -3
  3. data/README.rdoc +11 -0
  4. data/TODO.rdoc +4 -1
  5. data/lib/searchgasm/active_record/associations.rb +25 -14
  6. data/lib/searchgasm/active_record/base.rb +49 -0
  7. data/lib/searchgasm/active_record/connection_adapters/mysql_adapter.rb +20 -0
  8. data/lib/searchgasm/active_record/connection_adapters/postgresql_adapter.rb +20 -0
  9. data/lib/searchgasm/active_record/connection_adapters/sqlite_adapter.rb +21 -0
  10. data/lib/searchgasm/condition/base.rb +31 -10
  11. data/lib/searchgasm/condition/inclusive_descendant_of.rb +1 -1
  12. data/lib/searchgasm/condition/not_begin_with.rb +1 -1
  13. data/lib/searchgasm/condition/not_blank.rb +17 -0
  14. data/lib/searchgasm/condition/not_end_with.rb +1 -1
  15. data/lib/searchgasm/condition/not_equal.rb +1 -1
  16. data/lib/searchgasm/condition/not_have_keywords.rb +1 -1
  17. data/lib/searchgasm/condition/not_like.rb +1 -1
  18. data/lib/searchgasm/condition/not_nil.rb +17 -0
  19. data/lib/searchgasm/condition/sibling_of.rb +1 -1
  20. data/lib/searchgasm/conditions/base.rb +9 -7
  21. data/lib/searchgasm/conditions/protection.rb +1 -1
  22. data/lib/searchgasm/helpers/control_types/select.rb +1 -1
  23. data/lib/searchgasm/helpers/utilities.rb +1 -1
  24. data/lib/searchgasm/modifiers/lower.rb +15 -0
  25. data/lib/searchgasm/modifiers/ltrim.rb +15 -0
  26. data/lib/searchgasm/modifiers/rtrim.rb +15 -0
  27. data/lib/searchgasm/modifiers/trim.rb +15 -0
  28. data/lib/searchgasm/modifiers/upper.rb +15 -0
  29. data/lib/searchgasm/search/base.rb +44 -40
  30. data/lib/searchgasm/search/conditions.rb +0 -16
  31. data/lib/searchgasm/search/ordering.rb +0 -8
  32. data/lib/searchgasm/search/searching.rb +0 -6
  33. data/lib/searchgasm/version.rb +2 -2
  34. data/lib/searchgasm.rb +2 -2
  35. data/searchgasm.gemspec +22 -6
  36. data/test/fixtures/cats.yml +3 -0
  37. data/test/fixtures/dogs.yml +3 -0
  38. data/test/test_active_record_base.rb +4 -0
  39. data/test/test_condition_base.rb +4 -4
  40. data/test/test_condition_types.rb +27 -27
  41. data/test/test_conditions_base.rb +22 -1
  42. data/test/test_helper.rb +24 -5
  43. data/test/test_search_base.rb +12 -19
  44. data/test/test_search_conditions.rb +1 -11
  45. data/test/test_search_ordering.rb +3 -4
  46. data/{test/libs → test_libs}/acts_as_tree.rb +0 -0
  47. data/{test/libs → test_libs}/ordered_hash.rb +0 -0
  48. data/{test/libs → test_libs}/rexml_fix.rb +0 -0
  49. metadata +21 -5
@@ -0,0 +1,15 @@
1
+ module Searchgasm
2
+ module Modifiers
3
+ class Upper < Base
4
+ class << self
5
+ def modifier_names
6
+ super + ["upcase", "ucase"]
7
+ end
8
+
9
+ def return_type
10
+ :string
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -22,7 +22,7 @@ module Searchgasm #:nodoc:
22
22
  OPTIONS = SPECIAL_FIND_OPTIONS + AR_OPTIONS # the order is very important, these options get set in this order
23
23
 
24
24
  attr_accessor *AR_OPTIONS
25
- attr_reader :auto_joins
25
+ attr_writer :scope
26
26
 
27
27
  class << self
28
28
  # Used in the ActiveRecord methods to determine if Searchgasm should get involved or not.
@@ -56,8 +56,9 @@ module Searchgasm #:nodoc:
56
56
  # Specific implementation of cloning
57
57
  def clone
58
58
  options = {}
59
- (OPTIONS - [:conditions]).each { |option| options[option] = send(option) }
59
+ (AR_OPTIONS - [:conditions]).each { |option| options[option] = instance_variable_get("@#{option}") }
60
60
  options[:conditions] = conditions.conditions
61
+ SPECIAL_FIND_OPTIONS.each { |option| options[option] = send(option) }
61
62
  obj = self.class.new(options)
62
63
  obj.protect = protected?
63
64
  obj.scope = scope
@@ -79,49 +80,27 @@ module Searchgasm #:nodoc:
79
80
  "#<#{klass}Search #{current_find_options.inspect}>"
80
81
  end
81
82
 
82
- # The method is unique in the fact that if searchgasm is acting_as_filter? then auto joins are not included. Searchgasm should not change the default ActiveRecord behavior.
83
- # If you are running a search via new_search or build_search then auto joins are included.
83
+ # Merges all joins together, including the scopes joins for
84
84
  def joins
85
- joins_sql = ""
86
- all_joins = auto_joins
87
-
88
- case @joins
89
- when String
90
- joins_sql += @joins
91
- else
92
- all_joins = merge_joins(@joins, all_joins)
93
- end
94
-
95
- return if joins_sql.blank? && all_joins.blank?
96
- return joins_sql if all_joins.blank?
97
-
98
- # convert scopes joins to sql, so we can determine which joins to skip
99
- scope_joins_sql = nil
100
- if scope && !scope[:joins].blank?
101
- case scope[:joins]
102
- when String
103
- scope_joins_sql = scope[:joins]
104
- else
105
- join_dependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency.new(klass, scope[:joins], nil)
106
- scope_joins_sql = join_dependency.join_associations.collect { |assoc| assoc.association_join }.join
107
- end
108
- end
109
-
110
- join_dependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency.new(klass, all_joins, nil)
111
- safe_joins = []
112
- join_dependency.join_associations.each do |assoc|
113
- join_sql = assoc.association_join
114
-
115
- #debugger if scope && scope[:joins].include?("LEFT OUTER JOIN \"users\"")
116
- safe_joins << join_sql if scope_joins_sql.blank? || !scope_joins_sql.include?(join_sql.gsub(/(LEFT OUTER JOIN|INNER JOIN)/i, "").strip)
117
- end
85
+ all_joins = (safe_to_array(conditions.auto_joins) + safe_to_array(order_by_auto_joins) + safe_to_array(priority_order_by_auto_joins) + safe_to_array(@joins)).uniq
86
+ # For partial backwards compatibility, delete if the scope contains conflicts, AR 2.2 does this for you
87
+ scope_joins = safe_to_array(scope && scope[:joins])
88
+ all_joins.delete_if { |j| scope_joins.include?(j) } unless scope_joins.blank?
89
+ all_joins.size <= 1 ? all_joins.first : all_joins
90
+ end
91
+ =begin
92
+ def joins
93
+ auto_joins = joins_to_sql_array((safe_to_array(conditions.auto_joins) + safe_to_array(order_by_auto_joins) + safe_to_array(priority_order_by_auto_joins)).uniq)
94
+ scope_joins = joins_to_sql_array(scope && scope[:joins])
95
+ include_joins = joins_to_sql_array(include)
118
96
 
97
+ #raise auto_joins.inspect if conditions.respond_to?(:dogs) && conditions.dogs.id == 1
119
98
 
99
+ auto_joins.delete_if { |auto_join| scope_joins.include?(auto_join) || include_joins.include?(auto_join) }
120
100
 
121
- joins_sql += " " unless joins_sql.blank?
122
- joins_sql += safe_joins.join
101
+ #raise auto_joins.inspect if auto_joins.size > 1
123
102
  end
124
-
103
+ =end
125
104
  def limit=(value)
126
105
  @set_limit = true
127
106
  @limit = value.blank? || value == 0 ? nil : value.to_i
@@ -158,6 +137,31 @@ module Searchgasm #:nodoc:
158
137
  def scope
159
138
  @scope ||= {}
160
139
  end
140
+
141
+ private
142
+ def joins_to_sql_array(joins)
143
+ unless array_of_strings?(safe_to_array(joins))
144
+ join_dependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency.new(klass, joins, nil)
145
+ join_dependency.join_associations.collect { |assoc| assoc.association_join }
146
+ else
147
+ joins.is_a?(Array) ? joins : safe_to_array(joins)
148
+ end
149
+ end
150
+
151
+ def safe_to_array(o)
152
+ case o
153
+ when NilClass
154
+ []
155
+ when Array
156
+ o
157
+ else
158
+ [o]
159
+ end
160
+ end
161
+
162
+ def array_of_strings?(o)
163
+ o.is_a?(Array) && o.all?{|obj| obj.is_a?(String)}
164
+ end
161
165
  end
162
166
  end
163
167
  end
@@ -9,8 +9,6 @@ module Searchgasm
9
9
  klass.class_eval do
10
10
  alias_method_chain :initialize, :conditions
11
11
  alias_method_chain :conditions=, :conditions
12
- alias_method_chain :conditions, :conditions
13
- alias_method_chain :auto_joins, :conditions
14
12
  alias_method_chain :sanitize, :conditions
15
13
  end
16
14
  end
@@ -34,7 +32,6 @@ module Searchgasm
34
32
  # now you can create the rest of your search and your "scope" will get merged into your final SQL.
35
33
  # What this does is determine if the value a hash or a conditions object, if not it sets it up as a scope.
36
34
  def conditions_with_conditions=(values)
37
- @memoized_auto_joins = nil
38
35
  case values
39
36
  when Searchgasm::Conditions::Base
40
37
  @conditions = values
@@ -43,19 +40,6 @@ module Searchgasm
43
40
  end
44
41
  end
45
42
 
46
- def conditions_with_conditions
47
- @memoized_auto_joins = nil # have to assume they are calling a condition on a relationship
48
- conditions_without_conditions
49
- end
50
-
51
- # Tells searchgasm what relationships to join during the search. This is based on what conditions you set.
52
- #
53
- # <b>Be careful!</b>
54
- # ActiveRecord associations can be an SQL train wreck. Make sure you think about what you are searching and that you aren't joining a table with a million records.
55
- def auto_joins_with_conditions
56
- @memoized_auto_joins ||= merge_joins(auto_joins_without_conditions, conditions.auto_joins)
57
- end
58
-
59
43
  def sanitize_with_conditions(searching = true) # :nodoc:
60
44
  find_options = sanitize_without_conditions(searching)
61
45
  if conditions_obj = find_options.delete(:conditions)
@@ -18,22 +18,16 @@ module Searchgasm
18
18
  module Ordering
19
19
  def self.included(klass)
20
20
  klass.class_eval do
21
- alias_method_chain :auto_joins, :ordering
22
21
  alias_method_chain :order=, :ordering
23
22
  alias_method_chain :sanitize, :ordering
24
23
  attr_reader :priority_order
25
24
  end
26
25
  end
27
26
 
28
- def auto_joins_with_ordering # :nodoc:
29
- @memoized_auto_joins ||= merge_joins(auto_joins_without_ordering, order_by_auto_joins, priority_order_by_auto_joins)
30
- end
31
-
32
27
  def order_with_ordering=(value) # :nodoc
33
28
  @order_by = nil
34
29
  @order_as = nil
35
30
  @order_by_auto_joins = nil
36
- @memoized_auto_joins = nil
37
31
  self.order_without_ordering = value
38
32
  end
39
33
 
@@ -93,7 +87,6 @@ module Searchgasm
93
87
  # order_by = [:id, {:user_group => :name}] # => users.id ASC, user_groups.name ASC
94
88
  def order_by=(value)
95
89
  @order_by_auto_joins = nil
96
- @memoized_auto_joins = nil
97
90
  @order_by = get_order_by_value(value)
98
91
  @order = order_by_to_order(@order_by, @order_as)
99
92
  @order_by
@@ -122,7 +115,6 @@ module Searchgasm
122
115
  # Same as order_by= but for your priority order. See priority_order= for more informaton on priority_order.
123
116
  def priority_order_by=(value)
124
117
  @priority_order_by_auto_joins = nil
125
- @memoized_auto_joins = nil
126
118
  @priority_order_by = get_order_by_value(value)
127
119
  @priority_order = order_by_to_order(@priority_order_by, @priority_order_as)
128
120
  @priority_order_by
@@ -9,12 +9,6 @@ module Searchgasm
9
9
  SEARCH_METHODS = [:all, :find, :first]
10
10
  CALCULATION_METHODS = [:average, :calculate, :count, :maximum, :minimum, :sum]
11
11
 
12
- def self.included(klass)
13
- klass.class_eval do
14
- attr_accessor :scope
15
- end
16
- end
17
-
18
12
  (SEARCH_METHODS + CALCULATION_METHODS).each do |method|
19
13
  class_eval <<-"end_eval", __FILE__, __LINE__
20
14
  def #{method}(*args)
@@ -66,8 +66,8 @@ module Searchgasm
66
66
  end
67
67
 
68
68
  MAJOR = 1
69
- MINOR = 3
70
- TINY = 5
69
+ MINOR = 4
70
+ TINY = 0
71
71
 
72
72
  # The current version as a Version instance
73
73
  CURRENT = new(MAJOR, MINOR, TINY)
data/lib/searchgasm.rb CHANGED
@@ -42,12 +42,12 @@ require "searchgasm/conditions/base"
42
42
  # Condition
43
43
  require "searchgasm/condition/base"
44
44
  require "searchgasm/condition/tree"
45
- SEARCHGASM_CONDITIONS = [:begins_with, :blank, :child_of, :descendant_of, :ends_with, :equals, :greater_than, :greater_than_or_equal_to, :inclusive_descendant_of, :like, :nil, :not_begin_with, :not_end_with, :not_equal, :not_have_keywords, :keywords, :less_than, :less_than_or_equal_to, :sibling_of]
45
+ SEARCHGASM_CONDITIONS = [:begins_with, :blank, :child_of, :descendant_of, :ends_with, :equals, :greater_than, :greater_than_or_equal_to, :inclusive_descendant_of, :like, :nil, :not_begin_with, :not_blank, :not_end_with, :not_equal, :not_have_keywords, :not_nil, :keywords, :less_than, :less_than_or_equal_to, :sibling_of]
46
46
  SEARCHGASM_CONDITIONS.each { |condition| require "searchgasm/condition/#{condition}" }
47
47
 
48
48
  # Modifiers
49
49
  require "searchgasm/modifiers/base"
50
- SEARCHGASM_MODIFIERS = [:day_of_month, :day_of_week, :day_of_year, :hour, :microseconds, :milliseconds, :minute, :month, :second, :week, :year]
50
+ SEARCHGASM_MODIFIERS = [:absolute, :acos, :asin, :atan, :ceil, :char_length, :cos, :cot, :day_of_month, :day_of_week, :day_of_year, :degrees, :exp, :floor, :hex, :hour, :log, :log10, :log2, :lower, :ltrim, :md5, :microseconds, :milliseconds, :minute, :month, :octal, :radians, :round, :rtrim, :second, :sign, :sin, :square_root, :tan, :trim, :upper, :week, :year]
51
51
  SEARCHGASM_MODIFIERS.each { |modifier| require "searchgasm/modifiers/#{modifier}" }
52
52
 
53
53
  # Helpers
data/searchgasm.gemspec CHANGED
@@ -1,18 +1,18 @@
1
1
 
2
- # Gem::Specification for Searchgasm-1.3.5
2
+ # Gem::Specification for Searchgasm-1.4.0
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: 1.3.5
8
+ version: 1.4.0
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-10-08 00:00:00 -04:00
15
+ date: 2008-10-15 00:00:00 -04:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -74,10 +74,12 @@ extra_rdoc_files:
74
74
  - lib/searchgasm/condition/like.rb
75
75
  - lib/searchgasm/condition/nil.rb
76
76
  - lib/searchgasm/condition/not_begin_with.rb
77
+ - lib/searchgasm/condition/not_blank.rb
77
78
  - lib/searchgasm/condition/not_end_with.rb
78
79
  - lib/searchgasm/condition/not_equal.rb
79
80
  - lib/searchgasm/condition/not_have_keywords.rb
80
81
  - lib/searchgasm/condition/not_like.rb
82
+ - lib/searchgasm/condition/not_nil.rb
81
83
  - lib/searchgasm/condition/sibling_of.rb
82
84
  - lib/searchgasm/condition/tree.rb
83
85
  - lib/searchgasm/conditions/base.rb
@@ -112,6 +114,8 @@ extra_rdoc_files:
112
114
  - lib/searchgasm/modifiers/log.rb
113
115
  - lib/searchgasm/modifiers/log10.rb
114
116
  - lib/searchgasm/modifiers/log2.rb
117
+ - lib/searchgasm/modifiers/lower.rb
118
+ - lib/searchgasm/modifiers/ltrim.rb
115
119
  - lib/searchgasm/modifiers/md5.rb
116
120
  - lib/searchgasm/modifiers/microseconds.rb
117
121
  - lib/searchgasm/modifiers/milliseconds.rb
@@ -120,11 +124,14 @@ extra_rdoc_files:
120
124
  - lib/searchgasm/modifiers/octal.rb
121
125
  - lib/searchgasm/modifiers/radians.rb
122
126
  - lib/searchgasm/modifiers/round.rb
127
+ - lib/searchgasm/modifiers/rtrim.rb
123
128
  - lib/searchgasm/modifiers/second.rb
124
129
  - lib/searchgasm/modifiers/sign.rb
125
130
  - lib/searchgasm/modifiers/sin.rb
126
131
  - lib/searchgasm/modifiers/square_root.rb
127
132
  - lib/searchgasm/modifiers/tan.rb
133
+ - lib/searchgasm/modifiers/trim.rb
134
+ - lib/searchgasm/modifiers/upper.rb
128
135
  - lib/searchgasm/modifiers/week.rb
129
136
  - lib/searchgasm/modifiers/year.rb
130
137
  - lib/searchgasm/search/base.rb
@@ -164,10 +171,12 @@ files:
164
171
  - lib/searchgasm/condition/like.rb
165
172
  - lib/searchgasm/condition/nil.rb
166
173
  - lib/searchgasm/condition/not_begin_with.rb
174
+ - lib/searchgasm/condition/not_blank.rb
167
175
  - lib/searchgasm/condition/not_end_with.rb
168
176
  - lib/searchgasm/condition/not_equal.rb
169
177
  - lib/searchgasm/condition/not_have_keywords.rb
170
178
  - lib/searchgasm/condition/not_like.rb
179
+ - lib/searchgasm/condition/not_nil.rb
171
180
  - lib/searchgasm/condition/sibling_of.rb
172
181
  - lib/searchgasm/condition/tree.rb
173
182
  - lib/searchgasm/conditions/base.rb
@@ -202,6 +211,8 @@ files:
202
211
  - lib/searchgasm/modifiers/log.rb
203
212
  - lib/searchgasm/modifiers/log10.rb
204
213
  - lib/searchgasm/modifiers/log2.rb
214
+ - lib/searchgasm/modifiers/lower.rb
215
+ - lib/searchgasm/modifiers/ltrim.rb
205
216
  - lib/searchgasm/modifiers/md5.rb
206
217
  - lib/searchgasm/modifiers/microseconds.rb
207
218
  - lib/searchgasm/modifiers/milliseconds.rb
@@ -210,11 +221,14 @@ files:
210
221
  - lib/searchgasm/modifiers/octal.rb
211
222
  - lib/searchgasm/modifiers/radians.rb
212
223
  - lib/searchgasm/modifiers/round.rb
224
+ - lib/searchgasm/modifiers/rtrim.rb
213
225
  - lib/searchgasm/modifiers/second.rb
214
226
  - lib/searchgasm/modifiers/sign.rb
215
227
  - lib/searchgasm/modifiers/sin.rb
216
228
  - lib/searchgasm/modifiers/square_root.rb
217
229
  - lib/searchgasm/modifiers/tan.rb
230
+ - lib/searchgasm/modifiers/trim.rb
231
+ - lib/searchgasm/modifiers/upper.rb
218
232
  - lib/searchgasm/modifiers/week.rb
219
233
  - lib/searchgasm/modifiers/year.rb
220
234
  - lib/searchgasm/search/base.rb
@@ -232,12 +246,11 @@ files:
232
246
  - Rakefile
233
247
  - README.rdoc
234
248
  - test/fixtures/accounts.yml
249
+ - test/fixtures/cats.yml
250
+ - test/fixtures/dogs.yml
235
251
  - test/fixtures/orders.yml
236
252
  - test/fixtures/user_groups.yml
237
253
  - test/fixtures/users.yml
238
- - test/libs/acts_as_tree.rb
239
- - test/libs/ordered_hash.rb
240
- - test/libs/rexml_fix.rb
241
254
  - test/test_active_record_associations.rb
242
255
  - test/test_active_record_base.rb
243
256
  - test/test_condition_base.rb
@@ -251,6 +264,9 @@ files:
251
264
  - test/test_search_ordering.rb
252
265
  - test/test_search_pagination.rb
253
266
  - test/test_search_protection.rb
267
+ - test_libs/acts_as_tree.rb
268
+ - test_libs/ordered_hash.rb
269
+ - test_libs/rexml_fix.rb
254
270
  - TODO.rdoc
255
271
  - searchgasm.gemspec
256
272
  has_rdoc: true
@@ -0,0 +1,3 @@
1
+ pepper:
2
+ id: 1
3
+ description: pepper meows
@@ -0,0 +1,3 @@
1
+ harry:
2
+ id: 2
3
+ description: harry is hairy
@@ -85,4 +85,8 @@ class TestActiveRecordBase < Test::Unit::TestCase
85
85
  assert_equal Set.new(["id_gt", "name_contains"]), Account.protected_conditions
86
86
  Account.send(:write_inheritable_attribute, :conditions_protected, nil)
87
87
  end
88
+
89
+ def test_includes
90
+ assert_nothing_raised { Account.all(:conditions => {:users => {:first_name_like => "Ben"}}, :include => :users) }
91
+ end
88
92
  end
@@ -22,21 +22,21 @@ class TestConditionBase < Test::Unit::TestCase
22
22
  end
23
23
 
24
24
  def test_initialize
25
- condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
25
+ condition = Searchgasm::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
26
26
  assert_equal condition.klass, Account
27
27
  assert_equal Account.columns_hash["name"], condition.column
28
28
 
29
- condition = Searchgasm::Condition::GreaterThan.new(Account, "id")
29
+ condition = Searchgasm::Condition::GreaterThan.new(Account, :column => "id")
30
30
  assert_equal Account.columns_hash["id"], condition.column
31
31
 
32
- condition = Searchgasm::Condition::GreaterThan.new(Account, "id", :string, "some sql")
32
+ condition = Searchgasm::Condition::GreaterThan.new(Account, :column => "id", :column_type => :string, :column_sql_format => "some sql")
33
33
  assert_equal Account.columns_hash["id"], condition.column
34
34
  condition.value = "awesome"
35
35
  assert_equal ["some sql > ?", "awesome"], condition.sanitize
36
36
  end
37
37
 
38
38
  def test_explicitly_set_value
39
- condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
39
+ condition = Searchgasm::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
40
40
  assert !condition.explicitly_set_value?
41
41
  condition.value = "test"
42
42
  assert condition.explicitly_set_value?
@@ -2,31 +2,31 @@ require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class TestConditionTypes < Test::Unit::TestCase
4
4
  def test_sanitize
5
- condition = Searchgasm::Condition::BeginsWith.new(Account, Account.columns_hash["name"])
5
+ condition = Searchgasm::Condition::BeginsWith.new(Account, :column => Account.columns_hash["name"])
6
6
  condition.value = "Binary"
7
7
  assert_equal ["\"accounts\".\"name\" LIKE ?", "Binary%"], condition.sanitize
8
8
 
9
- condition = Searchgasm::Condition::Blank.new(Account, Account.columns_hash["id"])
9
+ condition = Searchgasm::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
10
10
  condition.value = "true"
11
11
  assert_equal "\"accounts\".\"id\" is NULL or \"accounts\".\"id\" = '' or \"accounts\".\"id\" = false", condition.sanitize
12
12
 
13
- condition = Searchgasm::Condition::Blank.new(Account, Account.columns_hash["id"])
13
+ condition = Searchgasm::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
14
14
  condition.value = "false"
15
15
  assert_equal "\"accounts\".\"id\" is NOT NULL and \"accounts\".\"id\" != '' and \"accounts\".\"id\" != false", condition.sanitize
16
16
 
17
- condition = Searchgasm::Condition::Blank.new(Account, Account.columns_hash["id"])
17
+ condition = Searchgasm::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
18
18
  condition.value = true
19
19
  assert_equal "\"accounts\".\"id\" is NULL or \"accounts\".\"id\" = '' or \"accounts\".\"id\" = false", condition.sanitize
20
20
 
21
- condition = Searchgasm::Condition::Blank.new(Account, Account.columns_hash["id"])
21
+ condition = Searchgasm::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
22
22
  condition.value = false
23
23
  assert_equal "\"accounts\".\"id\" is NOT NULL and \"accounts\".\"id\" != '' and \"accounts\".\"id\" != false", condition.sanitize
24
24
 
25
- condition = Searchgasm::Condition::Blank.new(Account, Account.columns_hash["id"])
25
+ condition = Searchgasm::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
26
26
  condition.value = nil
27
27
  assert_equal nil, condition.sanitize
28
28
 
29
- condition = Searchgasm::Condition::Blank.new(Account, Account.columns_hash["id"])
29
+ condition = Searchgasm::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
30
30
  condition.value = ""
31
31
  assert_equal nil, condition.sanitize
32
32
 
@@ -42,27 +42,27 @@ class TestConditionTypes < Test::Unit::TestCase
42
42
  condition.value = User.find(1)
43
43
  assert_equal ["\"users\".\"id\" = ? OR \"users\".\"id\" = ?", 2, 3], condition.sanitize
44
44
 
45
- condition = Searchgasm::Condition::EndsWith.new(Account, Account.columns_hash["name"])
45
+ condition = Searchgasm::Condition::EndsWith.new(Account, :column => Account.columns_hash["name"])
46
46
  condition.value = "Binary"
47
47
  assert_equal ["\"accounts\".\"name\" LIKE ?", "%Binary"], condition.sanitize
48
48
 
49
- condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
49
+ condition = Searchgasm::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
50
50
  condition.value = 12
51
51
  assert_equal ["\"accounts\".\"id\" = ?", 12], condition.sanitize
52
52
 
53
- condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
53
+ condition = Searchgasm::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
54
54
  condition.value = [1,2,3,4]
55
55
  assert_equal ["\"accounts\".\"id\" IN (?)", [1, 2, 3, 4]], condition.sanitize
56
56
 
57
- condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
57
+ condition = Searchgasm::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
58
58
  condition.value = (1..10)
59
59
  assert_equal ["\"accounts\".\"id\" BETWEEN ? AND ?", 1, 10], condition.sanitize
60
60
 
61
- condition = Searchgasm::Condition::GreaterThan.new(Account, Account.columns_hash["id"])
61
+ condition = Searchgasm::Condition::GreaterThan.new(Account, :column => Account.columns_hash["id"])
62
62
  condition.value = 2
63
63
  assert_equal ["\"accounts\".\"id\" > ?", 2], condition.sanitize
64
64
 
65
- condition = Searchgasm::Condition::GreaterThanOrEqualTo.new(Account, Account.columns_hash["id"])
65
+ condition = Searchgasm::Condition::GreaterThanOrEqualTo.new(Account, :column => Account.columns_hash["id"])
66
66
  condition.value = 2
67
67
  assert_equal ["\"accounts\".\"id\" >= ?", 2], condition.sanitize
68
68
 
@@ -70,59 +70,59 @@ class TestConditionTypes < Test::Unit::TestCase
70
70
  condition.value = User.find(1)
71
71
  assert_equal ["(\"users\".\"id\" = ?) OR (\"users\".\"id\" = ? OR \"users\".\"id\" = ?)", 1, 2, 3], condition.sanitize
72
72
 
73
- condition = Searchgasm::Condition::Like.new(Account, Account.columns_hash["name"])
73
+ condition = Searchgasm::Condition::Like.new(Account, :column => Account.columns_hash["name"])
74
74
  condition.value = "Binary and blah"
75
75
  assert_equal ["\"accounts\".\"name\" LIKE ?", "%Binary and blah%"], condition.sanitize
76
76
 
77
- condition = Searchgasm::Condition::Nil.new(Account, Account.columns_hash["id"])
77
+ condition = Searchgasm::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
78
78
  condition.value = true
79
79
  assert_equal "\"accounts\".\"id\" is NULL", condition.sanitize
80
80
 
81
- condition = Searchgasm::Condition::Nil.new(Account, Account.columns_hash["id"])
81
+ condition = Searchgasm::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
82
82
  condition.value = false
83
83
  assert_equal "\"accounts\".\"id\" is NOT NULL", condition.sanitize
84
84
 
85
- condition = Searchgasm::Condition::Nil.new(Account, Account.columns_hash["id"])
85
+ condition = Searchgasm::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
86
86
  condition.value = "true"
87
87
  assert_equal "\"accounts\".\"id\" is NULL", condition.sanitize
88
88
 
89
- condition = Searchgasm::Condition::Nil.new(Account, Account.columns_hash["id"])
89
+ condition = Searchgasm::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
90
90
  condition.value = "false"
91
91
  assert_equal "\"accounts\".\"id\" is NOT NULL", condition.sanitize
92
92
 
93
- condition = Searchgasm::Condition::Nil.new(Account, Account.columns_hash["id"])
93
+ condition = Searchgasm::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
94
94
  condition.value = nil
95
95
  assert_equal nil, condition.sanitize
96
96
 
97
- condition = Searchgasm::Condition::Nil.new(Account, Account.columns_hash["id"])
97
+ condition = Searchgasm::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
98
98
  condition.value = ""
99
99
  assert_equal nil, condition.sanitize
100
100
 
101
- condition = Searchgasm::Condition::NotEqual.new(Account, Account.columns_hash["id"])
101
+ condition = Searchgasm::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
102
102
  condition.value = 12
103
103
  assert_equal ["\"accounts\".\"id\" != ?", 12], condition.sanitize
104
104
 
105
- condition = Searchgasm::Condition::NotEqual.new(Account, Account.columns_hash["id"])
105
+ condition = Searchgasm::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
106
106
  condition.value = [1,2,3,4]
107
107
  assert_equal ["\"accounts\".\"id\" NOT IN (?)", [1, 2, 3, 4]], condition.sanitize
108
108
 
109
- condition = Searchgasm::Condition::NotEqual.new(Account, Account.columns_hash["id"])
109
+ condition = Searchgasm::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
110
110
  condition.value = (1..10)
111
111
  assert_equal ["\"accounts\".\"id\" NOT BETWEEN ? AND ?", 1, 10], condition.sanitize
112
112
 
113
- condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
113
+ condition = Searchgasm::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
114
114
  condition.value = "freedom yeah, freedom YEAH right"
115
115
  assert_equal ["\"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ?", "%freedom%", "%yeah%", "%right%"], condition.sanitize
116
116
 
117
- condition = Searchgasm::Condition::Keywords.new(Account, Account.columns_hash["name"])
117
+ condition = Searchgasm::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
118
118
  condition.value = "%^$*(^$)"
119
119
  assert_equal nil, condition.sanitize
120
120
 
121
- condition = Searchgasm::Condition::LessThan.new(Account, Account.columns_hash["id"])
121
+ condition = Searchgasm::Condition::LessThan.new(Account, :column => Account.columns_hash["id"])
122
122
  condition.value = 2
123
123
  assert_equal ["\"accounts\".\"id\" < ?", 2], condition.sanitize
124
124
 
125
- condition = Searchgasm::Condition::LessThanOrEqualTo.new(Account, Account.columns_hash["id"])
125
+ condition = Searchgasm::Condition::LessThanOrEqualTo.new(Account, :column => Account.columns_hash["id"])
126
126
  condition.value = 2
127
127
  assert_equal ["\"accounts\".\"id\" <= ?", 2], condition.sanitize
128
128
 
@@ -10,7 +10,7 @@ class TestConditionsBase < Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  def test_association_names
13
- assert_equal ["children", "orders", "account", "parent", "user_groups"], Searchgasm::Cache::UserConditions.association_names
13
+ assert_equal ["dogs", "children", "user_groups", "orders", "account", "parent", "cats"], Searchgasm::Cache::UserConditions.association_names
14
14
  assert_equal ["admin", "orders", "users"], Searchgasm::Cache::AccountConditions.association_names
15
15
  end
16
16
 
@@ -218,4 +218,25 @@ class TestConditionsBase < Test::Unit::TestCase
218
218
  conditions = Searchgasm::Cache::AccountConditions.new
219
219
  assert_equal nil, conditions.id
220
220
  end
221
+
222
+ def test_sti
223
+ #s = User.new_search
224
+ #s.conditions.dogs.description_like = "awesome"
225
+ #s.conditions.cats.description_like = "awesome"
226
+ #s.select = "ass"
227
+ #s.all
228
+
229
+ joins = []
230
+ join_dependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency.new(User, [:dogs, :cats], nil)
231
+ join_dependency.join_associations.each_with_index do |assoc, index|
232
+ #raise assoc.aliased_table_name.inspect if index == 1
233
+ joins << assoc.association_join
234
+ end
235
+ #raise joins.inspect
236
+
237
+ conditions = Searchgasm::Cache::UserConditions.new
238
+ conditions.dogs.description_like = "Harry"
239
+ r = User.reflect_on_association(:dogs)
240
+ #raise r.inspect
241
+ end
221
242
  end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  require "test/unit"
2
2
  require "rubygems"
3
3
  require "ruby-debug"
4
- require "activerecord"
5
- require File.dirname(__FILE__) + '/libs/acts_as_tree'
6
- require File.dirname(__FILE__) + '/libs/ordered_hash'
7
- require File.dirname(__FILE__) + '/libs/rexml_fix'
4
+ require "active_record"
5
+ require File.dirname(__FILE__) + '/../test_libs/acts_as_tree'
6
+ require File.dirname(__FILE__) + '/../test_libs/ordered_hash'
7
+ require File.dirname(__FILE__) + '/../test_libs/rexml_fix'
8
8
  require File.dirname(__FILE__) + '/../lib/searchgasm'
9
9
 
10
10
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
@@ -25,6 +25,8 @@ class User < ActiveRecord::Base
25
25
  acts_as_tree
26
26
  belongs_to :account
27
27
  has_many :orders, :dependent => :destroy
28
+ has_many :cats, :dependent => :destroy
29
+ has_many :dogs, :dependent => :destroy
28
30
  has_and_belongs_to_many :user_groups
29
31
  end
30
32
 
@@ -32,6 +34,16 @@ class Order < ActiveRecord::Base
32
34
  belongs_to :user
33
35
  end
34
36
 
37
+ # STI
38
+ class Animal < ActiveRecord::Base
39
+ end
40
+
41
+ class Dog < Animal
42
+ end
43
+
44
+ class Cat < Animal
45
+ end
46
+
35
47
  class Test::Unit::TestCase
36
48
  def setup_db
37
49
  ActiveRecord::Schema.define(:version => 1) do
@@ -72,11 +84,18 @@ class Test::Unit::TestCase
72
84
  t.text :description
73
85
  t.binary :receipt
74
86
  end
87
+
88
+ create_table :animals do |t|
89
+ t.datetime :created_at
90
+ t.datetime :updated_at
91
+ t.string :type
92
+ t.text :description
93
+ end
75
94
  end
76
95
  end
77
96
 
78
97
  def load_fixtures
79
- fixtures = [:accounts, :orders, :users, :user_groups]
98
+ fixtures = [:accounts, :orders, :users, :user_groups, :dogs, :cats]
80
99
  fixtures.each do |fixture|
81
100
  records = YAML.load(File.read(File.dirname(__FILE__) + "/fixtures/#{fixture.to_s}.yml"))
82
101
  records.each do |name, attributes|