searchlogic 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/CHANGELOG.rdoc +228 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Manifest +123 -0
  4. data/README.rdoc +383 -0
  5. data/Rakefile +15 -0
  6. data/TODO.rdoc +6 -0
  7. data/examples/README.rdoc +4 -0
  8. data/init.rb +1 -0
  9. data/lib/searchlogic.rb +89 -0
  10. data/lib/searchlogic/active_record/associations.rb +52 -0
  11. data/lib/searchlogic/active_record/base.rb +218 -0
  12. data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +172 -0
  13. data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +168 -0
  14. data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +75 -0
  15. data/lib/searchlogic/condition/base.rb +159 -0
  16. data/lib/searchlogic/condition/begins_with.rb +17 -0
  17. data/lib/searchlogic/condition/blank.rb +21 -0
  18. data/lib/searchlogic/condition/child_of.rb +11 -0
  19. data/lib/searchlogic/condition/descendant_of.rb +24 -0
  20. data/lib/searchlogic/condition/ends_with.rb +17 -0
  21. data/lib/searchlogic/condition/equals.rb +27 -0
  22. data/lib/searchlogic/condition/greater_than.rb +15 -0
  23. data/lib/searchlogic/condition/greater_than_or_equal_to.rb +15 -0
  24. data/lib/searchlogic/condition/inclusive_descendant_of.rb +11 -0
  25. data/lib/searchlogic/condition/keywords.rb +47 -0
  26. data/lib/searchlogic/condition/less_than.rb +15 -0
  27. data/lib/searchlogic/condition/less_than_or_equal_to.rb +15 -0
  28. data/lib/searchlogic/condition/like.rb +15 -0
  29. data/lib/searchlogic/condition/nil.rb +21 -0
  30. data/lib/searchlogic/condition/not_begin_with.rb +20 -0
  31. data/lib/searchlogic/condition/not_blank.rb +19 -0
  32. data/lib/searchlogic/condition/not_end_with.rb +20 -0
  33. data/lib/searchlogic/condition/not_equal.rb +26 -0
  34. data/lib/searchlogic/condition/not_have_keywords.rb +20 -0
  35. data/lib/searchlogic/condition/not_like.rb +20 -0
  36. data/lib/searchlogic/condition/not_nil.rb +19 -0
  37. data/lib/searchlogic/condition/sibling_of.rb +14 -0
  38. data/lib/searchlogic/condition/tree.rb +17 -0
  39. data/lib/searchlogic/conditions/base.rb +484 -0
  40. data/lib/searchlogic/conditions/protection.rb +36 -0
  41. data/lib/searchlogic/config.rb +31 -0
  42. data/lib/searchlogic/config/helpers.rb +289 -0
  43. data/lib/searchlogic/config/search.rb +53 -0
  44. data/lib/searchlogic/core_ext/hash.rb +75 -0
  45. data/lib/searchlogic/helpers/control_types/link.rb +310 -0
  46. data/lib/searchlogic/helpers/control_types/links.rb +241 -0
  47. data/lib/searchlogic/helpers/control_types/remote_link.rb +87 -0
  48. data/lib/searchlogic/helpers/control_types/remote_links.rb +72 -0
  49. data/lib/searchlogic/helpers/control_types/remote_select.rb +36 -0
  50. data/lib/searchlogic/helpers/control_types/select.rb +82 -0
  51. data/lib/searchlogic/helpers/form.rb +208 -0
  52. data/lib/searchlogic/helpers/utilities.rb +197 -0
  53. data/lib/searchlogic/modifiers/absolute.rb +15 -0
  54. data/lib/searchlogic/modifiers/acos.rb +11 -0
  55. data/lib/searchlogic/modifiers/asin.rb +11 -0
  56. data/lib/searchlogic/modifiers/atan.rb +11 -0
  57. data/lib/searchlogic/modifiers/base.rb +27 -0
  58. data/lib/searchlogic/modifiers/ceil.rb +15 -0
  59. data/lib/searchlogic/modifiers/char_length.rb +15 -0
  60. data/lib/searchlogic/modifiers/cos.rb +15 -0
  61. data/lib/searchlogic/modifiers/cot.rb +15 -0
  62. data/lib/searchlogic/modifiers/day_of_month.rb +15 -0
  63. data/lib/searchlogic/modifiers/day_of_week.rb +15 -0
  64. data/lib/searchlogic/modifiers/day_of_year.rb +15 -0
  65. data/lib/searchlogic/modifiers/degrees.rb +11 -0
  66. data/lib/searchlogic/modifiers/exp.rb +15 -0
  67. data/lib/searchlogic/modifiers/floor.rb +15 -0
  68. data/lib/searchlogic/modifiers/hex.rb +11 -0
  69. data/lib/searchlogic/modifiers/hour.rb +11 -0
  70. data/lib/searchlogic/modifiers/log.rb +15 -0
  71. data/lib/searchlogic/modifiers/log10.rb +11 -0
  72. data/lib/searchlogic/modifiers/log2.rb +11 -0
  73. data/lib/searchlogic/modifiers/lower.rb +15 -0
  74. data/lib/searchlogic/modifiers/ltrim.rb +15 -0
  75. data/lib/searchlogic/modifiers/md5.rb +11 -0
  76. data/lib/searchlogic/modifiers/microseconds.rb +11 -0
  77. data/lib/searchlogic/modifiers/milliseconds.rb +11 -0
  78. data/lib/searchlogic/modifiers/minute.rb +15 -0
  79. data/lib/searchlogic/modifiers/month.rb +15 -0
  80. data/lib/searchlogic/modifiers/octal.rb +15 -0
  81. data/lib/searchlogic/modifiers/radians.rb +11 -0
  82. data/lib/searchlogic/modifiers/round.rb +11 -0
  83. data/lib/searchlogic/modifiers/rtrim.rb +15 -0
  84. data/lib/searchlogic/modifiers/second.rb +15 -0
  85. data/lib/searchlogic/modifiers/sign.rb +11 -0
  86. data/lib/searchlogic/modifiers/sin.rb +11 -0
  87. data/lib/searchlogic/modifiers/square_root.rb +15 -0
  88. data/lib/searchlogic/modifiers/tan.rb +15 -0
  89. data/lib/searchlogic/modifiers/trim.rb +15 -0
  90. data/lib/searchlogic/modifiers/upper.rb +15 -0
  91. data/lib/searchlogic/modifiers/week.rb +11 -0
  92. data/lib/searchlogic/modifiers/year.rb +11 -0
  93. data/lib/searchlogic/search/base.rb +148 -0
  94. data/lib/searchlogic/search/conditions.rb +53 -0
  95. data/lib/searchlogic/search/ordering.rb +244 -0
  96. data/lib/searchlogic/search/pagination.rb +121 -0
  97. data/lib/searchlogic/search/protection.rb +89 -0
  98. data/lib/searchlogic/search/searching.rb +31 -0
  99. data/lib/searchlogic/shared/utilities.rb +50 -0
  100. data/lib/searchlogic/shared/virtual_classes.rb +39 -0
  101. data/lib/searchlogic/version.rb +79 -0
  102. data/searchlogic.gemspec +39 -0
  103. data/test/fixtures/accounts.yml +15 -0
  104. data/test/fixtures/cats.yml +3 -0
  105. data/test/fixtures/dogs.yml +3 -0
  106. data/test/fixtures/orders.yml +14 -0
  107. data/test/fixtures/user_groups.yml +13 -0
  108. data/test/fixtures/users.yml +36 -0
  109. data/test/test_active_record_associations.rb +81 -0
  110. data/test/test_active_record_base.rb +93 -0
  111. data/test/test_condition_base.rb +52 -0
  112. data/test/test_condition_types.rb +143 -0
  113. data/test/test_conditions_base.rb +242 -0
  114. data/test/test_conditions_protection.rb +16 -0
  115. data/test/test_config.rb +23 -0
  116. data/test/test_helper.rb +134 -0
  117. data/test/test_search_base.rb +227 -0
  118. data/test/test_search_conditions.rb +19 -0
  119. data/test/test_search_ordering.rb +165 -0
  120. data/test/test_search_pagination.rb +72 -0
  121. data/test/test_search_protection.rb +24 -0
  122. data/test_libs/acts_as_tree.rb +98 -0
  123. data/test_libs/ordered_hash.rb +9 -0
  124. data/test_libs/rexml_fix.rb +14 -0
  125. metadata +317 -0
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,228 @@
1
+ == 1.5.3 released 2008-10-30
2
+
3
+ * Removed ilike conditions and let the like condition determine if it should use like or ilike depending on the connection adapter.
4
+ * Fixed bug where the keywords condition was removing foreign characters.
5
+ * Added configuration option for specifying your javascript library, to cut down on the javascript Searchlogic adds into the form helpers
6
+ * Fixed bug with "not" conditions to use sanitize method, like it should, instead of to_conditions.
7
+ * Only pass :distinct option for the count calculation
8
+ * Fixed not_nil and not_blank conditions to use boolean values configuration option.
9
+
10
+ == 1.5.2 released 2008-10-21
11
+
12
+ * Added ilike conditions
13
+ * Removed configatron dependency (was accidentally added)
14
+
15
+ == 1.5.1 released 2008-10-20
16
+
17
+ * Fixed bug with per_page_links
18
+ * Added "ordering_by" class for per_page_links
19
+
20
+ == 1.5.0 released 2008-10-20
21
+
22
+ * 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.
23
+ * Cleaned up html validity so it will pass W3C validators
24
+ * Restructure configuration to be more organized and allow for more configuration options.
25
+ * Added more configuration options, especially for class names.
26
+
27
+ == 1.4.1 released 2008-10-15
28
+
29
+ * Extracted english text into configuration to support alternate languages. I18n support.
30
+ * Improved method detection in add_joins to use respond_to? with the private method option set to true
31
+
32
+ == 1.4.0 released 2008-10-15
33
+
34
+ * Fixed bug when duping or cloning to copy over instance vars instead of method values
35
+ * Fixed bug with older versions of ActiveRecord and creating an alias method chain on find in AssociationCollection
36
+ * Added modifiers: upper, lower, trim, ltrim, rtrim
37
+ * Added not_nil and not_blank conditions
38
+ * Modified conditions so that table name is a variable, this allows table name to be switched on the fly.
39
+ * 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.
40
+ * 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.
41
+ * 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.
42
+
43
+ == 1.3.5 released 2008-10-08
44
+
45
+ * 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.
46
+
47
+ == 1.3.4 released 2008-10-07
48
+
49
+ * Fixed method names creation for conditions with modifiers
50
+ * Create local column conditions, incase there are method conflicts. Such as "id".
51
+ * 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.
52
+ * If joins is manually set with a string, treat it as SQL, just like ActiveRecord
53
+
54
+ == 1.3.3 released 2008-10-03
55
+
56
+ * Fixed modifiers being double escaped on equals condition
57
+ * Fixed bug when merging conditions with array substitutions
58
+ * Updated blank condition to check if the values is false or not
59
+ * Fixed type for the year modifier in the mysql adapter
60
+
61
+ == 1.3.2 released 2008-10-03
62
+
63
+ * Fixed condition to type cast all values in an array
64
+ * Fixed bug to properly set the type for the type cast column
65
+ * Fixed bug to allow conditions on local columns
66
+ * Apparently ActiveRecord 2.2 doesnt remove duplicates on all joins, need to enforce this for ALL versions of ActiveRecord
67
+ * Fixed bug when passing a symbol as the SQL type for condition return types
68
+ * 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.
69
+ * Discard meaningless array values when setting a condition.
70
+ * Delete blank strings from mass assignments on conditions.
71
+
72
+ == 1.3.1 released 2008-10-02
73
+
74
+ * Fixed bug when requiring a connection adapter that is not present
75
+ * Fixes bug in "not" conditions
76
+
77
+ == 1.3.0 released 2008-10-02
78
+
79
+ * Added modifiers into the mix: hour_of_created_at_less_than = 10, etc.
80
+ * 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
81
+ 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.
82
+ * Altered how values are handled for each condition, meaningless values are ignored completely.
83
+ * Added in more "not" conditions: not_like, not_begin_with, not_have_keywords, etc
84
+
85
+ == 1.2.2 released 2008-09-29
86
+
87
+ * Fixed bug when reverse engineering order to order_by, assumed ASC and DESC would always be present when they are not.
88
+ * 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.
89
+ * Fixed aliases for lt, lte, gt, and gte.
90
+ * Fixed bug when writing conditions on associations via a hash with string keys
91
+ * Added Config.remove_duplicates to turn off the "automatic" removing of duplicates if desired.
92
+ * Updated searchlogic_state helper to insert the entire state all at once.
93
+ * Added CSS class "ordering" to order_by_link if the search is being ordered by that.
94
+
95
+ == 1.2.1 released 2008-09-25
96
+
97
+ * Fixed problem when determining if an order_by_link is currently being ordered. Just "stringified" both comparable values.
98
+ * Removed default order_by and order_as. They will ONLY have values if you specify how to order, otherwise they are nil.
99
+ * Removed order_as requirement. order_as is optional.
100
+ * Added in deep_merge methods for hash, copied over from ActiveSupport 2.1
101
+ * Improved order by auto joins to be based off of what order_by returns instead of setting it when setting order_by.
102
+ * 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.
103
+ * Added in base64 support for order_by and priority_order_by so that it's value is safe in the URL
104
+ * Added priority_order_by_link
105
+
106
+ == 1.2.0 released 2008-09-24
107
+
108
+ * Added searchlogic_params and searchlogic_url helper to use outside of the control type helpers.
109
+ * Added dup and clone methods that work properly for searchlogic objects
110
+ * Fixed bug to remove nil scope values, HABTM likes to add :limit => nil
111
+ * Removed unnecessary build_search methods for associations
112
+ * 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.
113
+ * Fixed bug when searching with *any* conditions to use left outer joins instead of inner joins.
114
+
115
+ == 1.1.3 released 2008-09-23
116
+
117
+ * Setting a condition to nil removes it if the condition is set to ignore blanks
118
+ * 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".
119
+ * Fixed bug with inspect
120
+ * Other small performance enhancements with memoized attributes
121
+
122
+ == 1.1.2 released 2008-09-22
123
+
124
+ * Fixed bug with select control types not using :search_obj to determine its select values.
125
+ * Added is_nil and is_blank condition types.
126
+ * "memoized" various attributes for performance enhancements
127
+ * Removed the :order option from calculation options when :order is useless and just slows down query.
128
+ * Switched from using :include to :joins, big performance increase
129
+
130
+ == 1.1.1 released 2008-09-19
131
+
132
+ * Fixed typo in "next page" button.
133
+ * Updated valid options for searching and performing calculations, fixed some bugs when searching and performing calculations with advanced options.
134
+ * Fixed bug in ordering where table name was assumed by the hash. Now assumed by the reflection.
135
+ * Added default for per_page, so pagination comes implemented by default
136
+ * 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.
137
+
138
+ == 1.1.0 released 2008-09-18
139
+
140
+ * Added the options :inner_spread and :outer_spread to the page_links helper. Also added various config options for setting page_links defaults.
141
+ * Updated calculation methods to ignore :limit and :offset. AR returns 0 or nil on calculations that provide an offset.
142
+ * 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
143
+
144
+ == 1.0.4 released 2008-09-18
145
+
146
+ * Fixed bugs when performing calculations and searches on has_many through relationships.
147
+ * 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.
148
+
149
+ == 1.0.3 released 2008-09-18
150
+
151
+ * Updated inspect to show the current options for your search. Plays nicer in the console.
152
+ * Made sure protection state is persistent among relationship conditions.
153
+ * Fixed bug with backwards compatibility of rails. concat requires a proc in older version.
154
+ * Defaulted remote control types to use GET requests instead of POST.
155
+ * 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.
156
+ * 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.
157
+ * Clean up redundant code and moved it into the Searchlogic::Shared namespace.
158
+
159
+ == 1.0.2 released 2008-09-12
160
+
161
+ * Moved cached searchers out of the global namespace and into the Searchlogic::Cache namespce.
162
+ * Various changes to improve performance through profiling / benchmarking. http://pastie.org/271936
163
+ * Config.per_page works with new_search & new_search! only. Where as before it was only working if the search was protected.
164
+
165
+ == 1.0.1 released 2008-09-11
166
+
167
+ * 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.
168
+ * Added in new options for page_links.
169
+ * Fixed minor bugs when doing page_links.
170
+ * Updated documentation to be more detailed and inclusive.
171
+
172
+ == 1.0.0 released 2008-09-08
173
+
174
+ * 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).
175
+
176
+ == 0.9.10 released 2008-09-08
177
+
178
+ * Fixed bug with setting the per_page configuration to only take effect on protected searches, thus staying out of the way of normal searching.
179
+ * Hardened more tests
180
+
181
+ == 0.9.9 released 2008-09-07
182
+
183
+ * Fixed setting per_page to nil, false, or ''. This is done to "show all" results.
184
+
185
+ == 0.9.8 released 2008-09-06
186
+
187
+ * Fixed order_by helper bug when guessing the text with arrays. Should use the first value instead of last.
188
+ * Added in per_page config option.
189
+
190
+ == 0.9.7 released 2008-09-06
191
+
192
+ * Complete class restructure. Moved the 3 main components into their own base level class: Search, Conditions, Condition
193
+ * Split logic and functionality into their own modules, implemented via alias_chain_method
194
+ * Added in helpers for using in a rails app
195
+ * Added link to documentation and live example in README
196
+ * Various small bug fixes
197
+ * Hardened tests
198
+
199
+ == 0.9.6 released 2008-09-04
200
+
201
+ * Fixed bug when instantiating with nil options
202
+
203
+ == 0.9.5 released 2008-09-03
204
+
205
+ * Enhanced searching with conditions only, added in search methods and calculations
206
+ * Updated README to include examples
207
+
208
+ == 0.9.4 released 2008-09-03
209
+
210
+ * Cleaned up search methods
211
+ * Removed reset! method for both searching and searching by conditions
212
+
213
+ == 0.9.3 released 2008-09-02
214
+
215
+ * Changed structure of conditions to have their own class
216
+ * Added API for adding your own conditions.
217
+
218
+ == 0.9.2 released 2008-09-02
219
+
220
+ * Enhanced protection from SQL injections (made more efficient)
221
+
222
+ == 0.9.1 released 2008-09-02
223
+
224
+ * 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.
225
+
226
+ == 0.9.0 released 2008-09-01
227
+
228
+ * First release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Ben Johnson of Binary Logic (binarylogic.com)
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/Manifest ADDED
@@ -0,0 +1,123 @@
1
+ CHANGELOG.rdoc
2
+ examples/README.rdoc
3
+ init.rb
4
+ lib/searchlogic/active_record/associations.rb
5
+ lib/searchlogic/active_record/base.rb
6
+ lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb
7
+ lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb
8
+ lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb
9
+ lib/searchlogic/condition/base.rb
10
+ lib/searchlogic/condition/begins_with.rb
11
+ lib/searchlogic/condition/blank.rb
12
+ lib/searchlogic/condition/child_of.rb
13
+ lib/searchlogic/condition/descendant_of.rb
14
+ lib/searchlogic/condition/ends_with.rb
15
+ lib/searchlogic/condition/equals.rb
16
+ lib/searchlogic/condition/greater_than.rb
17
+ lib/searchlogic/condition/greater_than_or_equal_to.rb
18
+ lib/searchlogic/condition/inclusive_descendant_of.rb
19
+ lib/searchlogic/condition/keywords.rb
20
+ lib/searchlogic/condition/less_than.rb
21
+ lib/searchlogic/condition/less_than_or_equal_to.rb
22
+ lib/searchlogic/condition/like.rb
23
+ lib/searchlogic/condition/nil.rb
24
+ lib/searchlogic/condition/not_begin_with.rb
25
+ lib/searchlogic/condition/not_blank.rb
26
+ lib/searchlogic/condition/not_end_with.rb
27
+ lib/searchlogic/condition/not_equal.rb
28
+ lib/searchlogic/condition/not_have_keywords.rb
29
+ lib/searchlogic/condition/not_like.rb
30
+ lib/searchlogic/condition/not_nil.rb
31
+ lib/searchlogic/condition/sibling_of.rb
32
+ lib/searchlogic/condition/tree.rb
33
+ lib/searchlogic/conditions/base.rb
34
+ lib/searchlogic/conditions/protection.rb
35
+ lib/searchlogic/config/helpers.rb
36
+ lib/searchlogic/config/search.rb
37
+ lib/searchlogic/config.rb
38
+ lib/searchlogic/core_ext/hash.rb
39
+ lib/searchlogic/helpers/control_types/link.rb
40
+ lib/searchlogic/helpers/control_types/links.rb
41
+ lib/searchlogic/helpers/control_types/remote_link.rb
42
+ lib/searchlogic/helpers/control_types/remote_links.rb
43
+ lib/searchlogic/helpers/control_types/remote_select.rb
44
+ lib/searchlogic/helpers/control_types/select.rb
45
+ lib/searchlogic/helpers/form.rb
46
+ lib/searchlogic/helpers/utilities.rb
47
+ lib/searchlogic/modifiers/absolute.rb
48
+ lib/searchlogic/modifiers/acos.rb
49
+ lib/searchlogic/modifiers/asin.rb
50
+ lib/searchlogic/modifiers/atan.rb
51
+ lib/searchlogic/modifiers/base.rb
52
+ lib/searchlogic/modifiers/ceil.rb
53
+ lib/searchlogic/modifiers/char_length.rb
54
+ lib/searchlogic/modifiers/cos.rb
55
+ lib/searchlogic/modifiers/cot.rb
56
+ lib/searchlogic/modifiers/day_of_month.rb
57
+ lib/searchlogic/modifiers/day_of_week.rb
58
+ lib/searchlogic/modifiers/day_of_year.rb
59
+ lib/searchlogic/modifiers/degrees.rb
60
+ lib/searchlogic/modifiers/exp.rb
61
+ lib/searchlogic/modifiers/floor.rb
62
+ lib/searchlogic/modifiers/hex.rb
63
+ lib/searchlogic/modifiers/hour.rb
64
+ lib/searchlogic/modifiers/log.rb
65
+ lib/searchlogic/modifiers/log10.rb
66
+ lib/searchlogic/modifiers/log2.rb
67
+ lib/searchlogic/modifiers/lower.rb
68
+ lib/searchlogic/modifiers/ltrim.rb
69
+ lib/searchlogic/modifiers/md5.rb
70
+ lib/searchlogic/modifiers/microseconds.rb
71
+ lib/searchlogic/modifiers/milliseconds.rb
72
+ lib/searchlogic/modifiers/minute.rb
73
+ lib/searchlogic/modifiers/month.rb
74
+ lib/searchlogic/modifiers/octal.rb
75
+ lib/searchlogic/modifiers/radians.rb
76
+ lib/searchlogic/modifiers/round.rb
77
+ lib/searchlogic/modifiers/rtrim.rb
78
+ lib/searchlogic/modifiers/second.rb
79
+ lib/searchlogic/modifiers/sign.rb
80
+ lib/searchlogic/modifiers/sin.rb
81
+ lib/searchlogic/modifiers/square_root.rb
82
+ lib/searchlogic/modifiers/tan.rb
83
+ lib/searchlogic/modifiers/trim.rb
84
+ lib/searchlogic/modifiers/upper.rb
85
+ lib/searchlogic/modifiers/week.rb
86
+ lib/searchlogic/modifiers/year.rb
87
+ lib/searchlogic/search/base.rb
88
+ lib/searchlogic/search/conditions.rb
89
+ lib/searchlogic/search/ordering.rb
90
+ lib/searchlogic/search/pagination.rb
91
+ lib/searchlogic/search/protection.rb
92
+ lib/searchlogic/search/searching.rb
93
+ lib/searchlogic/shared/utilities.rb
94
+ lib/searchlogic/shared/virtual_classes.rb
95
+ lib/searchlogic/version.rb
96
+ lib/searchlogic.rb
97
+ Manifest
98
+ MIT-LICENSE
99
+ Rakefile
100
+ README.rdoc
101
+ test/fixtures/accounts.yml
102
+ test/fixtures/cats.yml
103
+ test/fixtures/dogs.yml
104
+ test/fixtures/orders.yml
105
+ test/fixtures/user_groups.yml
106
+ test/fixtures/users.yml
107
+ test/test_active_record_associations.rb
108
+ test/test_active_record_base.rb
109
+ test/test_condition_base.rb
110
+ test/test_condition_types.rb
111
+ test/test_conditions_base.rb
112
+ test/test_conditions_protection.rb
113
+ test/test_config.rb
114
+ test/test_helper.rb
115
+ test/test_search_base.rb
116
+ test/test_search_conditions.rb
117
+ test/test_search_ordering.rb
118
+ test/test_search_pagination.rb
119
+ test/test_search_protection.rb
120
+ test_libs/acts_as_tree.rb
121
+ test_libs/ordered_hash.rb
122
+ test_libs/rexml_fix.rb
123
+ TODO.rdoc
data/README.rdoc ADDED
@@ -0,0 +1,383 @@
1
+ = Searchlogic
2
+
3
+ Searchlogic is orgasmic. Maybe not orgasmic, but you will get aroused. So go grab a towel and let's dive in.
4
+
5
+ <b>Searchlogic's inspiration comes right from ActiveRecord. ActiveRecord lets you create objects that represent a record in the database, so why can't you create objects that represent searching the database? Now you can! It's searching, ordering, and pagination all in one.</b>
6
+
7
+ == Helpful links
8
+
9
+ * <b>Documentation:</b> http://searchlogic.rubyforge.org
10
+ * <b>Easy pagination, ordering, and searching tutorial:</b> http://www.binarylogic.com/2008/9/7/tutorial-pagination-ordering-and-searching-with-searchlogic
11
+ * <b>Live example of the tutorial above (with source):</b> http://searchlogic_example.binarylogic.com
12
+ * <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/16601-searchlogic
13
+
14
+ == Install and use
15
+
16
+ sudo gem install searchlogic
17
+
18
+ For rails
19
+
20
+ $ cd vendor/plugins
21
+ $ sudo gem unpack searchlogic
22
+
23
+ Or as a plugin
24
+
25
+ script/plugin install git://github.com/binarylogic/searchlogic.git
26
+
27
+ Now try out some of the examples below:
28
+
29
+ <b>For all examples, let's assume the following relationships: User => Orders => Line Items</b>
30
+
31
+ == Simple Searching Example
32
+
33
+ User.all(
34
+ :conditions => {
35
+ :first_name_contains => "Ben", # first_name like '%Ben%'
36
+ :email_ends_with => "binarylogic.com", # email like '%binarylogic.com'
37
+ :created_after => Time.now, # created_at > Time.now
38
+ :hour_of_created_at => 5 # HOUR(created_at) > 5 (depends on DB type)
39
+ },
40
+ :per_page => 20, # limit 20
41
+ :page => 3, # offset 40, which starts us on page 3
42
+ :order_as => "ASC",
43
+ :order_by => {:user_group => :name} # order user_groups.name ASC
44
+ )
45
+
46
+ same as above, but object based
47
+
48
+ search = User.new_search
49
+ search.conditions.first_name_contains = "Ben"
50
+ search.conditions.email_ends_with = "binarylogic.com"
51
+ search.conditions.created_after = Time.now
52
+ search.conditiona.hour_of_created_at = 5
53
+ search.per_page = 20
54
+ search.page = 3
55
+ search.order_as = "ASC"
56
+ search.order_by = {:user_group => :name}
57
+ search.all
58
+
59
+ In both examples, instead of using the "all" method you could use any search method: first, find(:all), find(:first), count, sum, average, etc, just like ActiveRecord.
60
+
61
+ == The beauty of searchlogic, integration into rails
62
+
63
+ Using Searchlogic in rails is the best part, because rails has all kinds of nifty methods to make dealing with ActiveRecord objects quick and easy, especially with forms. So let's take advantage of them! That's the idea behind this plugin. Searchlogic is searching, ordering, and pagination all rolled into one simple plugin. Take all of that pagination and searching cruft out of your models and controllers, and let Searchlogic handle it. Check it out:
64
+
65
+ # app/controllers/users_controller.rb
66
+ def index
67
+ @search = User.new_search(params[:search])
68
+ @users, @users_count = @search.all, @search.count
69
+ end
70
+
71
+ Now your view:
72
+
73
+ # app/views/users/index.html.haml
74
+ - form_for @search do |f|
75
+ - f.fields_for @search.conditions do |users|
76
+ = users.text_field :first_name_contains
77
+ = users.calendar_date_select :created_after # nice rails plugin for replacing date_select
78
+ - users.fields_for users.object.orders do |orders|
79
+ = orders.select :total_gt, (1..100)
80
+ = f.submit "Search"
81
+
82
+ - if @users_count > 0
83
+ %table
84
+ %tr
85
+ %th= order_by_link :account => :name
86
+ %th= order_by_link :first_name
87
+ %th= order_by_link :last_name
88
+ %th= order_by_link :email
89
+ - @users.each do |user|
90
+ %tr
91
+ %td= user.account? ? user.account.name : "-"
92
+ %td= user.first_name
93
+ %td= user.last_name
94
+ %td= user.email
95
+
96
+ Per page:
97
+ = per_page_select
98
+ Page:
99
+ = page_select
100
+ - else
101
+ No users were found.
102
+
103
+ Things to note in this view:
104
+
105
+ 1. Passing a search object right into form\_for and fields\_for
106
+ 2. The built in conditions for each column and how you can traverse the relationships and set conditions on them
107
+ 3. The order_by_link helper
108
+ 4. The page_select and per_page_select helpers
109
+ 5. All of your search logic is in 1 spot: your view. Nice and DRY.
110
+
111
+ <b>See my tutorial on this example: http://www.binarylogic.com/2008/9/7/tutorial-pagination-ordering-and-searching-with-searchlogic</b>
112
+
113
+ == Exhaustive Example w/ Object Based Searching (great for form_for or fields_for)
114
+
115
+ # Start a new search
116
+ @search = User.new_search(
117
+ :conditions => {
118
+ :first_name_contains => "Ben",
119
+ :age_gt => 18,
120
+ :orders => {:total_lt => 100}
121
+ },
122
+ :per_page => 20,
123
+ :page => 2,
124
+ :order_by => {:orders => :total},
125
+ :order_as => "DESC"
126
+ )
127
+
128
+ # Set local conditions
129
+ @search.conditions.email_ends_with = "binarylogic.com"
130
+
131
+ # Set conditions on relationships
132
+ @search.conditions.oders.line_items.created_after = Time.now # can traverse through all relationships
133
+
134
+ # Set options
135
+ @search.per_page = 50 # overrides the 20 set above
136
+ @search.order_by = [:first_name, {:user_group => :name}] # order by first name and then by the user group's name it belongs to
137
+ @search.order_as = "ASC"
138
+
139
+ # Set ANY of the ActiveRecord options
140
+ @search.group = "last_name"
141
+ @search.readonly = true
142
+ # ... see ActiveRecord documentation
143
+
144
+ # Return results just like ActiveRecord
145
+ @search.all
146
+ @search.first
147
+
148
+ Take the @search object and pass it right into form\_for or fields\_for (see above).
149
+
150
+ == Calculations
151
+
152
+ Using the object from above:
153
+
154
+ @search.average('id')
155
+ @search.count # ignores limit and offset
156
+ @search.maximum('id')
157
+ @search.minimum('id')
158
+ @search.sum('id')
159
+ @search.calculate(:sum, 'id')
160
+ # ...any of the above calculations, see ActiveRecord documentation on calculations
161
+
162
+ Or do it from your model:
163
+
164
+ User.count(:conditions => {:first_name_contains => "Ben"})
165
+ User.sum('id', :conditions => {:first_name_contains => "Ben"})
166
+ # ... all other calcualtions, etc.
167
+
168
+ == Different ways to search, take your pick
169
+
170
+ Any of the options used in the above example can be used in these, but for the sake of brevity I am only using a few:
171
+
172
+ User.all(:conditions => {:age_gt => 18}, :per_page => 20)
173
+
174
+ User.first(:conditions => {:age_gt => 18}, :per_page => 20)
175
+
176
+ User.find(:all, :conditions => {:age_gt => 18}, :per_page => 20)
177
+
178
+ User.find(:first, :conditions => {:age_gt => 18}, :per_page => 20)
179
+
180
+ search = User.new_search(:conditions => {:age_gt => 18}) # build_search is an alias
181
+ search.conditions.first_name_contains = "Ben"
182
+ search.per_page = 20
183
+ search.all
184
+
185
+ == Match ANY or ALL of the conditions
186
+
187
+ As you saw above, the nice thing about Searchlogic is it's integration with forms. I designed the "any" option so that forms can set this as well, just like a condition.
188
+
189
+ search = User.new_search(:conditions => {:age_gt => 18})
190
+ search.conditions.first_name_contains = "Ben"
191
+ search.conditions.any = true # can set this to "true" or "1" or "yes"
192
+ search.all # will join all conditions with "or" instead of "and"
193
+ # ... all operations above are available
194
+
195
+ == Scoped searching
196
+
197
+ @current_user.orders.find(:all, :conditions => {:total_lte => 500})
198
+ @current_user.orders.count(:conditions => {:total_lte => 500})
199
+ @current_user.orders.sum('total', :conditions => {:total_lte => 500})
200
+
201
+ search = @current_user.orders.build_search(:conditions => {:total_lte => 500})
202
+
203
+ == Scope support
204
+
205
+ Not only can you use searchlogic when searching, but you can use it when using scopes.
206
+
207
+ class User < ActiveRecord::Base
208
+ named_scope :sexy, :conditions => {:first_name => "Ben", email_ends_with => "binarylogic.com"}, :per_page => 20
209
+ end
210
+
211
+ or
212
+
213
+ class User < ActiveRecord::Base
214
+ def self.find_sexy
215
+ with_scope(:find => {:conditions => {:first_name => "Ben", email_ends_with => "binarylogic.com"}, :per_page => 20}) do
216
+ all
217
+ end
218
+ end
219
+ end
220
+
221
+ == Always use protection...against SQL injections
222
+
223
+ If there is one thing we all know, it's to always use protection against SQL injections. That's why searchlogic protects you by default. The new\_search methods protect mass assignments by default (instantiation and search.options = {}). This means that various checks are done to ensure it is not possible to perform any type of SQL injection during mass assignments. But this also limits how you can search, meaning you can't write raw SQL. If you want to be daring and search without protection, all that you have to do is add ! to the end of the method: new\_search!.
224
+
225
+ === Protected from SQL injections
226
+
227
+ search = Account.new_search(params[:search])
228
+
229
+ === *NOT* protected from SQL injections
230
+
231
+ accounts = Account.find(params[:search])
232
+ accounts = Account.all(params[:search])
233
+ account = Account.first(params[:search])
234
+ search = Account.new_search!(params[:search])
235
+
236
+ I'm sure you already knew this, but it's tempting to do this when you can pass the params hash right into these methods.
237
+
238
+ Lesson learned: use new\_search when passing in params as *ANY* of the options.
239
+
240
+ == Available Conditions
241
+
242
+ The conditions are pretty self explanitory, but if you need more information checkout the docs or the source. The code is very simple and self explanatory.
243
+
244
+ === Column conditions
245
+
246
+ Each column can be used with any of the following conditions
247
+
248
+ Name Aliases Description
249
+ :begins_with :starts_with, :sw, :bw, :start col LIKE 'value%'
250
+ :ends_with :ew, :ends, :end col LIKE '%value'
251
+ :equals :is, "" Lets ActiveRecord handle this
252
+ :greater_than :gt, :after col > value
253
+ :greater_than_or_equal_to :at_least, :least, :gte col >= value
254
+ :ilike :icontains, :ihas PostgreSQL specific, makes LIKE case insensitive
255
+ :less_than :lt, :before col < value
256
+ :less_than_or_equal_to :at_most, :most, :lte col <= value
257
+ :keywords :kwords, :kw Splits into each word and omits meaningless words, a true keyword search
258
+ :like :contains, :has col LIKE '%value%'
259
+
260
+ :not_begin_with :not_bw, :not_sw, :not_start_with, :not_start, :beginning_is_not, :beginning_not
261
+ :not_end_with :not_ew, :not_end, :end_is_not, :end_not
262
+ :not_equal :does_not_equal, :is_not, :not
263
+ :not_have_keywords :not_have_keywords, :not_keywords, :not_have_kw, :not_kw, :not_have_kwwords, :not_kwwords
264
+ :not_ilike :not_icontain, :not_ihave
265
+ :not_like :not_contain, :not_have
266
+
267
+ === Class level conditions
268
+
269
+ Each model comes preloaded with class level conditions as well. The difference is that these are not applied to each column, but instead to the model as a whole. Example: search.conditions.child_of = 2
270
+
271
+ Name Description
272
+ :child_of Returns all children of value
273
+ :descendant_of Returns all descendants (children, grandchildren, grandgrandchildren, etc)
274
+ :inclusive_descendant_of Same as above but also includes the root
275
+ :sibling_of Returns all records that have the same parent
276
+
277
+ == Modifiers
278
+
279
+ === What are modifiers?
280
+
281
+ ActiveRecord does a great job when it comes to keeping your code database agnostic. But I feel like it neglected searching when it came to that goal. What if you want to find all records that were created after 7am? Depending on your database you would have to do something like the following:
282
+
283
+ MySQL: HOUR(created_at)
284
+ PostgreSQL: date_part('hour', created_at)
285
+ SQLite: strftime('%H', created_at)
286
+
287
+ All of a sudden your app is not database agnostic. Searchlogic to the rescue! Searchlogic creates what I like to call "modifiers" to handle this nonsense for you. A modifier modifies a column. For example, the hour modifier modifies a datetime column to return the hour.
288
+
289
+ The last thing to keep in mind is that <b>not all modifiers are available for every database</b>. MySQL and PostgreSQL support all of these, but SQLite does not. SQLite is nice, in the sense that its really is "lite". The only modifiers it supports are the datetime modifiers. If you want support for the other modifiers you have to write the SQLite function yourself and register the modifier in searchlogic.
290
+
291
+ Here are all of the available modifiers:
292
+
293
+ === Available modifiers
294
+
295
+ Name Aliases Description
296
+ :microsecond :microseconds, :microsecs, :microsec Extracts the microseconds
297
+ :millisecond :milliseconds, :millisecs, :millisec Extracts the milliseconds
298
+ :second :sec Extracts the seconds
299
+ :minute :min Extracts the minute
300
+ :hour Extracts the hour
301
+ :day_of_week :dow Extracts the day of week (1-7)
302
+ :day_of_month :dom Extracts the day of month (1-31)
303
+ :day_of_year :doy Extracts the day of year (1-366)
304
+ :week Extracts the week (1-53), 53rd week can be a "run-over" week to the next year
305
+ :month :mon Extracts the month (1-12)
306
+ :year Extracts the year
307
+
308
+ :md5 Converts to a MD5
309
+ :char_length :length The length of the string (integer)
310
+ :lower :downcase, :lcase Converts the string to all lower case characters
311
+ :ltrin :lstrip Strips off spaces from the beginning of the string
312
+ :trim :strip Strips off spaces from the beginning and end of the string
313
+ :rtrim :rstrip Strips off spaces from the end of the string
314
+ :upper :upcase, :ucase Converts the string to all upper case character
315
+
316
+ :absolute :abs The absolute value (-1 => 1)
317
+ :acos The arc cosine
318
+ :asin The arc sine
319
+ :atan The arc tangent
320
+ :ceil :round_up Rounds up to the nearest int
321
+ :cos :cosine The cosine
322
+ :cot :cotangent The cotangent
323
+ :degrees Converts radians to degrees
324
+ :exp :exponential Returns the value of e (the base of natural logarithms) raised to the power of X
325
+ :floor :round_down Rounds down to the nearest int
326
+ :hex Converts the number to a hex
327
+ :log :ln The natural logarithm
328
+ :log10 Returns the base-10 logarithm
329
+ :log2 Returns the base-2 logarithm
330
+ :octal :oct Return an octal representation of a decimal number
331
+ :radians Converts to radians
332
+ :round Rounds the number
333
+ :sign The sign of the number
334
+ :sin The sine of the number
335
+ :square_root :sqrt, :sq_rt The square root of the number
336
+ :tan :tangent The tangent of the number
337
+
338
+ === How to use modifiers
339
+
340
+ Here's what the above table means. Let's take the created_at column. The created_at column is a datetime column, laet's apply modifiers that make sense for a datetime column.
341
+
342
+ search.conditions.second_of_created_at = 2
343
+ search.conditions.sec_of_created_at_greater_than = 3
344
+ search.conditions.year_of_created_at_most = 5
345
+ search.conditions.year_of_created_at_lt = 2000
346
+ # ... any of the modifiers that apply to datetime columns
347
+
348
+ Here's the cool part. Chaining modifiers:
349
+
350
+ search.conditions.ceil_of_cos_of_sec_of_created_at_greater_than = 3
351
+
352
+ As long as the modifier chain makes sense the possibilities are endless.
353
+
354
+ === Modifiers are not indexed
355
+
356
+ Depending on your database you can create complex indexes. But chances are you probably didn't or don't plan to. So keep in mind that once you use a modifier it will not be using an index, meaning the query will be slower. One of the ways to get the best of both worlds is to cache virtual attributes in the database. Checkout my tutorial:
357
+
358
+ http://www.binarylogic.com/2008/10/5/tutorial-caching-virtual-attributes-in-the-database
359
+
360
+ == Roll your own conditions & modifiers
361
+
362
+ For more information on this please see Searchlogic::Conditions::Base
363
+
364
+ == Under the hood
365
+
366
+ I'm a big fan of understanding what I'm using, so here's a quick explanation: The design behind this plugin is pretty simple and I had 1 main rule when developing this:
367
+
368
+ ActiveRecord should never know about Searchlogic
369
+
370
+ What that rule means is that any options you pass when searching get "sanitized" down into options ActiveRecord can understand. Searchlogic serves as a transparent filter between you and ActiveRecord. It doesn't dig into the ActiveRecord internals, it only uses what is publicly available. It jumps in and helps out <em>only</em> when needed, otherwise it sits back and stays completely out of the way.
371
+
372
+ Lastly, Searchlogic is lazy. It only creates objects, methods, and classes when needed. Once it creates them it caches them. For example, all of the nifty conditions are created via meta programming. The first time you execute something like User.new_search all of that method creation gets cached into Searchlogic::Cache::UserSearch. The next time you execute User.new_search it will be over 50 times faster because it uses the cached class.
373
+
374
+ Between that and the extensive tests, this is a solid and fast plugin.
375
+
376
+ == Credits
377
+
378
+ Author: {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com]
379
+
380
+ Credit to {Zack Ham}[http://github.com/zackham] for helping with feature suggestions.
381
+
382
+
383
+ Copyright (c) 2008 {Ben Johnson}[http://github.com/binarylogic] of {Binary Logic}[http://www.binarylogic.com], released under the MIT license