kazjote-searchlogic 2.1.9.1 → 2.1.9.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module Searchlogic
|
|
5
5
|
def local_condition?(name) # :nodoc:
|
6
6
|
super || order_condition?(name)
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def primary_condition_name(name) # :nodoc
|
10
10
|
if result = super
|
11
11
|
result
|
@@ -15,18 +15,16 @@ module Searchlogic
|
|
15
15
|
nil
|
16
16
|
end
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def order_condition?(name) # :nodoc:
|
20
20
|
!order_condition_details(name).nil?
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
private
|
24
24
|
def method_missing(name, *args, &block)
|
25
|
-
if name == :
|
26
|
-
return @searchlogic_order_condition if args.empty?
|
25
|
+
if name == :order
|
27
26
|
named_scope name, lambda { |scope_name|
|
28
27
|
return {} if !order_condition?(scope_name)
|
29
|
-
@searchlogic_order_condition = scope_name
|
30
28
|
send(scope_name).proxy_options
|
31
29
|
}
|
32
30
|
send(name, *args)
|
@@ -37,7 +35,7 @@ module Searchlogic
|
|
37
35
|
super
|
38
36
|
end
|
39
37
|
end
|
40
|
-
|
38
|
+
|
41
39
|
def order_condition_details(name)
|
42
40
|
if name.to_s =~ /^(ascend|descend)_by_(#{column_names.join("|")})$/
|
43
41
|
{:order_as => $1, :column => $2}
|
@@ -45,12 +43,11 @@ module Searchlogic
|
|
45
43
|
{}
|
46
44
|
end
|
47
45
|
end
|
48
|
-
|
46
|
+
|
49
47
|
def create_order_conditions(column)
|
50
48
|
named_scope("ascend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} ASC"})
|
51
49
|
named_scope("descend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} DESC"})
|
52
50
|
end
|
53
51
|
end
|
54
52
|
end
|
55
|
-
end
|
56
|
-
|
53
|
+
end
|
@@ -23,9 +23,9 @@ module Searchlogic
|
|
23
23
|
options[:as] ||= options[:by].to_s.humanize
|
24
24
|
options[:ascend_scope] ||= "ascend_by_#{options[:by]}"
|
25
25
|
options[:descend_scope] ||= "descend_by_#{options[:by]}"
|
26
|
-
ascending = search.
|
26
|
+
ascending = search.order.to_s == options[:ascend_scope]
|
27
27
|
new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
|
28
|
-
selected = [options[:ascend_scope], options[:descend_scope]].include?(search.
|
28
|
+
selected = [options[:ascend_scope], options[:descend_scope]].include?(search.order.to_s)
|
29
29
|
if selected
|
30
30
|
css_classes = html_options[:class] ? html_options[:class].split(" ") : []
|
31
31
|
if ascending
|
@@ -38,7 +38,7 @@ module Searchlogic
|
|
38
38
|
html_options[:class] = css_classes.join(" ")
|
39
39
|
end
|
40
40
|
url_options = {
|
41
|
-
options[:params_scope] => search.conditions.merge( { :
|
41
|
+
options[:params_scope] => search.conditions.merge( { :order => new_scope } )
|
42
42
|
}.deep_merge(options[:params] || {})
|
43
43
|
link_to options[:as], url_for(url_options), html_options
|
44
44
|
end
|
@@ -62,7 +62,7 @@ module Searchlogic
|
|
62
62
|
def fields_for(*args, &block)
|
63
63
|
if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
|
64
64
|
args.unshift(:search) if args.first == search_obj
|
65
|
-
concat(content_tag("div", hidden_field_tag("#{args.first}[
|
65
|
+
concat(content_tag("div", hidden_field_tag("#{args.first}[order]", search_obj.order)) + "\n")
|
66
66
|
super
|
67
67
|
else
|
68
68
|
super
|
data/lib/searchlogic/search.rb
CHANGED
@@ -25,7 +25,7 @@ module Searchlogic
|
|
25
25
|
Search.new(self, scope(:find), conditions)
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
# Is an invalid condition is used this error will be raised. Ex:
|
30
30
|
#
|
31
31
|
# User.search(:unkown => true)
|
@@ -37,10 +37,10 @@ module Searchlogic
|
|
37
37
|
super(msg)
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
attr_accessor :klass, :current_scope, :conditions
|
42
42
|
undef :id if respond_to?(:id)
|
43
|
-
|
43
|
+
|
44
44
|
# Creates a new search object for the given class. Ex:
|
45
45
|
#
|
46
46
|
# Searchlogic::Search.new(User, {}, {:username_like => "bjohnson"})
|
@@ -49,16 +49,16 @@ module Searchlogic
|
|
49
49
|
self.current_scope = current_scope
|
50
50
|
self.conditions = conditions if conditions.is_a?(Hash)
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
def clone
|
54
54
|
self.class.new(klass, current_scope && current_scope.clone, conditions.clone)
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# Returns a hash of the current conditions set.
|
58
58
|
def conditions
|
59
59
|
@conditions ||= {}
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
# Accepts a hash of conditions.
|
63
63
|
def conditions=(values)
|
64
64
|
values.each do |condition, value|
|
@@ -67,7 +67,7 @@ module Searchlogic
|
|
67
67
|
send("#{condition}=", value)
|
68
68
|
end
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
# Delete a condition from the search. Since conditions map to named scopes,
|
72
72
|
# if a named scope accepts a parameter there is no way to actually delete
|
73
73
|
# the scope if you do not want it anymore. A nil value might be meaningful
|
@@ -76,13 +76,13 @@ module Searchlogic
|
|
76
76
|
names.each { |name| @conditions.delete(name.to_sym) }
|
77
77
|
self
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
private
|
81
81
|
def method_missing(name, *args, &block)
|
82
82
|
if name.to_s =~ /(\w+)=$/
|
83
83
|
condition = $1.to_sym
|
84
84
|
scope_name = normalize_scope_name($1)
|
85
|
-
if scope?(scope_name)
|
85
|
+
if scope?(scope_name)
|
86
86
|
conditions[condition] = type_cast(args.first, cast_type(scope_name))
|
87
87
|
else
|
88
88
|
raise UnknownConditionError.new(name)
|
@@ -100,7 +100,7 @@ module Searchlogic
|
|
100
100
|
scope_name = normalize_scope_name(scope_name)
|
101
101
|
klass.send(scope_name, value) if !klass.respond_to?(scope_name)
|
102
102
|
arity = klass.named_scope_arity(scope_name)
|
103
|
-
|
103
|
+
|
104
104
|
if !arity || arity == 0
|
105
105
|
if value == true
|
106
106
|
scope.send(scope_name)
|
@@ -114,15 +114,15 @@ module Searchlogic
|
|
114
114
|
scope.send(name, *args, &block)
|
115
115
|
end
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
def normalize_scope_name(scope_name)
|
119
119
|
klass.column_names.include?(scope_name.to_s) ? "#{scope_name}_equals".to_sym : scope_name.to_sym
|
120
120
|
end
|
121
|
-
|
121
|
+
|
122
122
|
def scope?(scope_name)
|
123
123
|
klass.scopes.key?(scope_name) || klass.condition?(scope_name)
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
def cast_type(name)
|
127
127
|
klass.send(name, nil) if !klass.respond_to?(name) # We need to set up the named scope if it doesn't exist, so we can get a value for named_ssope_options
|
128
128
|
named_scope_options = klass.named_scope_options(name)
|
@@ -133,7 +133,7 @@ module Searchlogic
|
|
133
133
|
named_scope_options.respond_to?(:searchlogic_arg_type) ? named_scope_options.searchlogic_arg_type : :string
|
134
134
|
end
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
def type_cast(value, type)
|
138
138
|
case value
|
139
139
|
when Array
|
@@ -148,5 +148,4 @@ module Searchlogic
|
|
148
148
|
end
|
149
149
|
end
|
150
150
|
end
|
151
|
-
end
|
152
|
-
|
151
|
+
end
|
data/searchlogic.gemspec
CHANGED
@@ -6,23 +6,22 @@ describe "Ordering" do
|
|
6
6
|
User.ascend_by_username
|
7
7
|
User.should respond_to(:ascend_by_username)
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
it "should have ascending" do
|
11
11
|
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
12
12
|
User.ascend_by_username.all.should == User.all(:order => "username ASC")
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
it "should have descending" do
|
16
16
|
%w(bjohnson thunt).each { |username| User.create(:username => username) }
|
17
17
|
User.descend_by_username.all.should == User.all(:order => "username DESC")
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it "should have order" do
|
21
|
-
User.
|
21
|
+
User.order("ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
it "should have priorty to columns over conflicting association columns" do
|
25
25
|
Company.ascend_by_users_count
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
data/spec/search_spec.rb
CHANGED
@@ -299,15 +299,7 @@ describe "Search" do
|
|
299
299
|
end
|
300
300
|
|
301
301
|
it "should recognize the order condition" do
|
302
|
-
User.search(:
|
303
|
-
end
|
304
|
-
|
305
|
-
it "should return actual ordering condition" do
|
306
|
-
User.search(:order_by => "ascend_by_username").order_by.should == "ascend_by_username"
|
307
|
-
end
|
308
|
-
|
309
|
-
it "should return nil when no actual ordering condition" do
|
310
|
-
User.search.order_by.should == nil
|
302
|
+
User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
|
311
303
|
end
|
312
304
|
end
|
313
305
|
end
|