skanev-searchlogic 2.1.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ *.log
3
+ pkg/*
4
+ coverage/*
5
+ doc/*
6
+ benchmarks/*
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,361 @@
1
+ == 2.1.8
2
+
3
+ * Added support for not_like, not_begin_with, not_end_with, and not_null
4
+
5
+ == 2.1.7 released 2009-07-14
6
+
7
+ * Add support for time zones in the Search class when type casting to Time objects.
8
+
9
+ == 2.1.6 released 2009-07-13
10
+
11
+ * Fix bug when trying to set conditions on conflicting column names with associations. Ex: User.has_many :orders, User.order_count_gt(10) would raise an exception because it was trying to set conditions on the count column for the orders table.
12
+
13
+ == 2.1.5 released 2009-07-12
14
+
15
+ * Check for the existence of id before undefining it, fixes bug in ruby 1.9
16
+
17
+ == 2.1.4 released 2009-07-12
18
+
19
+ * Add ActiveRecordConsistency module to help AR remove duplicate joins easier.
20
+
21
+ == 2.1.3 released 2009-07-12
22
+
23
+ * Added a no conflic resolution for other libraries already using the "search" method. If you have a conflict, use "searchlogic".
24
+ * Put the hidden order field in a div, to be valid XHTML.
25
+
26
+ == 2.1.2 released 2009-07-04
27
+
28
+ * Undefine the id method for the Search class, so that it acts like a condition.
29
+
30
+ == 2.1.1 released 2009-06-28
31
+
32
+ * Added inner_join convenience method.
33
+
34
+ == 2.1.0 released 2009-06-28
35
+
36
+ * Added a delete method to the Search class to allow the deleting of conditions off of the object.
37
+ * Add alias_scope feature, lets your create "named scopes" that represent a procedure of named scopes, while at the same time telling Searchlogic it is safe to use in the Search class.
38
+ * Use url_for as the default with form_for since rails does some magic to determine the url to use.
39
+ * Switched to using inner joins instead of left outer joins.
40
+
41
+ == 2.0.1 released 2009-06-20
42
+
43
+ * Allow the chaining of conditions off of a search object. Ex: search.username_like("bjohnson").age_gt(20).all
44
+ * Split out left outer join creation into its own method, allowing you to use it in your own named scopes.
45
+ * Make sure the search objects clone properly.
46
+ * named_scope_options should return the proper options for existing named scopes with an arity greater than 0
47
+
48
+ == 2.0.0
49
+
50
+ * Everything, completely rewritten.
51
+
52
+ == 1.6.6
53
+
54
+ * Fixed time zone issues when searching.
55
+
56
+ == 1.6.5 released 2009-04-2
57
+
58
+ * Skip all associations defined with the :finder_sql option, since conditions can not be chained to them, etc.
59
+ * Fixed bug when using non string values for the keywords condition.
60
+ * Fixed undefined methods link_options error.
61
+ * Return blank conditions when trying to scope blank conditions. Fixes the issue with groups that return a blank SQL string.
62
+ * Fixed issue with blank condition comparing boolean values to non boolean columns.
63
+ * Add utf-8 magic comment for ruby 1.9
64
+
65
+ == 1.6.4 released 2009-01-22
66
+
67
+ * Fixed calling attribute_condition with newer versions of rails.
68
+
69
+ == 1.6.3 released 2008-12-13
70
+
71
+ * Added in avg, count, and sum modifiers for SQLite and Postgres.
72
+ * Added ne for not_equal condition.
73
+ * Ignore :include option for calculations since we don't need it and AR throws an error for polymorphic relationships.
74
+ * Removed sum and count since they can not be in the WHERE statement, but must be in the HAVING statement
75
+ * Added in auto joins for groups so that the joins don't have to be manually specified.
76
+ * Use first column in column_names as the default column for calculations, since a column is required when using DISTINCT and some tables can have more than one primary key.
77
+ * On associations see what the first conditions "any" status is to determine if to join with "ANY" or "AND"
78
+
79
+ == 1.6.2 released 2008-12-13
80
+
81
+ * Remove duplicate joins at a lower level for a cleaner and less brittle approach.
82
+
83
+ == 1.6.1 released 2008-12-8
84
+
85
+ * Group multi faceted conditions since we allow for the mix of "and" and "or"
86
+ * Blank arrays are not meaningful values for conditions
87
+ * Add in a unique identifier for the auto-generated hidden fields so that multiple forms do no present a problem.
88
+
89
+ == 1.6.0 released 2008-12-8
90
+
91
+ * Converted all tree conditions to nested set conditions for performance reasons. A nested set data structure is now required instead of a tree
92
+ * Added DISTINCT to select if you are not provided a select, searching, and have joins.
93
+ * Included :not_like in the list of conditions to be required upon initialization.
94
+
95
+ == 1.5.10 released 2008-12-8
96
+
97
+ * Create class level conditions upon instantiation to avoid conflicts.
98
+
99
+ == 1.5.9 released 2008-12-3
100
+
101
+ * Added metaclass object extension for older version of ActiveSupport.
102
+ * Added average, count, and sum modifiers.
103
+
104
+ == 1.5.8 released 2008-12-3
105
+
106
+ * Removed array support for groups, arrays will set conditions, not create multiple groups. If you need to create multiple groups you will have to pass an array with multiple {:group => my_groups} hashes.
107
+ * Fixed issues with using name spaced models.
108
+ * Added support for multiparameter conditions to support date_select helpers.
109
+
110
+ == 1.5.7 released 2008-11-30
111
+
112
+ * Fixed how grouping works, you can now specify "and_group" and "or_group", just like you can with conditions.
113
+ * Specifying a join type will take priority. So "and_name_like" will ALWAYS join with AND, even is any = true for the search.
114
+
115
+ == 1.5.6 released 2008-11-29
116
+
117
+ * The order conditions are set is now relevant and will be reflected in the SQL. Setting a condition first will make it first in the SQL, setting a condition last will make it last in the SQL.
118
+ * The above allows for prefixing any condition with and_ or or_. Thus letting you create somewhat more complex queries, since the order matters.
119
+ * Added ability to group conditions to mimic parenthesis in raw SQL.
120
+ * Major clean up in the Searchlogic::Conditions name space: faster, smaller, cleaner, and modular.
121
+
122
+ == 1.5.5 released 2008-11-17
123
+
124
+ * Cleaned up class_name configuration for helpers, so that setting them to nil disables the class name
125
+ * Added page_links_div_wrapper and page_links_div_wrapper_class_name configuration option to add an wrapping div around page_links
126
+ * Added page_links_act_like_will_paginate configuration option to help with the transition from will_paginate
127
+
128
+ == 1.5.4 released 2008-11-16
129
+
130
+ * Removed subclass requirement for adding conditions in Conditions::Base
131
+ * Removed DISTINCT in the select clause, instead called uniq! on the results. PostgreSQL complains about DISTINCT and AR uses uniq! when using :includes, so this is the best option.
132
+ * Converted the return for SQLite's strftime to integer from string. This way day of month, day of year, etc works as expected.
133
+
134
+ == 1.5.3 released 2008-10-30
135
+
136
+ * Removed ilike conditions and let the like condition determine if it should use like or ilike depending on the connection adapter.
137
+ * Fixed bug where the keywords condition was removing foreign characters.
138
+ * Added configuration option for specifying your javascript library, to cut down on the javascript Searchlogic adds into the form helpers
139
+ * Fixed bug with "not" conditions to use sanitize method, like it should, instead of to_conditions.
140
+ * Only pass :distinct option for the count calculation
141
+ * Fixed not_nil and not_blank conditions to use boolean values configuration option.
142
+
143
+ == 1.5.2 released 2008-10-21
144
+
145
+ * Added ilike conditions
146
+ * Removed configatron dependency (was accidentally added)
147
+
148
+ == 1.5.1 released 2008-10-20
149
+
150
+ * Fixed bug with per_page_links
151
+ * Added "ordering_by" class for per_page_links
152
+
153
+ == 1.5.0 released 2008-10-20
154
+
155
+ * Merge array of conditions with or or and where it makes sense. Begins with condition does not make sense to join with and, so instead join with or.
156
+ * Cleaned up html validity so it will pass W3C validators
157
+ * Restructure configuration to be more organized and allow for more configuration options.
158
+ * Added more configuration options, especially for class names.
159
+
160
+ == 1.4.1 released 2008-10-15
161
+
162
+ * Extracted english text into configuration to support alternate languages. I18n support.
163
+ * Improved method detection in add_joins to use respond_to? with the private method option set to true
164
+
165
+ == 1.4.0 released 2008-10-15
166
+
167
+ * Fixed bug when duping or cloning to copy over instance vars instead of method values
168
+ * Fixed bug with older versions of ActiveRecord and creating an alias method chain on find in AssociationCollection
169
+ * Added modifiers: upper, lower, trim, ltrim, rtrim
170
+ * Added not_nil and not_blank conditions
171
+ * Modified conditions so that table name is a variable, this allows table name to be switched on the fly.
172
+ * Modified auto joins to return symbols instead of SQL. This allows AR to properly merge the joins and ultimately result in proper SQL when performing multiple complex scopes.
173
+ * Fixed conflicts between includes and joins, duplicate joins are not added. Includes are prioritized. This also makes ActiveRecord much more flexible. Now you can cherry pick included associations. And not have to worry about conflicting with joins.
174
+ * Modified ActiveRecord's default behavior to use inner joins with the :joins option. Inner joins impose limitations. When providing this as a "convenience", inner joins do not make sense. The don't allow OR conditions to be across associations, nor does it properly order when an ordering by an association attribute when that association is optional.
175
+
176
+ == 1.3.5 released 2008-10-08
177
+
178
+ * Since joins are now strings, AR doesn't know how to merge them properly. So searchlogic now checks which joins to skip over to avoid conflicts.
179
+
180
+ == 1.3.4 released 2008-10-07
181
+
182
+ * Fixed method names creation for conditions with modifiers
183
+ * Create local column conditions, incase there are method conflicts. Such as "id".
184
+ * All joins are switched to left outer joins. When search with *any* conditions, inner joins return incorrect results. Also when ordering by an association that allows for blanks, inner joins exclude the records with blank values. Only option is left outer joins.
185
+ * If joins is manually set with a string, treat it as SQL, just like ActiveRecord
186
+
187
+ == 1.3.3 released 2008-10-03
188
+
189
+ * Fixed modifiers being double escaped on equals condition
190
+ * Fixed bug when merging conditions with array substitutions
191
+ * Updated blank condition to check if the values is false or not
192
+ * Fixed type for the year modifier in the mysql adapter
193
+
194
+ == 1.3.2 released 2008-10-03
195
+
196
+ * Fixed condition to type cast all values in an array
197
+ * Fixed bug to properly set the type for the type cast column
198
+ * Fixed bug to allow conditions on local columns
199
+ * Apparently ActiveRecord 2.2 doesnt remove duplicates on all joins, need to enforce this for ALL versions of ActiveRecord
200
+ * Fixed bug when passing a symbol as the SQL type for condition return types
201
+ * Moved duplicate removal to only apply when searching or calculating from a search object. Anything done in a normal ActiveRecord method should remain untouched and let ActiveRecord handle it. Just like in ActiveRecord if you do a User.all search and pass some :joins, you might get duplicate records. Searchlogic should not change that behavior, unless called from a searchlogic object.
202
+ * Discard meaningless array values when setting a condition.
203
+ * Delete blank strings from mass assignments on conditions.
204
+
205
+ == 1.3.1 released 2008-10-02
206
+
207
+ * Fixed bug when requiring a connection adapter that is not present
208
+ * Fixes bug in "not" conditions
209
+
210
+ == 1.3.0 released 2008-10-02
211
+
212
+ * Added modifiers into the mix: hour_of_created_at_less_than = 10, etc.
213
+ * Changed how the Searchlogic::Conditions::Base class works. Instead of predefining all methods for all conditions upon instantiation, they are defined as needed via method_missing. Similar to
214
+ ActiveRecord's dynamic finders: User.find_by_name_and_email(name, email). Once the are defined they never hit method_missing again, acts like a cache.
215
+ * Altered how values are handled for each condition, meaningless values are ignored completely.
216
+ * Added in more "not" conditions: not_like, not_begin_with, not_have_keywords, etc
217
+
218
+ == 1.2.2 released 2008-09-29
219
+
220
+ * Fixed bug when reverse engineering order to order_by, assumed ASC and DESC would always be present when they are not.
221
+ * False is a meaningful value for some conditions, and false.blank? == true. So instead of using value.blank? to ignore conditions we use meaningless?(value), which returns false if it is false.
222
+ * Fixed aliases for lt, lte, gt, and gte.
223
+ * Fixed bug when writing conditions on associations via a hash with string keys
224
+ * Added Config.remove_duplicates to turn off the "automatic" removing of duplicates if desired.
225
+ * Updated searchlogic_state helper to insert the entire state all at once.
226
+ * Added CSS class "ordering" to order_by_link if the search is being ordered by that.
227
+
228
+ == 1.2.1 released 2008-09-25
229
+
230
+ * Fixed problem when determining if an order_by_link is currently being ordered. Just "stringified" both comparable values.
231
+ * Removed default order_by and order_as. They will ONLY have values if you specify how to order, otherwise they are nil.
232
+ * Removed order_as requirement. order_as is optional.
233
+ * Added in deep_merge methods for hash, copied over from ActiveSupport 2.1
234
+ * Improved order by auto joins to be based off of what order_by returns instead of setting it when setting order_by.
235
+ * Added priority_order_by. Useful if you want to order featured products first and then order as usual. See documentation in Searchlogic::Search::Ordering for more info.
236
+ * Added in base64 support for order_by and priority_order_by so that it's value is safe in the URL
237
+ * Added priority_order_by_link
238
+
239
+ == 1.2.0 released 2008-09-24
240
+
241
+ * Added searchlogic_params and searchlogic_url helper to use outside of the control type helpers.
242
+ * Added dup and clone methods that work properly for searchlogic objects
243
+ * Fixed bug to remove nil scope values, HABTM likes to add :limit => nil
244
+ * Removed unnecessary build_search methods for associations
245
+ * Removed support for searching with conditions only. This just made things much more complicated when you can accomplish the same thing by starting a new search and only setting conditions.
246
+ * Fixed bug when searching with *any* conditions to use left outer joins instead of inner joins.
247
+
248
+ == 1.1.3 released 2008-09-23
249
+
250
+ * Setting a condition to nil removes it if the condition is set to ignore blanks
251
+ * 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".
252
+ * Fixed bug with inspect
253
+ * Other small performance enhancements with memoized attributes
254
+
255
+ == 1.1.2 released 2008-09-22
256
+
257
+ * Fixed bug with select control types not using :search_obj to determine its select values.
258
+ * Added is_nil and is_blank condition types.
259
+ * "memoized" various attributes for performance enhancements
260
+ * Removed the :order option from calculation options when :order is useless and just slows down query.
261
+ * Switched from using :include to :joins, big performance increase
262
+
263
+ == 1.1.1 released 2008-09-19
264
+
265
+ * Fixed typo in "next page" button.
266
+ * Updated valid options for searching and performing calculations, fixed some bugs when searching and performing calculations with advanced options.
267
+ * Fixed bug in ordering where table name was assumed by the hash. Now assumed by the reflection.
268
+ * Added default for per_page, so pagination comes implemented by default
269
+ * On mass assignments blank strings for *any* conditions are ignored. Sometimes blank strings are meaningful for "equals" and "does not equal", those only takes effect if you explicitly call these conditions: search.conditions.name = "". User.new_search(:conditions => {:name => ""}) will be ignored. Also, Searchlogic should never change how ActiveRecord behaves by default. So User.all(:conditions => {:name => ""}) will NOT be ignored.
270
+
271
+ == 1.1.0 released 2008-09-18
272
+
273
+ * Added the options :inner_spread and :outer_spread to the page_links helper. Also added various config options for setting page_links defaults.
274
+ * Updated calculation methods to ignore :limit and :offset. AR returns 0 or nil on calculations that provide an offset.
275
+ * Added support to allow for "any" of the conditions, instead of all of them. Joins conditions with "or" instead of "and". See Searchlogic::Conditions::Base or the readme
276
+
277
+ == 1.0.4 released 2008-09-18
278
+
279
+ * Fixed bugs when performing calculations and searches on has_many through relationships.
280
+ * Instead of merging the find_options myself, I delegated that to AR's with_scope function, which already does this. Much more solid, less intrusive.
281
+
282
+ == 1.0.3 released 2008-09-18
283
+
284
+ * Updated inspect to show the current options for your search. Plays nicer in the console.
285
+ * Made sure protection state is persistent among relationship conditions.
286
+ * Fixed bug with backwards compatibility of rails. concat requires a proc in older version.
287
+ * Defaulted remote control types to use GET requests instead of POST.
288
+ * Completely reengineered integration with ActiveRecord. Searchlogic is properly using scopes letting you do use serachgasm where scope are implemented. @current_users.orders.new_search, etc. If your search is scoped and you want a search object, that search object will represent a new search in the context of those scopes, meaning the scopes get merged into Searchlogic as options.
289
+ * Dropped support for Searchlogic functionality when defining relationships: has_many :order, :conditions => {:total_gt => 100}, will not work anymore. It's a chicken and the egg thing. Searchlogic needs AR constants, some models get loaded before others, therefore the Order model may not have been loaded yet, causing an unknown constant error.
290
+ * Clean up redundant code and moved it into the Searchlogic::Shared namespace.
291
+
292
+ == 1.0.2 released 2008-09-12
293
+
294
+ * Moved cached searchers out of the global namespace and into the Searchlogic::Cache namespce.
295
+ * Various changes to improve performance through profiling / benchmarking. http://pastie.org/271936
296
+ * Config.per_page works with new_search & new_search! only. Where as before it was only working if the search was protected.
297
+
298
+ == 1.0.1 released 2008-09-11
299
+
300
+ * Cached "searchers" so when a new search object is instantiated it doesn't go through all of the meta programming and method creation. Helps a lot with performance. You will see the speed benefits after the first instantiation.
301
+ * Added in new options for page_links.
302
+ * Fixed minor bugs when doing page_links.
303
+ * Updated documentation to be more detailed and inclusive.
304
+
305
+ == 1.0.0 released 2008-09-08
306
+
307
+ * Major changes in the helpers, they were completely re-engineered. Hence the new version. I established a pattern between all helpers giving you complete flexibility as to how they are used. All helpers are called differently now (see documentation).
308
+
309
+ == 0.9.10 released 2008-09-08
310
+
311
+ * Fixed bug with setting the per_page configuration to only take effect on protected searches, thus staying out of the way of normal searching.
312
+ * Hardened more tests
313
+
314
+ == 0.9.9 released 2008-09-07
315
+
316
+ * Fixed setting per_page to nil, false, or ''. This is done to "show all" results.
317
+
318
+ == 0.9.8 released 2008-09-06
319
+
320
+ * Fixed order_by helper bug when guessing the text with arrays. Should use the first value instead of last.
321
+ * Added in per_page config option.
322
+
323
+ == 0.9.7 released 2008-09-06
324
+
325
+ * Complete class restructure. Moved the 3 main components into their own base level class: Search, Conditions, Condition
326
+ * Split logic and functionality into their own modules, implemented via alias_chain_method
327
+ * Added in helpers for using in a rails app
328
+ * Added link to documentation and live example in README
329
+ * Various small bug fixes
330
+ * Hardened tests
331
+
332
+ == 0.9.6 released 2008-09-04
333
+
334
+ * Fixed bug when instantiating with nil options
335
+
336
+ == 0.9.5 released 2008-09-03
337
+
338
+ * Enhanced searching with conditions only, added in search methods and calculations
339
+ * Updated README to include examples
340
+
341
+ == 0.9.4 released 2008-09-03
342
+
343
+ * Cleaned up search methods
344
+ * Removed reset! method for both searching and searching by conditions
345
+
346
+ == 0.9.3 released 2008-09-02
347
+
348
+ * Changed structure of conditions to have their own class
349
+ * Added API for adding your own conditions.
350
+
351
+ == 0.9.2 released 2008-09-02
352
+
353
+ * Enhanced protection from SQL injections (made more efficient)
354
+
355
+ == 0.9.1 released 2008-09-02
356
+
357
+ * Added aliases for datetime, date, time, and timestamp attrs. You could call created_at_after, now you can also call created_after. Just removed the "at" requirement.
358
+
359
+ == 0.9.0 released 2008-09-01
360
+
361
+ * First release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ben Johnson of Binary Logic
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,226 @@
1
+ = Searchlogic
2
+
3
+ <b>Searchlogic has been <em>completely</em> rewritten for v2. It is much simpler and has taken an entirely new approach. To give you an idea, v1 had ~2300 lines of code, v2 has ~420 lines of code.</b>
4
+
5
+ Searchlogic provides common named scopes and object based searching for ActiveRecord.
6
+
7
+ == Helpful links
8
+
9
+ * <b>Documentation:</b> http://rdoc.info/projects/binarylogic/searchlogic
10
+ * <b>Repository:</b> http://github.com/binarylogic/searchlogic/tree/master
11
+ * <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/16601-searchlogic
12
+ * <b>Google group:</b> http://groups.google.com/group/searchlogic
13
+
14
+ <b>Before contacting me directly, please read:</b>
15
+
16
+ If you find a bug or a problem please post it on lighthouse. If you need help with something, please use google groups. I check both regularly and get emails when anything happens, so that is the best place to get help. This also benefits other people in the future with the same questions / problems. Thank you.
17
+
18
+ == Install & use
19
+
20
+ Install the gem from rubyforge:
21
+
22
+ sudo gem install searchlogic
23
+
24
+ Or from github:
25
+
26
+ sudo gem install binarylogic-searchlogic
27
+
28
+ Now just include it in your project and you are ready to go.
29
+
30
+ You can also install this as a plugin:
31
+
32
+ script/plugin install git://github.com/binarylogic/searchlogic.git
33
+
34
+ See below for usage examples.
35
+
36
+ == Search using conditions on columns
37
+
38
+ Instead of explaining what Searchlogic can do, let me show you. Let's start at the top:
39
+
40
+ # We have the following model
41
+ User(id: integer, created_at: datetime, username: string, age: integer)
42
+
43
+ # Searchlogic gives you a bunch of named scopes for free:
44
+ User.username_equals("bjohnson")
45
+ User.username_does_not_equal("bjohnson")
46
+ User.username_begins_with("bjohnson")
47
+ User.username_like("bjohnson")
48
+ User.username_ends_with("bjohnson")
49
+ User.age_greater_than(20)
50
+ User.age_greater_than_or_equal_to(20)
51
+ User.age_less_than(20)
52
+ User.age_less_than_or_equal_to(20)
53
+ User.username_null
54
+ User.username_blank
55
+
56
+ # You can also order by columns
57
+ User.ascend_by_username
58
+ User.descend_by_username
59
+ User.order("ascend_by_username")
60
+
61
+ Any named scope Searchlogic creates is dynamic and created via method_missing. Meaning it will only create what you need. Also, keep in mind, these are just named scopes, you can chain them, call methods off of them, etc:
62
+
63
+ scope = User.username_like("bjohnson").age_greater_than(20).ascend_by_username
64
+ scope.all
65
+ scope.first
66
+ scope.count
67
+ # etc...
68
+
69
+ That's all pretty standard, but here's where Searchlogic starts to get interesting...
70
+
71
+ == Search using conditions on associated columns
72
+
73
+ You also get named scopes for any of your associations:
74
+
75
+ # We have the following relationships
76
+ User.has_many :orders
77
+ Order.has_many :line_items
78
+ LineItem
79
+
80
+ # Set conditions on association columns
81
+ User.orders_total_greater_than(20)
82
+ User.orders_line_items_price_greater_than(20)
83
+
84
+ # Order by association columns
85
+ User.ascend_by_order_total
86
+ User.descend_by_orders_line_items_price
87
+
88
+ Again these are just named scopes. You can chain them together, call methods off of them, etc. What's great about these named scopes is that they do NOT use the :include option, making them <em>much</em> faster. Instead they create a INNER JOIN and pass it to the :joins option, which is great for performance. To prove my point here is a quick benchmark from an application I am working on:
89
+
90
+ Benchmark.bm do |x|
91
+ x.report { 10.times { Event.tickets_id_gt(10).all(:include => :tickets) } }
92
+ x.report { 10.times { Event.tickets_id_gt(10).all } }
93
+ end
94
+ user system total real
95
+ 10.120000 0.170000 10.290000 ( 12.625521)
96
+ 2.630000 0.050000 2.680000 ( 3.313754)
97
+
98
+ If you want to use the :include option, just specify it:
99
+
100
+ User.orders_line_items_price_greater_than(20).all(:include => {:orders => :line_items})
101
+
102
+ Obviously, only do this if you want to actually use the included objects.
103
+
104
+ == Make searching and ordering data in your application trivial
105
+
106
+ The above is great, but what about tying all of this in with a search form in your application? What would be really nice is if we could use an object that represented a single search. Like this...
107
+
108
+ search = User.search(:username_like => "bjohnson", :age_less_than => 20)
109
+ search.all
110
+
111
+ The above is equivalent to:
112
+
113
+ User.username_like("bjohnson").age_less_than(20).all
114
+
115
+ You can set, read, and chain conditions off of your search too:
116
+
117
+ search.username_like => "bjohnson"
118
+ search.age_gt = 2 => 2
119
+ search.id_gt(10).email_begins_with("bjohnson") => <#Searchlogic::Search...>
120
+ search.all => An array of users
121
+ search.count => integer
122
+ # .. etc
123
+
124
+ So let's start with the controller...
125
+
126
+ === Your controller
127
+
128
+ The search class just chains named scopes together for you. What's so great about that? It keeps your controllers extremely simple:
129
+
130
+ class UsersController < ApplicationController
131
+ def index
132
+ @search = User.search(params[:search])
133
+ @users = @search.all
134
+ end
135
+ end
136
+
137
+ It doesn't get any simpler than that.
138
+
139
+ === Your form
140
+
141
+ Adding a search condition is as simple as adding a condition to your form. Remember all of those named scopes above? Just create fields with the same names:
142
+
143
+ - form_for @search do |f|
144
+ = f.text_field :username_like
145
+ = f.select :age_greater_than, (0..100)
146
+ = f.text_field :orders_total_greater_than
147
+ = f.submit
148
+
149
+ When a Searchlogic::Search object is passed to form_for it will add a hidden field for the "order" condition, to preserve the order of the data.
150
+
151
+ === Additional helpers
152
+
153
+ There really isn't a big need for helpers in searchlogic, other than helping you order data. If you want to order your search with a link, just specify the name of the column. Ex:
154
+
155
+ = order @search, :by => :age
156
+ = order @search, :by => :created_at, :as => "Created date"
157
+
158
+ The first one will create a link that alternates between calling "ascend_by_age" and "descend_by_age". If you wanted to order your data by more than just a column, create your own named scopes: "ascend_by_*" and "descend_by_*". The "order" helper is a very straight forward helper, checkout the docs for some of the options.
159
+
160
+ <b>This helper is just a convenience method. It's extremely simple and there is nothing wrong with creating your own. If it doesn't do what you want, copy the code, modify it, and create your own. You could even fork the project, modify it there, and use your own gem.</b>
161
+
162
+ == Use your existing named scopes
163
+
164
+ This is one of the big differences between Searchlogic v1 and v2. What about your existing named scopes? Let's say you have this:
165
+
166
+ User.named_scope :four_year_olds, :conditions => {:age => 4}
167
+
168
+ Again, these are all just named scopes, use it in the same way:
169
+
170
+ User.search(:four_year_olds => true, :username_like => "bjohnson")
171
+
172
+ Notice we pass true as the value. If a named scope does not accept any parameters (arity == 0) you can simply pass it true or false. If you pass false, the named scope will be ignored. If your named scope accepts a parameter, the value will be passed right to the named scope regardless of the value.
173
+
174
+ Now just throw it in your form:
175
+
176
+ - form_for @search do |f|
177
+ = f.text_field :username_like
178
+ = f.check_box :four_year_olds
179
+ = f.submit
180
+
181
+ What's great about this is that you can do just about anything you want. If Searchlogic doesn't provide a named scope for that crazy edge case that you need, just create your own named scope. The sky is the limit.
182
+
183
+ == Use any or all
184
+
185
+ Every condition you've seen in this readme also has 2 related conditions that you can use. Example:
186
+
187
+ User.username_like_any("bjohnson", "thunt") # will return any users that have either of the strings in their username
188
+ User.username_like_all("bjohnson", "thunt") # will return any users that have all of the strings in their username
189
+ User.username_like_any(["bjohnson", "thunt"]) # also accepts an array
190
+
191
+ This is great for checkbox filters, etc. Where you can pass an array right from your form to this condition.
192
+
193
+ == Pagination (leverage will_paginate)
194
+
195
+ Instead of recreating the wheel with pagination, Searchlogic works great with will_paginate. All that Searchlogic is doing is creating named scopes, and will_paginate works great with named scopes:
196
+
197
+ User.username_like("bjohnson").age_less_than(20).paginate(:page => params[:page])
198
+ User.search(:username_like => "bjohnson", :age_less_than => 20).paginate(:page => params[:page])
199
+
200
+ If you don't like will_paginate, use another solution, or roll your own. Pagination really has nothing to do with searching, and the main goal for Searchlogic v2 was to keep it lean and simple. No reason to recreate the wheel and bloat the library.
201
+
202
+ == Conflicts with other gems
203
+
204
+ You will notice searchlogic wants to create a method called "search". So do other libraries like thinking sphinx, etc. So searchlogic has a no conflict resolution. If the "search" method is already taken the method will be called "searchlogic" instead. So instead of
205
+
206
+ User.search
207
+
208
+ You would do:
209
+
210
+ User.searchlogic
211
+
212
+ == Under the hood
213
+
214
+ Before I use a library in my application I like to glance at the source and try to at least understand the basics of how it works. If you are like me, a nice little explanation from the author is always helpful:
215
+
216
+ Searchlogic utilizes method_missing to create all of these named scopes. When it hits method_missing it creates a named scope to ensure it will never hit method missing for that named scope again. Sort of a caching mechanism. It works in the same fashion as ActiveRecord's "find_by_*" methods. This way only the named scopes you need are created and nothing more.
217
+
218
+ That's about it, the named scope options are pretty bare bones and created just like you would manually.
219
+
220
+ == Credit
221
+
222
+ Thanks a lot to {Tyler Hunt}[http://github.com/tylerhunt] for helping plan, design, and start the project. He was a big help.
223
+
224
+ == Copyright
225
+
226
+ Copyright (c) 2009 {Ben Johnson of Binary Logic}[http://www.binarylogic.com], released under the MIT license