searchlogic 1.6.6 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/.gitignore +6 -0
  2. data/CHANGELOG.rdoc +17 -0
  3. data/{MIT-LICENSE → LICENSE} +2 -2
  4. data/README.rdoc +128 -379
  5. data/Rakefile +56 -20
  6. data/VERSION.yml +4 -0
  7. data/init.rb +1 -1
  8. data/lib/searchlogic.rb +18 -98
  9. data/lib/searchlogic/core_ext/object.rb +33 -13
  10. data/lib/searchlogic/core_ext/proc.rb +11 -0
  11. data/lib/searchlogic/named_scopes/alias_scope.rb +63 -0
  12. data/lib/searchlogic/named_scopes/associations.rb +126 -0
  13. data/lib/searchlogic/named_scopes/conditions.rb +215 -0
  14. data/lib/searchlogic/named_scopes/ordering.rb +53 -0
  15. data/lib/searchlogic/rails_helpers.rb +69 -0
  16. data/lib/searchlogic/search.rb +146 -0
  17. data/rails/init.rb +1 -0
  18. data/searchlogic.gemspec +69 -0
  19. data/spec/core_ext/object_spec.rb +7 -0
  20. data/spec/core_ext/proc_spec.rb +9 -0
  21. data/spec/named_scopes/alias_scope_spec.rb +15 -0
  22. data/spec/named_scopes/associations_spec.rb +120 -0
  23. data/spec/named_scopes/conditions_spec.rb +253 -0
  24. data/spec/named_scopes/ordering_spec.rb +23 -0
  25. data/spec/search_spec.rb +283 -0
  26. data/spec/spec_helper.rb +78 -0
  27. metadata +40 -231
  28. data/Manifest.txt +0 -158
  29. data/TODO.rdoc +0 -4
  30. data/lib/searchlogic/active_record/associations.rb +0 -52
  31. data/lib/searchlogic/active_record/base.rb +0 -224
  32. data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +0 -176
  33. data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +0 -172
  34. data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +0 -80
  35. data/lib/searchlogic/condition/base.rb +0 -165
  36. data/lib/searchlogic/condition/begins_with.rb +0 -17
  37. data/lib/searchlogic/condition/blank.rb +0 -24
  38. data/lib/searchlogic/condition/child_of.rb +0 -11
  39. data/lib/searchlogic/condition/descendant_of.rb +0 -11
  40. data/lib/searchlogic/condition/ends_with.rb +0 -17
  41. data/lib/searchlogic/condition/equals.rb +0 -33
  42. data/lib/searchlogic/condition/greater_than.rb +0 -15
  43. data/lib/searchlogic/condition/greater_than_or_equal_to.rb +0 -15
  44. data/lib/searchlogic/condition/inclusive_descendant_of.rb +0 -10
  45. data/lib/searchlogic/condition/keywords.rb +0 -52
  46. data/lib/searchlogic/condition/less_than.rb +0 -15
  47. data/lib/searchlogic/condition/less_than_or_equal_to.rb +0 -15
  48. data/lib/searchlogic/condition/like.rb +0 -15
  49. data/lib/searchlogic/condition/nested_set.rb +0 -17
  50. data/lib/searchlogic/condition/nil.rb +0 -21
  51. data/lib/searchlogic/condition/not_begin_with.rb +0 -20
  52. data/lib/searchlogic/condition/not_blank.rb +0 -19
  53. data/lib/searchlogic/condition/not_end_with.rb +0 -20
  54. data/lib/searchlogic/condition/not_equal.rb +0 -27
  55. data/lib/searchlogic/condition/not_have_keywords.rb +0 -20
  56. data/lib/searchlogic/condition/not_like.rb +0 -20
  57. data/lib/searchlogic/condition/not_nil.rb +0 -19
  58. data/lib/searchlogic/condition/sibling_of.rb +0 -14
  59. data/lib/searchlogic/conditions/any_or_all.rb +0 -42
  60. data/lib/searchlogic/conditions/base.rb +0 -244
  61. data/lib/searchlogic/conditions/groups.rb +0 -74
  62. data/lib/searchlogic/conditions/magic_methods.rb +0 -286
  63. data/lib/searchlogic/conditions/multiparameter_attributes.rb +0 -105
  64. data/lib/searchlogic/conditions/protection.rb +0 -36
  65. data/lib/searchlogic/config.rb +0 -31
  66. data/lib/searchlogic/config/helpers.rb +0 -338
  67. data/lib/searchlogic/config/search.rb +0 -53
  68. data/lib/searchlogic/core_ext/hash.rb +0 -75
  69. data/lib/searchlogic/helpers/control_types/link.rb +0 -310
  70. data/lib/searchlogic/helpers/control_types/links.rb +0 -242
  71. data/lib/searchlogic/helpers/control_types/remote_link.rb +0 -87
  72. data/lib/searchlogic/helpers/control_types/remote_links.rb +0 -72
  73. data/lib/searchlogic/helpers/control_types/remote_select.rb +0 -36
  74. data/lib/searchlogic/helpers/control_types/select.rb +0 -82
  75. data/lib/searchlogic/helpers/form.rb +0 -208
  76. data/lib/searchlogic/helpers/utilities.rb +0 -197
  77. data/lib/searchlogic/modifiers/absolute.rb +0 -15
  78. data/lib/searchlogic/modifiers/acos.rb +0 -11
  79. data/lib/searchlogic/modifiers/asin.rb +0 -11
  80. data/lib/searchlogic/modifiers/atan.rb +0 -11
  81. data/lib/searchlogic/modifiers/avg.rb +0 -15
  82. data/lib/searchlogic/modifiers/base.rb +0 -27
  83. data/lib/searchlogic/modifiers/ceil.rb +0 -15
  84. data/lib/searchlogic/modifiers/char_length.rb +0 -15
  85. data/lib/searchlogic/modifiers/cos.rb +0 -15
  86. data/lib/searchlogic/modifiers/cot.rb +0 -15
  87. data/lib/searchlogic/modifiers/count.rb +0 -11
  88. data/lib/searchlogic/modifiers/day_of_month.rb +0 -15
  89. data/lib/searchlogic/modifiers/day_of_week.rb +0 -15
  90. data/lib/searchlogic/modifiers/day_of_year.rb +0 -15
  91. data/lib/searchlogic/modifiers/degrees.rb +0 -11
  92. data/lib/searchlogic/modifiers/exp.rb +0 -15
  93. data/lib/searchlogic/modifiers/floor.rb +0 -15
  94. data/lib/searchlogic/modifiers/hex.rb +0 -11
  95. data/lib/searchlogic/modifiers/hour.rb +0 -11
  96. data/lib/searchlogic/modifiers/log.rb +0 -15
  97. data/lib/searchlogic/modifiers/log10.rb +0 -11
  98. data/lib/searchlogic/modifiers/log2.rb +0 -11
  99. data/lib/searchlogic/modifiers/lower.rb +0 -15
  100. data/lib/searchlogic/modifiers/ltrim.rb +0 -15
  101. data/lib/searchlogic/modifiers/md5.rb +0 -11
  102. data/lib/searchlogic/modifiers/microseconds.rb +0 -11
  103. data/lib/searchlogic/modifiers/milliseconds.rb +0 -11
  104. data/lib/searchlogic/modifiers/minute.rb +0 -15
  105. data/lib/searchlogic/modifiers/month.rb +0 -15
  106. data/lib/searchlogic/modifiers/octal.rb +0 -15
  107. data/lib/searchlogic/modifiers/radians.rb +0 -11
  108. data/lib/searchlogic/modifiers/round.rb +0 -11
  109. data/lib/searchlogic/modifiers/rtrim.rb +0 -15
  110. data/lib/searchlogic/modifiers/second.rb +0 -15
  111. data/lib/searchlogic/modifiers/sign.rb +0 -11
  112. data/lib/searchlogic/modifiers/sin.rb +0 -11
  113. data/lib/searchlogic/modifiers/square_root.rb +0 -15
  114. data/lib/searchlogic/modifiers/sum.rb +0 -11
  115. data/lib/searchlogic/modifiers/tan.rb +0 -15
  116. data/lib/searchlogic/modifiers/trim.rb +0 -15
  117. data/lib/searchlogic/modifiers/upper.rb +0 -15
  118. data/lib/searchlogic/modifiers/week.rb +0 -11
  119. data/lib/searchlogic/modifiers/year.rb +0 -11
  120. data/lib/searchlogic/search/base.rb +0 -148
  121. data/lib/searchlogic/search/conditions.rb +0 -53
  122. data/lib/searchlogic/search/ordering.rb +0 -244
  123. data/lib/searchlogic/search/pagination.rb +0 -121
  124. data/lib/searchlogic/search/protection.rb +0 -89
  125. data/lib/searchlogic/search/searching.rb +0 -32
  126. data/lib/searchlogic/shared/utilities.rb +0 -57
  127. data/lib/searchlogic/shared/virtual_classes.rb +0 -39
  128. data/lib/searchlogic/version.rb +0 -79
  129. data/test/active_record_tests/associations_test.rb +0 -94
  130. data/test/active_record_tests/base_test.rb +0 -115
  131. data/test/condition_tests/base_test.rb +0 -62
  132. data/test/condition_tests/begins_with_test.rb +0 -11
  133. data/test/condition_tests/blank_test.rb +0 -31
  134. data/test/condition_tests/child_of_test.rb +0 -17
  135. data/test/condition_tests/descendant_of_test.rb +0 -12
  136. data/test/condition_tests/ends_with_test.rb +0 -11
  137. data/test/condition_tests/equals_test.rb +0 -28
  138. data/test/condition_tests/greater_than_or_equal_to_test.rb +0 -11
  139. data/test/condition_tests/greater_than_test.rb +0 -11
  140. data/test/condition_tests/inclusive_descendant_of_test.rb +0 -12
  141. data/test/condition_tests/keywords_test.rb +0 -23
  142. data/test/condition_tests/less_than_or_equal_to_test.rb +0 -11
  143. data/test/condition_tests/less_than_test.rb +0 -11
  144. data/test/condition_tests/like_test.rb +0 -11
  145. data/test/condition_tests/nil_test.rb +0 -31
  146. data/test/condition_tests/not_begin_with_test.rb +0 -8
  147. data/test/condition_tests/not_blank_test.rb +0 -8
  148. data/test/condition_tests/not_end_with_test.rb +0 -8
  149. data/test/condition_tests/not_equal_test.rb +0 -19
  150. data/test/condition_tests/not_have_keywords_test.rb +0 -8
  151. data/test/condition_tests/not_like_test.rb +0 -8
  152. data/test/condition_tests/not_nil_test.rb +0 -13
  153. data/test/condition_tests/sibling_of_test.rb +0 -15
  154. data/test/conditions_tests/any_or_all_test.rb +0 -23
  155. data/test/conditions_tests/base_test.rb +0 -185
  156. data/test/conditions_tests/groups_test.rb +0 -68
  157. data/test/conditions_tests/magic_methods_test.rb +0 -36
  158. data/test/conditions_tests/multiparameter_attributes_test.rb +0 -15
  159. data/test/conditions_tests/protection_test.rb +0 -18
  160. data/test/config_test.rb +0 -23
  161. data/test/fixtures/accounts.yml +0 -12
  162. data/test/fixtures/animals.yml +0 -7
  163. data/test/fixtures/orders.yml +0 -12
  164. data/test/fixtures/user_groups.yml +0 -5
  165. data/test/fixtures/users.yml +0 -45
  166. data/test/libs/awesome_nested_set.rb +0 -545
  167. data/test/libs/awesome_nested_set/.autotest +0 -13
  168. data/test/libs/awesome_nested_set/compatability.rb +0 -29
  169. data/test/libs/awesome_nested_set/helper.rb +0 -40
  170. data/test/libs/awesome_nested_set/named_scope.rb +0 -140
  171. data/test/libs/rexml_fix.rb +0 -14
  172. data/test/modifier_tests/day_of_month_test.rb +0 -16
  173. data/test/search_tests/base_test.rb +0 -241
  174. data/test/search_tests/conditions_test.rb +0 -21
  175. data/test/search_tests/ordering_test.rb +0 -167
  176. data/test/search_tests/pagination_test.rb +0 -74
  177. data/test/search_tests/protection_test.rb +0 -26
  178. data/test/test_helper.rb +0 -122
