joost-searchlogic 2.1.5.2 → 2.1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +20 -0
- data/lib/searchlogic/rails_helpers.rb +25 -18
- data/lib/searchlogic.rb +3 -2
- data/searchlogic.gemspec +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
This is a clone of the original SearchLogic! Only used to apply some fast patches.
|
2
2
|
You should probably use the newer original source.
|
3
3
|
|
4
|
+
Changes made:
|
5
|
+
* Fixed bug #92 (see lighthouse page).
|
6
|
+
* Changed order helper.
|
7
|
+
* Changed loading of helpers.
|
8
|
+
* Added some documentation.
|
9
|
+
|
4
10
|
= Searchlogic
|
5
11
|
|
6
12
|
<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>
|
@@ -174,6 +180,20 @@ Again, these are all just named scopes, use it in the same way:
|
|
174
180
|
|
175
181
|
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.
|
176
182
|
|
183
|
+
This way you can create more complex searches like:
|
184
|
+
|
185
|
+
User.named_scope :search_who, lambda { |*args|
|
186
|
+
who = "%#{(args.first || '').strip.gsub('%', '')}%"
|
187
|
+
{
|
188
|
+
:conditions => ["users.email LIKE ? OR user_profiles.firstname LIKE ?", who, who],
|
189
|
+
:include => :user_profile
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
Use it, again, very simple:
|
194
|
+
|
195
|
+
User.search(:who_search => 'john')
|
196
|
+
|
177
197
|
Now just throw it in your form:
|
178
198
|
|
179
199
|
- form_for @search do |f|
|
@@ -1,5 +1,6 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module ActionView
|
2
|
+
class Base
|
3
|
+
|
3
4
|
# Creates a link that alternates between acending and descending. It basically
|
4
5
|
# alternates between calling 2 named scopes: "ascend_by_*" and "descend_by_*"
|
5
6
|
#
|
@@ -10,6 +11,7 @@ module Searchlogic
|
|
10
11
|
#
|
11
12
|
# order @search, :by => :username
|
12
13
|
# order @search, :by => :created_at, :as => "Created"
|
14
|
+
# order(@search, {:by => :last_login_at, :as => 'Login time', :arrow => true}, {:class => 'order'})
|
13
15
|
#
|
14
16
|
# This helper accepts the following options:
|
15
17
|
#
|
@@ -18,31 +20,37 @@ module Searchlogic
|
|
18
20
|
# * <tt>:ascend_scope</tt> - what scope to call for ascending the data, defaults to "ascend_by_:by"
|
19
21
|
# * <tt>:descend_scope</tt> - what scope to call for descending the data, defaults to "descend_by_:by"
|
20
22
|
# * <tt>:params_scope</tt> - the name of the params key to scope the order condition by, defaults to :search
|
23
|
+
# * <tt>:default_scope</tt> - either :asc or :desc, defaults to ascend_scope
|
24
|
+
# * <tt>:arrow</tt> - set to true if you want an ascii arrow showing the ordering
|
25
|
+
# * <tt>:css_classes</tt> - Array of what css classes you want to add, defaults to [:asc, :desc]. For old behaviour use [:ascending, :descending] here
|
21
26
|
def order(search, options = {}, html_options = {})
|
22
|
-
options[:params_scope]
|
23
|
-
options[:as]
|
24
|
-
options[:ascend_scope]
|
27
|
+
options[:params_scope] ||= :search
|
28
|
+
options[:as] ||= options[:by].to_s.humanize
|
29
|
+
options[:ascend_scope] ||= "ascend_by_#{options[:by]}"
|
25
30
|
options[:descend_scope] ||= "descend_by_#{options[:by]}"
|
26
|
-
|
27
|
-
new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
|
31
|
+
options[:css_classes] ||= [:asc, :desc]
|
28
32
|
selected = [options[:ascend_scope], options[:descend_scope]].include?(search.order.to_s)
|
29
33
|
if selected
|
34
|
+
ascending = search.order.to_s == options[:ascend_scope]
|
35
|
+
new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
|
30
36
|
css_classes = html_options[:class] ? html_options[:class].split(" ") : []
|
31
37
|
if ascending
|
32
|
-
options[:as] = "▲ #{options[:as]}"
|
33
|
-
css_classes <<
|
38
|
+
options[:as] = "▲ #{options[:as]}" if options[:arrow]
|
39
|
+
css_classes << options[:css_classes].first
|
34
40
|
else
|
35
|
-
options[:as] = "▼ #{options[:as]}"
|
36
|
-
css_classes <<
|
41
|
+
options[:as] = "▼ #{options[:as]}" if options[:arrow]
|
42
|
+
css_classes << options[:css_classes].last
|
37
43
|
end
|
38
44
|
html_options[:class] = css_classes.join(" ")
|
45
|
+
else
|
46
|
+
new_scope = (options[:default_scope] == :desc) ? options[:descend_scope] : options[:ascend_scope]
|
39
47
|
end
|
40
48
|
link_to options[:as], url_for(options[:params_scope] => search.conditions.merge( { :order => new_scope } ) ), html_options
|
41
49
|
end
|
42
50
|
|
43
51
|
# Automatically makes the form method :get if a Searchlogic::Search and sets
|
44
52
|
# the params scope to :search
|
45
|
-
def
|
53
|
+
def form_for_with_searchlogic(*args, &block)
|
46
54
|
if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
|
47
55
|
options = args.extract_options!
|
48
56
|
options[:html] ||= {}
|
@@ -51,19 +59,18 @@ module Searchlogic
|
|
51
59
|
args.unshift(:search) if args.first == search_obj
|
52
60
|
args << options
|
53
61
|
end
|
54
|
-
|
62
|
+
form_for_without_searchlogic(*args, &block)
|
55
63
|
end
|
56
|
-
|
64
|
+
|
57
65
|
# Automatically adds an "order" hidden field in your form to preserve how the data
|
58
66
|
# is being ordered.
|
59
|
-
def
|
67
|
+
def fields_for_with_searchlogic(*args, &block)
|
60
68
|
if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
|
61
69
|
args.unshift(:search) if args.first == search_obj
|
62
70
|
concat(content_tag("div", hidden_field_tag("#{args.first}[order]", search_obj.order)) + "\n")
|
63
|
-
super
|
64
|
-
else
|
65
|
-
super
|
66
71
|
end
|
72
|
+
fields_for_without_searchlogic(*args, &block)
|
67
73
|
end
|
74
|
+
|
68
75
|
end
|
69
76
|
end
|
data/lib/searchlogic.rb
CHANGED
@@ -25,7 +25,8 @@ if !ActiveRecord::Base.respond_to?(:search)
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
if defined?(
|
28
|
+
if defined?(ActionView)
|
29
29
|
require "searchlogic/rails_helpers"
|
30
|
-
|
30
|
+
ActionView::Base.alias_method_chain :form_for, :searchlogic
|
31
|
+
ActionView::Base.alias_method_chain :fields_for, :searchlogic
|
31
32
|
end
|
data/searchlogic.gemspec
CHANGED