searchgasm 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -0
- data/lib/searchgasm/condition/base.rb +1 -8
- data/lib/searchgasm/conditions/base.rb +32 -19
- data/lib/searchgasm/search/base.rb +11 -5
- data/lib/searchgasm/search/conditions.rb +8 -1
- data/lib/searchgasm/search/ordering.rb +3 -1
- data/lib/searchgasm/version.rb +1 -1
- data/searchgasm.gemspec +3 -3
- data/test/test_condition_base.rb +0 -12
- data/test/test_conditions_base.rb +16 -5
- data/test/test_search_base.rb +20 -4
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
== 1.1.3 released 2008-09-22
|
2
|
+
|
3
|
+
* Setting a condition to nil removes it if the condition is set to ignore blanks
|
4
|
+
* Setting search.conditions = "some sql" will reset ALL conditions. Alternatively search.conditions => {:first_name_contains => "Ben"} will overwrite "some sql". The same goes with search.conditions.first_name_contains = "Ben".
|
5
|
+
* Fixed bug with inspect
|
6
|
+
* Other small performance enhancements with memoized attributes
|
7
|
+
|
1
8
|
== 1.1.2 released 2008-09-22
|
2
9
|
|
3
10
|
* Fixed bug with select control types not using :search_obj to determine its select values.
|
@@ -8,7 +8,6 @@ module Searchgasm
|
|
8
8
|
include Shared::Utilities
|
9
9
|
|
10
10
|
attr_accessor :column, :klass
|
11
|
-
attr_reader :value
|
12
11
|
class_inheritable_accessor :ignore_blanks, :type_cast_value
|
13
12
|
self.ignore_blanks = true
|
14
13
|
self.type_cast_value = true
|
@@ -116,20 +115,14 @@ module Searchgasm
|
|
116
115
|
to_conditions(v)
|
117
116
|
end
|
118
117
|
end
|
119
|
-
|
120
|
-
# Should the value be automatically type casted
|
121
|
-
def type_cast_value?
|
122
|
-
true
|
123
|
-
end
|
124
118
|
|
125
119
|
# The value for the condition
|
126
120
|
def value
|
127
121
|
self.class.type_cast_value? && @value.is_a?(String) ? column.type_cast(@value) : @value
|
128
122
|
end
|
129
123
|
|
130
|
-
# Sets the value for the condition
|
124
|
+
# Sets the value for the condition
|
131
125
|
def value=(v)
|
132
|
-
return if self.class.ignore_blanks? && v.blank?
|
133
126
|
self.explicitly_set_value = true
|
134
127
|
@value = v
|
135
128
|
end
|
@@ -9,7 +9,7 @@ module Searchgasm
|
|
9
9
|
include Shared::Searching
|
10
10
|
include Shared::VirtualClasses
|
11
11
|
|
12
|
-
attr_accessor :any, :relationship_name
|
12
|
+
attr_accessor :any, :relationship_name
|
13
13
|
|
14
14
|
class << self
|
15
15
|
attr_accessor :added_klass_conditions, :added_column_conditions, :added_associations
|
@@ -116,36 +116,35 @@ module Searchgasm
|
|
116
116
|
end
|
117
117
|
|
118
118
|
def inspect
|
119
|
-
|
120
|
-
conditions_hash[:sql] = sql if sql
|
121
|
-
conditions_hash[:protected] = true if protected?
|
122
|
-
conditions_hash.inspect
|
119
|
+
"#<#{klass}Conditions#{conditions.blank? ? "" : " #{conditions.inspect}"}>"
|
123
120
|
end
|
124
121
|
|
125
122
|
# Sanitizes the conditions down into conditions that ActiveRecord::Base.find can understand.
|
126
123
|
def sanitize
|
127
|
-
conditions
|
128
|
-
|
129
|
-
merged_conditions = merge_conditions(conditions, sql, :any => any)
|
130
|
-
merged_conditions
|
124
|
+
return @conditions if @conditions
|
125
|
+
merge_conditions(*(objects.collect { |object| object.sanitize } << {:any => any}))
|
131
126
|
end
|
132
127
|
|
133
128
|
# Allows you to set the conditions via a hash.
|
134
|
-
def conditions=(
|
135
|
-
case
|
136
|
-
when Hash
|
137
|
-
assert_valid_conditions(
|
138
|
-
remove_conditions_from_protected_assignement(
|
139
|
-
next if
|
140
|
-
send("#{condition}=",
|
129
|
+
def conditions=(value)
|
130
|
+
case value
|
131
|
+
when Hash
|
132
|
+
assert_valid_conditions(value)
|
133
|
+
remove_conditions_from_protected_assignement(value).each do |condition, condition_value|
|
134
|
+
next if condition_value.blank? # ignore blanks on mass assignments
|
135
|
+
send("#{condition}=", condition_value)
|
141
136
|
end
|
142
137
|
else
|
143
|
-
|
138
|
+
reset_objects!
|
139
|
+
@conditions = value
|
144
140
|
end
|
145
141
|
end
|
146
142
|
|
147
143
|
# All of the active conditions (conditions that have been set)
|
148
144
|
def conditions
|
145
|
+
return @conditions if @conditions
|
146
|
+
return if objects.blank?
|
147
|
+
|
149
148
|
conditions_hash = {}
|
150
149
|
objects.each do |object|
|
151
150
|
if object.class < Searchgasm::Conditions::Base
|
@@ -178,7 +177,7 @@ module Searchgasm
|
|
178
177
|
@#{association.name}
|
179
178
|
end
|
180
179
|
|
181
|
-
def #{association.name}=(conditions); #{association.name}.conditions = conditions; end
|
180
|
+
def #{association.name}=(conditions); @conditions = nil; #{association.name}.conditions = conditions; end
|
182
181
|
def reset_#{association.name}!; objects.delete(#{association.name}); @#{association.name} = nil; end
|
183
182
|
end_eval
|
184
183
|
end
|
@@ -214,7 +213,16 @@ module Searchgasm
|
|
214
213
|
end
|
215
214
|
|
216
215
|
def #{name}; #{name}_object.value; end
|
217
|
-
|
216
|
+
|
217
|
+
def #{name}=(value)
|
218
|
+
if value.blank? && #{name}_object.class.ignore_blanks?
|
219
|
+
reset_#{name}!
|
220
|
+
else
|
221
|
+
@conditions = nil
|
222
|
+
#{name}_object.value = value
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
218
226
|
def reset_#{name}!; objects.delete(#{name}_object); @#{name} = nil; end
|
219
227
|
end_eval
|
220
228
|
end
|
@@ -253,6 +261,11 @@ module Searchgasm
|
|
253
261
|
@objects ||= []
|
254
262
|
end
|
255
263
|
|
264
|
+
def reset_objects!
|
265
|
+
objects.each { |object| eval("@#{object.name} = nil") }
|
266
|
+
objects.clear
|
267
|
+
end
|
268
|
+
|
256
269
|
def remove_conditions_from_protected_assignement(conditions)
|
257
270
|
return conditions if klass.accessible_conditions.nil? && klass.protected_conditions.nil?
|
258
271
|
if klass.accessible_conditions
|
@@ -55,19 +55,25 @@ module Searchgasm #:nodoc:
|
|
55
55
|
# Makes using searchgasm in the console less annoying and keeps the output meaningful and useful
|
56
56
|
def inspect
|
57
57
|
current_find_options = {}
|
58
|
-
AR_OPTIONS.each do |option|
|
58
|
+
(AR_OPTIONS - [:conditions]).each do |option|
|
59
59
|
value = send(option)
|
60
60
|
next if value.nil?
|
61
61
|
current_find_options[option] = value
|
62
62
|
end
|
63
|
+
conditions_value = conditions.conditions
|
64
|
+
current_find_options[:conditions] = conditions_value unless conditions_value.blank?
|
63
65
|
current_find_options[:scope] = scope unless scope.blank?
|
64
66
|
"#<#{klass}Search #{current_find_options.inspect}>"
|
65
67
|
end
|
66
68
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
69
|
+
def joins=(value)
|
70
|
+
@memoized_joins = nil
|
71
|
+
@joins = value
|
72
|
+
end
|
73
|
+
|
74
|
+
def joins
|
75
|
+
@memoized_joins ||= @joins
|
76
|
+
end
|
71
77
|
|
72
78
|
def limit=(value)
|
73
79
|
@set_limit = true
|
@@ -9,6 +9,7 @@ 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
|
12
13
|
alias_method_chain :joins, :conditions
|
13
14
|
alias_method_chain :sanitize, :conditions
|
14
15
|
end
|
@@ -33,6 +34,7 @@ module Searchgasm
|
|
33
34
|
# now you can create the rest of your search and your "scope" will get merged into your final SQL.
|
34
35
|
# What this does is determine if the value a hash or a conditions object, if not it sets it up as a scope.
|
35
36
|
def conditions_with_conditions=(values)
|
37
|
+
@memoized_joins = nil
|
36
38
|
case values
|
37
39
|
when Searchgasm::Conditions::Base
|
38
40
|
@conditions = values
|
@@ -41,12 +43,17 @@ module Searchgasm
|
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
46
|
+
def conditions_with_conditions
|
47
|
+
@memoized_joins = nil # have to assume they are calling a condition on a relationship
|
48
|
+
conditions_without_conditions
|
49
|
+
end
|
50
|
+
|
44
51
|
# Tells searchgasm what relationships to join during the search. This is based on what conditions you set.
|
45
52
|
#
|
46
53
|
# <b>Be careful!</b>
|
47
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.
|
48
55
|
def joins_with_conditions
|
49
|
-
merge_joins(joins_without_conditions, conditions.joins)
|
56
|
+
@memoized_joins ||= merge_joins(joins_without_conditions, conditions.joins)
|
50
57
|
end
|
51
58
|
|
52
59
|
def sanitize_with_conditions(searching = true) # :nodoc:
|
@@ -24,13 +24,14 @@ module Searchgasm
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def joins_with_ordering # :nodoc:
|
27
|
-
merge_joins(joins_without_ordering, order_by_joins)
|
27
|
+
@memoized_joins ||= merge_joins(joins_without_ordering, order_by_joins)
|
28
28
|
end
|
29
29
|
|
30
30
|
def order_with_ordering=(value) # :nodoc
|
31
31
|
@order_by = nil
|
32
32
|
@order_as = nil
|
33
33
|
self.order_by_joins.clear
|
34
|
+
@memoized_joins = nil
|
34
35
|
self.order_without_ordering = value
|
35
36
|
end
|
36
37
|
|
@@ -101,6 +102,7 @@ module Searchgasm
|
|
101
102
|
# order_by = [:id, {:user_group => :name}] # => users.id ASC, user_groups.name ASC
|
102
103
|
def order_by=(value)
|
103
104
|
self.order_by_joins.clear
|
105
|
+
@memoized_joins = nil
|
104
106
|
@order_by = get_order_by_value(value)
|
105
107
|
@order = order_by_to_order(@order_by, order_as)
|
106
108
|
@order_by
|
data/lib/searchgasm/version.rb
CHANGED
data/searchgasm.gemspec
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
|
2
|
-
# Gem::Specification for Searchgasm-1.1.
|
2
|
+
# Gem::Specification for Searchgasm-1.1.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: 1.1.
|
8
|
+
version: 1.1.3
|
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-
|
15
|
+
date: 2008-09-23 00:00:00 -04:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
data/test/test_condition_base.rb
CHANGED
@@ -35,18 +35,6 @@ class TestConditionBase < Test::Unit::TestCase
|
|
35
35
|
assert_equal condition.column, Account.columns_hash["id"]
|
36
36
|
end
|
37
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
38
|
def test_ignore_blanks?
|
51
39
|
condition = Searchgasm::Condition::Equals.new(Account, Account.columns_hash["id"])
|
52
40
|
assert !condition.class.ignore_blanks?
|
@@ -146,16 +146,22 @@ class TestConditionsBase < Test::Unit::TestCase
|
|
146
146
|
|
147
147
|
sql = "id in (1,2,3,4)"
|
148
148
|
conditions.conditions = sql
|
149
|
-
assert_equal
|
150
|
-
assert_equal sql, conditions.sql
|
149
|
+
assert_equal sql, conditions.conditions
|
151
150
|
|
152
151
|
v2 = {:id_less_than => 5, :name_begins_with => "Beginning of string"}
|
153
152
|
conditions.conditions = v2
|
154
|
-
assert_equal
|
153
|
+
assert_equal v2, conditions.conditions
|
154
|
+
|
155
|
+
v = {:name_contains => "Binary", :created_at_greater_than => now}
|
156
|
+
conditions.conditions = v
|
157
|
+
assert_equal v2.merge(v), conditions.conditions
|
155
158
|
|
156
159
|
sql2 = "id > 5 and name = 'Test'"
|
157
160
|
conditions.conditions = sql2
|
158
|
-
assert_equal sql2, conditions.
|
161
|
+
assert_equal sql2, conditions.conditions
|
162
|
+
|
163
|
+
conditions.name_contains = "awesome"
|
164
|
+
assert_equal({:name_contains => "awesome"}, conditions.conditions)
|
159
165
|
end
|
160
166
|
|
161
167
|
def test_searching
|
@@ -188,10 +194,15 @@ class TestConditionsBase < Test::Unit::TestCase
|
|
188
194
|
|
189
195
|
def test_ignoring_blanks
|
190
196
|
conditions = Account.new_conditions(:name => "", :created_after => nil)
|
191
|
-
assert_equal(
|
197
|
+
assert_equal(nil, conditions.conditions)
|
192
198
|
conditions.name = ""
|
193
199
|
assert_equal({:name_equals => ""}, conditions.conditions)
|
194
200
|
conditions.created_after = ""
|
195
201
|
assert_equal({:name_equals => ""}, conditions.conditions)
|
196
202
|
end
|
203
|
+
|
204
|
+
def test_inspect
|
205
|
+
conditions = Account.new_conditions
|
206
|
+
assert_nothing_raised { conditions.inspect }
|
207
|
+
end
|
197
208
|
end
|
data/test/test_search_base.rb
CHANGED
@@ -157,13 +157,14 @@ class TestSearchBase < Test::Unit::TestCase
|
|
157
157
|
def test_scope
|
158
158
|
search = Account.new_search!
|
159
159
|
search.conditions = "some sql"
|
160
|
-
|
160
|
+
conditions = search.conditions
|
161
|
+
assert_equal "some sql", search.conditions.conditions
|
161
162
|
search.conditions = nil
|
162
|
-
assert_equal nil, search.conditions.
|
163
|
+
assert_equal nil, search.conditions.conditions
|
163
164
|
search.conditions = "some sql"
|
164
|
-
assert_equal "some sql", search.conditions.
|
165
|
+
assert_equal "some sql", search.conditions.conditions
|
165
166
|
search.conditions = "some sql"
|
166
|
-
assert_equal "some sql", search.conditions.
|
167
|
+
assert_equal "some sql", search.conditions.conditions
|
167
168
|
end
|
168
169
|
|
169
170
|
def test_searching
|
@@ -210,4 +211,19 @@ class TestSearchBase < Test::Unit::TestCase
|
|
210
211
|
def test_method_creation_in_scope
|
211
212
|
# test ot make sure methods are not created across the board for all models
|
212
213
|
end
|
214
|
+
|
215
|
+
=begin
|
216
|
+
# AR desont handle this problem either
|
217
|
+
def test_specifying_includes
|
218
|
+
search = Account.new_search
|
219
|
+
search.include = :users
|
220
|
+
search.conditions.users.first_name_like = "Ben"
|
221
|
+
assert_nothing_raised { search.all }
|
222
|
+
end
|
223
|
+
=end
|
224
|
+
|
225
|
+
def test_inspect
|
226
|
+
search = Account.new_search
|
227
|
+
assert_nothing_raised { search.inspect }
|
228
|
+
end
|
213
229
|
end
|
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: 1.1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Johnson of Binary Logic
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-09-
|
12
|
+
date: 2008-09-23 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|