searchlogic 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +228 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +123 -0
- data/README.rdoc +383 -0
- data/Rakefile +15 -0
- data/TODO.rdoc +6 -0
- data/examples/README.rdoc +4 -0
- data/init.rb +1 -0
- data/lib/searchlogic.rb +89 -0
- data/lib/searchlogic/active_record/associations.rb +52 -0
- data/lib/searchlogic/active_record/base.rb +218 -0
- data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +172 -0
- data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +168 -0
- data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +75 -0
- data/lib/searchlogic/condition/base.rb +159 -0
- data/lib/searchlogic/condition/begins_with.rb +17 -0
- data/lib/searchlogic/condition/blank.rb +21 -0
- data/lib/searchlogic/condition/child_of.rb +11 -0
- data/lib/searchlogic/condition/descendant_of.rb +24 -0
- data/lib/searchlogic/condition/ends_with.rb +17 -0
- data/lib/searchlogic/condition/equals.rb +27 -0
- data/lib/searchlogic/condition/greater_than.rb +15 -0
- data/lib/searchlogic/condition/greater_than_or_equal_to.rb +15 -0
- data/lib/searchlogic/condition/inclusive_descendant_of.rb +11 -0
- data/lib/searchlogic/condition/keywords.rb +47 -0
- data/lib/searchlogic/condition/less_than.rb +15 -0
- data/lib/searchlogic/condition/less_than_or_equal_to.rb +15 -0
- data/lib/searchlogic/condition/like.rb +15 -0
- data/lib/searchlogic/condition/nil.rb +21 -0
- data/lib/searchlogic/condition/not_begin_with.rb +20 -0
- data/lib/searchlogic/condition/not_blank.rb +19 -0
- data/lib/searchlogic/condition/not_end_with.rb +20 -0
- data/lib/searchlogic/condition/not_equal.rb +26 -0
- data/lib/searchlogic/condition/not_have_keywords.rb +20 -0
- data/lib/searchlogic/condition/not_like.rb +20 -0
- data/lib/searchlogic/condition/not_nil.rb +19 -0
- data/lib/searchlogic/condition/sibling_of.rb +14 -0
- data/lib/searchlogic/condition/tree.rb +17 -0
- data/lib/searchlogic/conditions/base.rb +484 -0
- data/lib/searchlogic/conditions/protection.rb +36 -0
- data/lib/searchlogic/config.rb +31 -0
- data/lib/searchlogic/config/helpers.rb +289 -0
- data/lib/searchlogic/config/search.rb +53 -0
- data/lib/searchlogic/core_ext/hash.rb +75 -0
- data/lib/searchlogic/helpers/control_types/link.rb +310 -0
- data/lib/searchlogic/helpers/control_types/links.rb +241 -0
- data/lib/searchlogic/helpers/control_types/remote_link.rb +87 -0
- data/lib/searchlogic/helpers/control_types/remote_links.rb +72 -0
- data/lib/searchlogic/helpers/control_types/remote_select.rb +36 -0
- data/lib/searchlogic/helpers/control_types/select.rb +82 -0
- data/lib/searchlogic/helpers/form.rb +208 -0
- data/lib/searchlogic/helpers/utilities.rb +197 -0
- data/lib/searchlogic/modifiers/absolute.rb +15 -0
- data/lib/searchlogic/modifiers/acos.rb +11 -0
- data/lib/searchlogic/modifiers/asin.rb +11 -0
- data/lib/searchlogic/modifiers/atan.rb +11 -0
- data/lib/searchlogic/modifiers/base.rb +27 -0
- data/lib/searchlogic/modifiers/ceil.rb +15 -0
- data/lib/searchlogic/modifiers/char_length.rb +15 -0
- data/lib/searchlogic/modifiers/cos.rb +15 -0
- data/lib/searchlogic/modifiers/cot.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_month.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_week.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_year.rb +15 -0
- data/lib/searchlogic/modifiers/degrees.rb +11 -0
- data/lib/searchlogic/modifiers/exp.rb +15 -0
- data/lib/searchlogic/modifiers/floor.rb +15 -0
- data/lib/searchlogic/modifiers/hex.rb +11 -0
- data/lib/searchlogic/modifiers/hour.rb +11 -0
- data/lib/searchlogic/modifiers/log.rb +15 -0
- data/lib/searchlogic/modifiers/log10.rb +11 -0
- data/lib/searchlogic/modifiers/log2.rb +11 -0
- data/lib/searchlogic/modifiers/lower.rb +15 -0
- data/lib/searchlogic/modifiers/ltrim.rb +15 -0
- data/lib/searchlogic/modifiers/md5.rb +11 -0
- data/lib/searchlogic/modifiers/microseconds.rb +11 -0
- data/lib/searchlogic/modifiers/milliseconds.rb +11 -0
- data/lib/searchlogic/modifiers/minute.rb +15 -0
- data/lib/searchlogic/modifiers/month.rb +15 -0
- data/lib/searchlogic/modifiers/octal.rb +15 -0
- data/lib/searchlogic/modifiers/radians.rb +11 -0
- data/lib/searchlogic/modifiers/round.rb +11 -0
- data/lib/searchlogic/modifiers/rtrim.rb +15 -0
- data/lib/searchlogic/modifiers/second.rb +15 -0
- data/lib/searchlogic/modifiers/sign.rb +11 -0
- data/lib/searchlogic/modifiers/sin.rb +11 -0
- data/lib/searchlogic/modifiers/square_root.rb +15 -0
- data/lib/searchlogic/modifiers/tan.rb +15 -0
- data/lib/searchlogic/modifiers/trim.rb +15 -0
- data/lib/searchlogic/modifiers/upper.rb +15 -0
- data/lib/searchlogic/modifiers/week.rb +11 -0
- data/lib/searchlogic/modifiers/year.rb +11 -0
- data/lib/searchlogic/search/base.rb +148 -0
- data/lib/searchlogic/search/conditions.rb +53 -0
- data/lib/searchlogic/search/ordering.rb +244 -0
- data/lib/searchlogic/search/pagination.rb +121 -0
- data/lib/searchlogic/search/protection.rb +89 -0
- data/lib/searchlogic/search/searching.rb +31 -0
- data/lib/searchlogic/shared/utilities.rb +50 -0
- data/lib/searchlogic/shared/virtual_classes.rb +39 -0
- data/lib/searchlogic/version.rb +79 -0
- data/searchlogic.gemspec +39 -0
- data/test/fixtures/accounts.yml +15 -0
- data/test/fixtures/cats.yml +3 -0
- data/test/fixtures/dogs.yml +3 -0
- data/test/fixtures/orders.yml +14 -0
- data/test/fixtures/user_groups.yml +13 -0
- data/test/fixtures/users.yml +36 -0
- data/test/test_active_record_associations.rb +81 -0
- data/test/test_active_record_base.rb +93 -0
- data/test/test_condition_base.rb +52 -0
- data/test/test_condition_types.rb +143 -0
- data/test/test_conditions_base.rb +242 -0
- data/test/test_conditions_protection.rb +16 -0
- data/test/test_config.rb +23 -0
- data/test/test_helper.rb +134 -0
- data/test/test_search_base.rb +227 -0
- data/test/test_search_conditions.rb +19 -0
- data/test/test_search_ordering.rb +165 -0
- data/test/test_search_pagination.rb +72 -0
- data/test/test_search_protection.rb +24 -0
- data/test_libs/acts_as_tree.rb +98 -0
- data/test_libs/ordered_hash.rb +9 -0
- data/test_libs/rexml_fix.rb +14 -0
- metadata +317 -0
@@ -0,0 +1,197 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Helpers #:nodoc:
|
3
|
+
module Utilities
|
4
|
+
# Builds a hash of params for creating a url and preserves any existing params. You can pass this into url_for and build your url. Although most rails helpers accept a hash.
|
5
|
+
#
|
6
|
+
# Let's take the page_link helper. Here is the code behind that helper:
|
7
|
+
#
|
8
|
+
# link_to("Page 2", searchlogic_params(:search_params => {:page => 2}))
|
9
|
+
#
|
10
|
+
# That's pretty much it. So if you wanted to roll your own link to execute a search, go for it. It's pretty simple. Pass conditions instead of the page, set how the search will be ordered, etc.
|
11
|
+
#
|
12
|
+
# <b>Be careful</b> when taking this approach though. Searchlogic helps you out when you use form_for. For example, when you use the per_page_select helper, it adds in a hidden form field with the value of the page. So when
|
13
|
+
# your search form is submitted it searches the document for that element, finds the current value, which is the current per_page value, and includes that in the search. So when a user searches the per_page
|
14
|
+
# value stays consistent. If you use the searchlogic_params you are on your own. I am always curious how people are using searchlogic. So if you are building your own helpers contact me and maybe I can help you
|
15
|
+
# and add in a helper for you, making it an *official* feature.
|
16
|
+
#
|
17
|
+
# === Options
|
18
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base leve in params such as params[:page], params[:per_page], etc, then set this to nil.
|
19
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
20
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
21
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
22
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
23
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
24
|
+
def searchlogic_params(options = {})
|
25
|
+
add_searchlogic_defaults!(options)
|
26
|
+
options[:search_params] ||= {}
|
27
|
+
options[:literal_search_params] ||= {}
|
28
|
+
|
29
|
+
options[:params] ||= {}
|
30
|
+
params_copy = params.deep_dup.with_indifferent_access
|
31
|
+
search_params = options[:params_scope].blank? ? params_copy : params_copy.delete(options[:params_scope])
|
32
|
+
search_params ||= {}
|
33
|
+
search_params = search_params.with_indifferent_access
|
34
|
+
search_params.delete(:commit)
|
35
|
+
search_params.delete(:page)
|
36
|
+
search_params.deep_delete_duplicate_keys(options[:literal_search_params])
|
37
|
+
search_params.deep_delete(options[:exclude_search_params])
|
38
|
+
|
39
|
+
if options[:search_params]
|
40
|
+
|
41
|
+
#raise params_copy.inspect if options[:search_params][:order_by] == :id
|
42
|
+
search_params.deep_merge!(options[:search_params])
|
43
|
+
|
44
|
+
if options[:search_params][:order_by] && !options[:search_params][:order_as]
|
45
|
+
search_params[:order_as] = (searchlogic_ordering_by?(options[:search_params][:order_by], options) && options[:search_obj].asc?) ? "DESC" : "ASC"
|
46
|
+
end
|
47
|
+
|
48
|
+
[:order_by, :priority_order_by].each { |base64_field| search_params[base64_field] = searchlogic_base64_value(search_params[base64_field]) if search_params.has_key?(base64_field) }
|
49
|
+
end
|
50
|
+
|
51
|
+
new_params = params_copy
|
52
|
+
new_params.deep_merge!(options[:params])
|
53
|
+
new_params.deep_delete(options[:exclude_params])
|
54
|
+
|
55
|
+
if options[:params_scope].blank? || search_params.blank?
|
56
|
+
new_params
|
57
|
+
else
|
58
|
+
new_params.merge(options[:params_scope] => search_params)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Similar to searchlogic_hash, but instead returns a string url. The reason this exists is to assist in creating urls in javascript. It's the muscle behind all of the select helpers that searchlogic provides.
|
63
|
+
# Take the instance where you want to do:
|
64
|
+
#
|
65
|
+
# :onchange => "window.location = '#{url_for(searchlogic_params)}&my_param=' + this.value;"
|
66
|
+
#
|
67
|
+
# Well the above obviously won't work. Do you need to apped the url with a ? or a &? What about that tricky :params_scope? That's where this is handy, beacuse it does all of the params string building for you. Check it out:
|
68
|
+
#
|
69
|
+
# :onchange => "window.location = '" + searchlogic_url(:literal_search_params => {:per_page => "' + escape(this.value) + '"}) + "';"
|
70
|
+
#
|
71
|
+
# or what about something a little more tricky?
|
72
|
+
#
|
73
|
+
# :onchange => "window.location = '" + searchlogic_url(:literal_search_params => {:conditions => {:name_contains => "' + escape(this.value) + '"}}) + "';"
|
74
|
+
#
|
75
|
+
# I have personally used this for an event calendar. Above the calendar there was a drop down for each month. Here is the code:
|
76
|
+
#
|
77
|
+
# :onchange => "window.location = '" + searchlogic_url(:literal_search_params => {:conditions => {:occurs_at_after => "' + escape(this.value) + '"}}) + "';"
|
78
|
+
#
|
79
|
+
# Now when the user changes the month in the drop down it just runs a new search that sets my conditions to occurs_at_after = selected month. Then in my controller I set occurs_at_before = occurs_at_after.at_end_of_month.
|
80
|
+
#
|
81
|
+
# === Options
|
82
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base leve in params such as params[:page], params[:per_page], etc, then set this to nil.
|
83
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
84
|
+
# * <tt>:params</tt> -- default: nil, Additional params to add to the url, must be a hash
|
85
|
+
# * <tt>:exclude_params</tt> -- default: nil, params you want to exclude. This is nifty because it does a "deep delete". So you can pass {:param1 => {:param2 => :param3}} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.
|
86
|
+
# * <tt>:search_params</tt> -- default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.
|
87
|
+
# * <tt>:literal_search_params</tt> -- default: nil, Additional search params to add to the url, but are not escaped. So you can add javascript into the URL: :literal_search_params => {:per_page => "' + escape(this.value) + '"}
|
88
|
+
# * <tt>:exclude_search_params</tt> -- default: nil, Same as :exclude_params but for the :search_params.
|
89
|
+
def searchlogic_url(options = {})
|
90
|
+
search_params = searchlogic_params(options)
|
91
|
+
url = url_for(search_params)
|
92
|
+
literal_param_strings = literal_param_strings(options[:literal_search_params], options[:params_scope].blank? ? "" : "#{options[:params_scope]}")
|
93
|
+
url += (url.last == "?" ? "" : (url.include?("?") ? "&" : "?")) + literal_param_strings.join("&")
|
94
|
+
end
|
95
|
+
|
96
|
+
# When you set up a search form using form_for for remote_form_for searchlogic adds in some *magic* for you.
|
97
|
+
#
|
98
|
+
# Take the instance where a user orders the data by something other than the default, and then does a search. The user would expect the search to remember what the user selected to order the data by, right?
|
99
|
+
# What searchlogic does is add in some hidden fields, somewhere in the page, the represent the searchlogic "state". These are automatically added for you when you use the searchlogic helpers.
|
100
|
+
# Such as: page_links, page_link, order_by_link, per_page_select, etc. So if you are using those you do not need to worry about this helper.
|
101
|
+
#
|
102
|
+
# If for some reason you do not use any of these you need to put the searchlogic state on your page somewhere. Somewhere where the state will *always* be up-to-date, which would be most likely be in the
|
103
|
+
# partial that renders your search results (assuming you are using AJAX). Otherwise when the user starts a new search, the state will be reset. Meaning the order_by, per_page, etc will all be reset.
|
104
|
+
#
|
105
|
+
# === Options
|
106
|
+
# * <tt>:params_scope</tt> -- default: :search, this is the scope in which your search params will be preserved (params[:search]). If you don't want a scope and want your options to be at base leve in params such as params[:page], params[:per_page], etc, then set this to nil.
|
107
|
+
# * <tt>:search_obj</tt> -- default: @#{params_scope}, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.
|
108
|
+
def searchlogic_state(options = {})
|
109
|
+
return "" if @added_searchlogic_state
|
110
|
+
add_searchlogic_defaults!(options)
|
111
|
+
html = ""
|
112
|
+
(Search::Base::SPECIAL_FIND_OPTIONS - [:page, :priority_order]).each do |option|
|
113
|
+
value = options[:search_obj].send(option)
|
114
|
+
html += hidden_field(options[:params_scope], option, :value => (option == :order_by ? searchlogic_base64_value(value) : value))
|
115
|
+
end
|
116
|
+
@added_searchlogic_state = true
|
117
|
+
html
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
# Adds default options for all helper methods.
|
122
|
+
def add_searchlogic_defaults!(options)
|
123
|
+
options[:params_scope] = :search unless options.has_key?(:params_scope)
|
124
|
+
options[:search_obj] ||= instance_variable_get("@#{options[:params_scope]}")
|
125
|
+
raise(ArgumentError, "@search object could not be inferred, please specify: :search_obj => @search or :params_scope => :search_obj_name") unless options[:search_obj].is_a?(Searchlogic::Search::Base)
|
126
|
+
options
|
127
|
+
end
|
128
|
+
|
129
|
+
# Adds default options for all control type helper methods.
|
130
|
+
def add_searchlogic_control_defaults!(options)
|
131
|
+
add_searchlogic_defaults!(options)
|
132
|
+
options[:html] ||= {}
|
133
|
+
options
|
134
|
+
end
|
135
|
+
|
136
|
+
def searchlogic_add_class!(html_options, new_class)
|
137
|
+
new_class = new_class.to_s
|
138
|
+
html_options[:class] ||= ""
|
139
|
+
classes = html_options[:class].split(" ")
|
140
|
+
classes << new_class unless classes.include?(new_class)
|
141
|
+
html_options[:class] = classes.join(" ")
|
142
|
+
end
|
143
|
+
|
144
|
+
def searchlogic_base64_value(order_by)
|
145
|
+
case order_by
|
146
|
+
when String, Symbol
|
147
|
+
order_by
|
148
|
+
when Array, Hash
|
149
|
+
[Marshal.dump(order_by)].pack("m")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def searchlogic_ordering_by?(order_by, options)
|
154
|
+
stringified_search_order_by = deep_stringify(options[:search_obj].order_by)
|
155
|
+
stringified_order_by = deep_stringify(order_by)
|
156
|
+
(options[:search_obj].order_by.blank? && options[:search_obj].klass.primary_key == stringified_order_by) || stringified_search_order_by == stringified_order_by
|
157
|
+
end
|
158
|
+
|
159
|
+
def literal_param_strings(literal_params, prefix)
|
160
|
+
param_strings = []
|
161
|
+
|
162
|
+
literal_params.each do |k, v|
|
163
|
+
param_string = prefix.blank? ? k.to_s : "#{prefix}[#{k}]"
|
164
|
+
case v
|
165
|
+
when Hash
|
166
|
+
literal_param_strings(v, param_string).each do |literal_param_string|
|
167
|
+
param_strings << literal_param_string
|
168
|
+
end
|
169
|
+
else
|
170
|
+
param_strings << (CGI.escape(param_string) + "=#{v}")
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
param_strings
|
175
|
+
end
|
176
|
+
|
177
|
+
def deep_stringify(obj)
|
178
|
+
case obj
|
179
|
+
when String
|
180
|
+
obj
|
181
|
+
when Symbol
|
182
|
+
obj.to_s
|
183
|
+
when Array
|
184
|
+
obj.collect { |item| deep_stringify(item) }
|
185
|
+
when Hash
|
186
|
+
new_obj = {}
|
187
|
+
obj.each { |key, value| new_obj[key.to_s] = deep_stringify(value) }
|
188
|
+
new_obj
|
189
|
+
else
|
190
|
+
obj
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
ActionController::Base.helper(Searchlogic::Helpers::Utilities) if defined?(ActionController)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module Modifiers
|
3
|
+
class Base
|
4
|
+
class << self
|
5
|
+
# A convenience method for the name of this modifier
|
6
|
+
def modifier_name
|
7
|
+
name.split("::").last.underscore
|
8
|
+
end
|
9
|
+
|
10
|
+
# The various names for the modifier. The first in the array is the "main" name, the rest are just aliases to the "main" name
|
11
|
+
def modifier_names
|
12
|
+
[modifier_name]
|
13
|
+
end
|
14
|
+
|
15
|
+
# The method in the connection adapter that creates the SQL for the modifier
|
16
|
+
def adapter_method_name
|
17
|
+
"#{modifier_name}_sql"
|
18
|
+
end
|
19
|
+
|
20
|
+
# The type of value returned from the SQL. A class the extends this MUST define this method.
|
21
|
+
def return_type
|
22
|
+
raise "You did not specify a return type for the #{modifier_name} modifier. Please specify if it is an :integer, :string, etc."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|