@@ -0,0 +1,215 @@
1
+ module Searchlogic
2
+ module NamedScopes
3
+ # Handles dynamically creating named scopes for columns.
4
+ module Conditions
5
+ COMPARISON_CONDITIONS = {
6
+ :equals => [:is, :eq],
7
+ :does_not_equal => [:not_equal_to, :is_not, :not, :ne],
8
+ :less_than => [:lt, :before],
9
+ :less_than_or_equal_to => [:lte],
10
+ :greater_than => [:gt, :after],
11
+ :greater_than_or_equal_to => [:gte],
12
+ }
13
+
14
+ WILDCARD_CONDITIONS = {
15
+ :like => [:contains, :includes],
16
+ :begins_with => [:bw],
17
+ :ends_with => [:ew],
18
+ }
19
+
20
+ BOOLEAN_CONDITIONS = {
21
+ :null => [:nil],
22
+ :empty => []
23
+ }
24
+
25
+ CONDITIONS = {}
26
+
27
+ COMPARISON_CONDITIONS.merge(WILDCARD_CONDITIONS).each do |condition, aliases|
28
+ CONDITIONS[condition] = aliases
29
+ CONDITIONS["#{condition}_any".to_sym] = aliases.collect { |a| "#{a}_any".to_sym }
30
+ CONDITIONS["#{condition}_all".to_sym] = aliases.collect { |a| "#{a}_all".to_sym }
31
+ end
32
+
33
+ BOOLEAN_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
34
+
35
+ PRIMARY_CONDITIONS = CONDITIONS.keys
36
+ ALIAS_CONDITIONS = CONDITIONS.values.flatten
37
+
38
+ # Retrieves the options passed when creating the respective named scope. Ex:
39
+ #
40
+ # named_scope :whatever, :conditions => {:column => value}
41
+ #
42
+ # This method will return:
43
+ #
44
+ # :conditions => {:column => value}
45
+ #
46
+ # ActiveRecord hides this internally, so we have to try and pull it out with this
47
+ # method.
48
+ def named_scope_options(name)
49
+ key = scopes.key?(name.to_sym) ? name.to_sym : primary_condition_name(name)
50
+
51
+ if key
52
+ eval("options", scopes[key])
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ # The arity for a named scope's proc is important, because we use the arity
59
+ # to determine if the condition should be ignored when calling the search method.
60
+ # If the condition is false and the arity is 0, then we skip it all together. Ex:
61
+ #
62
+ # User.named_scope :age_is_4, :conditions => {:age => 4}
63
+ # User.search(:age_is_4 => false) == User.all
64
+ # User.search(:age_is_4 => true) == User.all(:conditions => {:age => 4})
65
+ #
66
+ # We also use it when trying to "copy" the underlying named scope for association
67
+ # conditions.
68
+ def named_scope_arity(name)
69
+ options = named_scope_options(name)
70
+ options.respond_to?(:arity) ? options.arity : nil
71
+ end
72
+
73
+ # Returns the primary condition for the given alias. Ex:
74
+ #
75
+ # primary_condition(:gt) => :greater_than
76
+ def primary_condition(alias_condition)
77
+ CONDITIONS.find { |k, v| k == alias_condition.to_sym || v.include?(alias_condition.to_sym) }.first
78
+ end
79
+
80
+ # Returns the primary name for any condition on a column. You can pass it
81
+ # a primary condition, alias condition, etc, and it will return the proper
82
+ # primary condition name. This helps simply logic throughout Searchlogic. Ex:
83
+ #
84
+ # primary_condition_name(:id_gt) => :id_greater_than
85
+ # primary_condition_name(:id_greater_than) => :id_greater_than
86
+ def primary_condition_name(name)
87
+ if primary_condition?(name)
88
+ name.to_sym
89
+ elsif details = alias_condition_details(name)
90
+ "#{details[:column]}_#{primary_condition(details[:condition])}".to_sym
91
+ else
92
+ nil
93
+ end
94
+ end
95
+
96
+ # Is the name of the method a valid condition that can be dynamically created?
97
+ def condition?(name)
98
+ primary_condition?(name) || alias_condition?(name)
99
+ end
100
+
101
+ # Is the name of the method a valid condition that can be dynamically created,
102
+ # AND is it a primary condition (not an alias). "greater_than" not "gt".
103
+ def primary_condition?(name)
104
+ !primary_condition_details(name).nil?
105
+ end
106
+
107
+ # Is the name of the method a valid condition that can be dynamically created,
108
+ # AND is it an alias condition. "gt" not "greater_than".
109
+ def alias_condition?(name)
110
+ !alias_condition_details(name).nil?
111
+ end
112
+
113
+ private
114
+ def method_missing(name, *args, &block)
115
+ if details = primary_condition_details(name)
116
+ create_primary_condition(details[:column], details[:condition])
117
+ send(name, *args)
118
+ elsif details = alias_condition_details(name)
119
+ create_alias_condition(details[:column], details[:condition], args)
120
+ send(name, *args)
121
+ else
122
+ super
123
+ end
124
+ end
125
+
126
+ def primary_condition_details(name)
127
+ if name.to_s =~ /^(#{column_names.join("|")})_(#{PRIMARY_CONDITIONS.join("|")})$/
128
+ {:column => $1, :condition => $2}
129
+ end
130
+ end
131
+
132
+ def create_primary_condition(column, condition)
133
+ column_type = columns_hash[column.to_s].type
134
+ scope_options = case condition.to_s
135
+ when /^equals/
136
+ scope_options(condition, column_type, "#{table_name}.#{column} = ?")
137
+ when /^does_not_equal/
138
+ scope_options(condition, column_type, "#{table_name}.#{column} != ?")
139
+ when /^less_than_or_equal_to/
140
+ scope_options(condition, column_type, "#{table_name}.#{column} <= ?")
141
+ when /^less_than/
142
+ scope_options(condition, column_type, "#{table_name}.#{column} < ?")
143
+ when /^greater_than_or_equal_to/
144
+ scope_options(condition, column_type, "#{table_name}.#{column} >= ?")
145
+ when /^greater_than/
146
+ scope_options(condition, column_type, "#{table_name}.#{column} > ?")
147
+ when /^like/
148
+ scope_options(condition, column_type, "#{table_name}.#{column} LIKE ?", :like)
149
+ when /^begins_with/
150
+ scope_options(condition, column_type, "#{table_name}.#{column} LIKE ?", :begins_with)
151
+ when /^ends_with/
152
+ scope_options(condition, column_type, "#{table_name}.#{column} LIKE ?", :ends_with)
153
+ when "null"
154
+ {:conditions => "#{table_name}.#{column} IS NULL"}
155
+ when "empty"
156
+ {:conditions => "#{table_name}.#{column} = ''"}
157
+ end
158
+
159
+ named_scope("#{column}_#{condition}".to_sym, scope_options)
160
+ end
161
+
162
+ # This method helps cut down on defining scope options for conditions that allow *_any or *_all conditions.
163
+ # Kepp in mind that the lambdas get cached in a method, so you want to keep the contents of the lambdas as
164
+ # fast as possible, which is why I didn't do the case statement inside of the lambda.
165
+ def scope_options(condition, column_type, sql, value_modifier = nil)
166
+ case condition.to_s
167
+ when /_(any|all)$/
168
+ searchlogic_lambda(column_type) { |*values|
169
+ return {} if values.empty?
170
+ values = values.flatten
171
+
172
+ values_to_sub = nil
173
+ if value_modifier.nil?
174
+ values_to_sub = values
175
+ else
176
+ values_to_sub = values.collect { |value| value_with_modifier(value, value_modifier) }
177
+ end
178
+
179
+ join = $1 == "any" ? " OR " : " AND "
180
+ {:conditions => [values.collect { |value| sql }.join(join), *values_to_sub]}
181
+ }
182
+ else
183
+ searchlogic_lambda(column_type) { |value| {:conditions => [sql, value_with_modifier(value, value_modifier)]} }
184
+ end
185
+ end
186
+
187
+ def value_with_modifier(value, modifier)
188
+ case modifier
189
+ when :like
190
+ "%#{value}%"
191
+ when :begins_with
192
+ "#{value}%"
193
+ when :ends_with
194
+ "%#{value}"
195
+ else
196
+ value
197
+ end
198
+ end
199
+
200
+ def alias_condition_details(name)
201
+ if name.to_s =~ /^(#{column_names.join("|")})_(#{ALIAS_CONDITIONS.join("|")})$/
202
+ {:column => $1, :condition => $2}
203
+ end
204
+ end
205
+
206
+ def create_alias_condition(column, condition, args)
207
+ primary_condition = primary_condition(condition)
208
+ alias_name = "#{column}_#{condition}"
209
+ primary_name = "#{column}_#{primary_condition}"
210
+ send(primary_name, *args) # go back to method_missing and make sure we create the method
211
+ (class << self; self; end).class_eval { alias_method alias_name, primary_name }
212
+ end
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,53 @@
1
+ module Searchlogic
2
+ module NamedScopes
3
+ # Handles dynamically creating named scopes for orderin by columns.
4
+ module Ordering
5
+ def condition?(name) # :nodoc:
6
+ super || order_condition?(name)
7
+ end
8
+
9
+ def primary_condition_name(name) # :nodoc
10
+ if result = super
11
+ result
12
+ elsif order_condition?(name)
13
+ name.to_sym
14
+ else
15
+ nil
16
+ end
17
+ end
18
+
19
+ def order_condition?(name) # :nodoc:
20
+ !order_condition_details(name).nil?
21
+ end
22
+
23
+ private
24
+ def method_missing(name, *args, &block)
25
+ if name == :order
26
+ named_scope name, lambda { |scope_name|
27
+ return {} if !order_condition?(scope_name)
28
+ send(scope_name).proxy_options
29
+ }
30
+ send(name, *args)
31
+ elsif details = order_condition_details(name)
32
+ create_order_conditions(details[:column])
33
+ send(name, *args)
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def order_condition_details(name)
40
+ if name.to_s =~ /^(ascend|descend)_by_(\w+)$/
41
+ {:order_as => $1, :column => $2}
42
+ elsif name.to_s =~ /^order$/
43
+ {}
44
+ end
45
+ end
46
+
47
+ def create_order_conditions(column)
48
+ named_scope("ascend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} ASC"})
49
+ named_scope("descend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} DESC"})
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,69 @@
1
+ module Searchlogic
2
+ module RailsHelpers
3
+ # Creates a link that alternates between acending and descending. It basically
4
+ # alternates between calling 2 named scopes: "ascend_by_*" and "descend_by_*"
5
+ #
6
+ # By default Searchlogic gives you these named scopes for all of your columns, but
7
+ # if you wanted to create your own, it will work with those too.
8
+ #
9
+ # Examples:
10
+ #
11
+ # order @search, :by => :username
12
+ # order @search, :by => :created_at, :as => "Created"
13
+ #
14
+ # This helper accepts the following options:
15
+ #
16
+ # * <tt>:by</tt> - the name of the named scope. This helper will prepend this value with "ascend_by_" and "descend_by_"
17
+ # * <tt>:as</tt> - the text used in the link, defaults to whatever is passed to :by
18
+ # * <tt>:ascend_scope</tt> - what scope to call for ascending the data, defaults to "ascend_by_:by"
19
+ # * <tt>:descend_scope</tt> - what scope to call for descending the data, defaults to "descend_by_:by"
20
+ # * <tt>:params_scope</tt> - the name of the params key to scope the order condition by, defaults to :search
21
+ def order(search, options = {}, html_options = {})
22
+ options[:params_scope] ||= :search
23
+ options[:as] ||= options[:by].to_s.humanize
24
+ options[:ascend_scope] ||= "ascend_by_#{options[:by]}"
25
+ options[:descend_scope] ||= "descend_by_#{options[:by]}"
26
+ ascending = search.order.to_s == options[:ascend_scope]
27
+ new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
28
+ selected = [options[:ascend_scope], options[:descend_scope]].include?(search.order.to_s)
29
+ if selected
30
+ css_classes = html_options[:class] ? html_options[:class].split(" ") : []
31
+ if ascending
32
+ options[:as] = "&#9650;&nbsp;#{options[:as]}"
33
+ css_classes << "ascending"
34
+ else
35
+ options[:as] = "&#9660;&nbsp;#{options[:as]}"
36
+ css_classes << "descending"
37
+ end
38
+ html_options[:class] = css_classes.join(" ")
39
+ end
40
+ link_to options[:as], url_for(options[:params_scope] => search.conditions.merge( { :order => new_scope } ) ), html_options
41
+ end
42
+
43
+ # Automatically makes the form method :get if a Searchlogic::Search and sets
44
+ # the params scope to :search
45
+ def form_for(*args, &block)
46
+ if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
47
+ options = args.extract_options!
48
+ options[:html] ||= {}
49
+ options[:html][:method] ||= :get
50
+ options[:url] ||= url_for
51
+ args.unshift(:search) if args.first == search_obj
52
+ args << options
53
+ end
54
+ super
55
+ end
56
+
57
+ # Automatically adds an "order" hidden field in your form to preserve how the data
58
+ # is being ordered.
59
+ def fields_for(*args, &block)
60
+ if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) }
61
+ args.unshift(:search) if args.first == search_obj
62
+ concat(hidden_field_tag("#{args.first}[order]", search_obj.order) + "\n")
63
+ super
64
+ else
65
+ super
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,146 @@
1
+ module Searchlogic
2
+ # A class that acts like a model, creates attr_accessors for named_scopes, and then
3
+ # chains together everything when an "action" method is called. It basically makes
4
+ # implementing search forms in your application effortless:
5
+ #
6
+ # search = User.search
7
+ # search.username_like = "bjohnson"
8
+ # search.all
9
+ #
10
+ # Is equivalent to:
11
+ #
12
+ # User.search(:username_like => "bjohnson").all
13
+ #
14
+ # Is equivalent to:
15
+ #
16
+ # User.username_like("bjohnson").all
17
+ class Search
18
+ # Responsible for adding a "search" method into your models.
19
+ module Implementation
20
+ # Returns a new Search object for the given model.
21
+ def search(conditions = {})
22
+ Search.new(self, scope(:find), conditions)
23
+ end
24
+ end
25
+
26
+ # Is an invalid condition is used this error will be raised. Ex:
27
+ #
28
+ # User.search(:unkown => true)
29
+ #
30
+ # Where unknown is not a valid named scope for the User model.
31
+ class UnknownConditionError < StandardError
32
+ def initialize(condition)
33
+ msg = "The #{condition} is not a valid condition. You may only use conditions that map to a named scope"
34
+ super(msg)
35
+ end
36
+ end
37
+
38
+ attr_accessor :klass, :current_scope, :conditions
39
+
40
+ # Creates a new search object for the given class. Ex:
41
+ #
42
+ # Searchlogic::Search.new(User, {}, {:username_like => "bjohnson"})
43
+ def initialize(klass, current_scope, conditions = {})
44
+ self.klass = klass
45
+ self.current_scope = current_scope
46
+ self.conditions = conditions if conditions.is_a?(Hash)
47
+ end
48
+
49
+ def clone
50
+ self.class.new(klass, current_scope.clone, conditions.clone)
51
+ end
52
+
53
+ # Returns a hash of the current conditions set.
54
+ def conditions
55
+ @conditions ||= {}
56
+ end
57
+
58
+ # Accepts a hash of conditions.
59
+ def conditions=(values)
60
+ values.each do |condition, value|
61
+ value.delete_if { |v| v.blank? } if value.is_a?(Array)
62
+ next if value.blank?
63
+ send("#{condition}=", value)
64
+ end
65
+ end
66
+
67
+ # Delete a condition from the search. Since conditions map to named scopes,
68
+ # if a named scope accepts a parameter there is no way to actually delete
69
+ # the scope if you do not want it anymore. A nil value might be meaningful
70
+ # to that scope.
71
+ def delete(*names)
72
+ names.each { |name| @conditions.delete(name.to_sym) }
73
+ self
74
+ end
75
+
76
+ private
77
+ def method_missing(name, *args, &block)
78
+ if name.to_s =~ /(\w+)=$/
79
+ condition = $1.to_sym
80
+ scope_name = normalize_scope_name($1)
81
+ if scope?(scope_name)
82
+ conditions[condition] = type_cast(args.first, cast_type(scope_name))
83
+ else
84
+ raise UnknownConditionError.new(name)
85
+ end
86
+ elsif scope?(normalize_scope_name(name))
87
+ if args.size > 0
88
+ send("#{name}=", *args)
89
+ self
90
+ else
91
+ conditions[name]
92
+ end
93
+ else
94
+ scope = conditions.inject(klass.scoped(current_scope)) do |scope, condition|
95
+ scope_name, value = condition
96
+ scope_name = normalize_scope_name(scope_name)
97
+ klass.send(scope_name, value) if !klass.respond_to?(scope_name)
98
+ arity = klass.named_scope_arity(scope_name)
99
+
100
+ if !arity || arity == 0
101
+ if value == true
102
+ scope.send(scope_name)
103
+ else
104
+ scope
105
+ end
106
+ else
107
+ scope.send(scope_name, value)
108
+ end
109
+ end
110
+ scope.send(name, *args, &block)
111
+ end
112
+ end
113
+
114
+ def normalize_scope_name(scope_name)
115
+ klass.column_names.include?(scope_name.to_s) ? "#{scope_name}_equals".to_sym : scope_name.to_sym
116
+ end
117
+
118
+ def scope?(scope_name)
119
+ klass.scopes.key?(scope_name) || klass.condition?(scope_name)
120
+ end
121
+
122
+ def cast_type(name)
123
+ 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
124
+ named_scope_options = klass.named_scope_options(name)
125
+ arity = klass.named_scope_arity(name)
126
+ if !arity || arity == 0
127
+ :boolean
128
+ else
129
+ named_scope_options.respond_to?(:searchlogic_arg_type) ? named_scope_options.searchlogic_arg_type : :string
130
+ end
131
+ end
132
+
133
+ def type_cast(value, type)
134
+ case value
135
+ when Array
136
+ value.collect { |v| type_cast(v, type) }
137
+ else
138
+ # Let's leverage ActiveRecord's type casting, so that casting is consistent
139
+ # with the other models.
140
+ column_for_type_cast = ActiveRecord::ConnectionAdapters::Column.new("", nil)
141
+ column_for_type_cast.instance_variable_set(:@type, type)
142
+ column_for_type_cast.type_cast(value)
143
+ end
144
+ end
145
+ end
146
+ end