README.rdoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. = MetaSearch
  2. MetaSearch is extensible searching for your form_for enjoyment. It “wraps” one of your ActiveRecord models, providing methods that allow you to build up search conditions against that model, and has a few extra form helpers to simplify sorting and supplying multiple parameters to your condition methods as well.
  3. == NOTE
  4. The successor to MetaSearch is {Ransack}[http://github.com/ernie/ransack]. It's got features
  5. that MetaSearch doesn't, along with some API changes. I haven't had the time to dedicate to
  6. making it bulletproof yet, so I'm releasing a 1.1.x branch of MetaSearch to help with migrations
  7. to Rails 3.1.
  8. This is intended to be a stopgap measure.
  9. t's important to note that the long-term migration path for your apps should be toward
  10. Ransack, which is written in a more sane manner that will make supporting new versions
  11. of Rails much easier going forward.
  12. == Getting Started
  13. In your Gemfile:
  14. gem "meta_search" # Last officially released gem
  15. # gem "meta_search", :git => "git://github.com/ernie/meta_search.git" # Track git repo
  16. or, to install as a plugin:
  17. rails plugin install git://github.com/ernie/meta_search.git
  18. In your controller:
  19. def index
  20. @search = Article.search(params[:search])
  21. @articles = @search.all # load all matching records
  22. # @articles = @search.relation # Retrieve the relation, to lazy-load in view
  23. # @articles = @search.paginate(:page => params[:page]) # Who doesn't love will_paginate?
  24. end
  25. In your view:
  26. <%= form_for @search, :url => articles_path, :html => {:method => :get} do |f| %>
  27. <%= f.label :title_contains %>
  28. <%= f.text_field :title_contains %><br />
  29. <%= f.label :comments_created_at_greater_than, 'With comments after' %>
  30. <%= f.datetime_select :comments_created_at_greater_than, :include_blank => true %><br />
  31. <!-- etc... -->
  32. <%= f.submit %>
  33. <% end %>
  34. Options for the search method are documented at MetaSearch::Searches::ActiveRecord.
  35. == "Wheres", and what they're good for
  36. Wheres are how MetaSearch does its magic. Wheres have a name (and possible aliases) which are
  37. appended to your model and association attributes. When you instantiate a MetaSearch::Builder
  38. against a model (manually or by calling your model's +search+ method) the builder responds to
  39. methods named for your model's attributes and associations, suffixed by the name of the Where.
  40. These are the default Wheres, broken down by the types of ActiveRecord columns they can search
  41. against:
  42. === All data types
  43. * _equals_ (alias: _eq_) - Just as it sounds.
  44. * _does_not_equal_ (aliases: _ne_, _noteq_) - The opposite of equals, oddly enough.
  45. * _in_ - Takes an array, matches on equality with any of the items in the array.
  46. * _not_in_ (aliases: _ni_, _notin_) - Like above, but negated.
  47. * _is_null_ - The column has an SQL NULL value.
  48. * _is_not_null_ - The column contains anything but NULL.
  49. === Strings
  50. * _contains_ (aliases: _like_, _matches_) - Substring match.
  51. * _does_not_contain_ (aliases: _nlike_, _nmatches_) - Negative substring match.
  52. * _starts_with_ (alias: _sw_) - Match strings beginning with the entered term.
  53. * _does_not_start_with_ (alias: _dnsw_) - The opposite of above.
  54. * _ends_with_ (alias: _ew_) - Match strings ending with the entered term.
  55. * _does_not_end_with_ (alias: _dnew_) - Negative of above.
  56. === Numbers, dates, and times
  57. * _greater_than_ (alias: _gt_) - Greater than.
  58. * _greater_than_or_equal_to_ (aliases: _gte_, _gteq_) - Greater than or equal to.
  59. * _less_than_ (alias: _lt_) - Less than.
  60. * _less_than_or_equal_to_ (aliases: _lte_, _lteq_) - Less than or equal to.
  61. === Booleans
  62. * _is_true_ - Is true. Useful for a checkbox like "only show admin users".
  63. * _is_false_ - The complement of _is_true_.
  64. === Non-boolean data types
  65. * _is_present_ - As with _is_true_, useful with a checkbox. Not NULL or the empty string.
  66. * _is_blank_ - Returns records with a value of NULL or the empty string in the column.
  67. So, given a model like this...
  68. class Article < ActiveRecord::Base
  69. belongs_to :author
  70. has_many :comments
  71. has_many :moderations, :through => :comments
  72. end
  73. ...you might end up with attributes like <tt>title_contains</tt>,
  74. <tt>comments_title_starts_with</tt>, <tt>moderations_value_less_than</tt>,
  75. <tt>author_name_equals</tt>, and so on.
  76. Additionally, all of the above predicate types also have an _any and _all version, which
  77. expects an array of the corresponding parameter type, and requires any or all of the
  78. parameters to be a match, respectively. So:
  79. Article.search :author_name_starts_with_any => ['Jim', 'Bob', 'Fred']
  80. will match articles authored by Jimmy, Bobby, or Freddy, but not Winifred.
  81. == Advanced usage
  82. === Narrowing the scope of a search
  83. While the most common use case is to simply call Model.search(params[:search]), there
  84. may be times where you want to scope your search more tightly. For instance, only allowing
  85. users to search their own projects (assuming a current_user method returning the current user):
  86. @search = current_user.projects.search(params[:search])
  87. Or, you can build up any relation you like and call the search method on that object:
  88. @projects_with_awesome_users_search =
  89. Project.joins(:user).where(:users => {:awesome => true}).search(params[:search])
  90. === ORed conditions
  91. If you'd like to match on one of several possible columns, you can do this:
  92. <%= f.text_field :title_or_description_contains %>
  93. <%= f.text_field :title_or_author_name_starts_with %>
  94. Caveats:
  95. * Only one match type is supported. You <b>can't</b> do
  96. <tt>title_matches_or_description_starts_with</tt> for instance.
  97. * If you're matching across associations, remember that the associated table will be
  98. INNER JOINed, therefore limiting results to those that at least have a corresponding
  99. record in the associated table.
  100. === Compound conditions (any/all)
  101. All Where types automatically get an "any" and "all" variant. This has the same name and
  102. aliases as the original, but is suffixed with _any and _all, for an "OR" or "AND" search,
  103. respectively. So, if you want to provide the user with 5 different search boxes to enter
  104. possible article titles:
  105. <%= f.multiparameter_field :title_contains_any,
  106. *5.times.inject([]) {|a, b| a << {:field_type => :text_field}} +
  107. [:size => 10] %>
  108. === Multi-level associations
  109. MetaSearch will allow you to traverse your associations in one form, generating the
  110. necessary joins along the way. If you have the following models...
  111. class Company < ActiveRecord::Base
  112. has_many :developers
  113. end
  114. class Developer < ActiveRecord::Base
  115. belongs_to :company
  116. has_many :notes
  117. end
  118. ...you can do this in your form to search your companies by developers with certain notes:
  119. <%= f.text_field :developers_notes_note_contains %>
  120. You can travel forward and back through the associations, so this would also work (though
  121. be entirely pointless in this case):
  122. <%= f.text_field :developers_notes_developer_company_name_contains %>
  123. However, to prevent abuse, this is limited to associations of a total "depth" of 5 levels.
  124. This means that while starting from a Company model, as above, you could do
  125. Company -> :developers -> :notes -> :developer -> :company, which has gotten you right
  126. back where you started, but "travels" through 5 models total.
  127. In the case of polymorphic belongs_to associations, things work a bit differently. Let's say
  128. you have the following models:
  129. class Article < ActiveRecord::Base
  130. has_many :comments, :as => :commentable
  131. end
  132. class Post < ActiveRecord::Base
  133. has_many :comments, :as => :commentable
  134. end
  135. class Comment < ActiveRecord::Base
  136. belongs_to :commentable, :polymorphic => true
  137. validates_presence_of :body
  138. end
  139. Your first instinct might be to set up a text field for :commentable_body_contains, but
  140. you can't do this. MetaSearch would have no way to know which class lies on the other side
  141. of the polymorphic association, so it wouldn't be able to join the correct tables.
  142. Instead, you'll follow a convention Searchlogic users are already familiar with, using the
  143. name of the polymorphic association, then the underscored class name (AwesomeClass becomes
  144. awesome_class), then the delimiter "type", to tell MetaSearch anything that follows is an
  145. attribute name. For example:
  146. <%= f.text_field :commentable_article_type_body_contains %>
  147. If you'd like to match on multiple types of polymorphic associations, you can join them
  148. with \_or_, just like any other conditions:
  149. <%= f.text_field :commentable_article_type_body_or_commentable_post_type_body_contains %>
  150. It's not pretty, but it works. Alternately, consider creating a custom search method as
  151. described below to save yourself some typing if you're creating a lot of these types of
  152. search fields.
  153. === Adding a new Where
  154. If none of the built-in search criteria work for you, you can add new Wheres. To do so,
  155. create an initializer (<tt>/config/initializers/meta_search.rb</tt>, for instance) and add lines
  156. like:
  157. MetaSearch::Where.add :between, :btw,
  158. :predicate => :in,
  159. :types => [:integer, :float, :decimal, :date, :datetime, :timestamp, :time],
  160. :formatter => Proc.new {|param| Range.new(param.first, param.last)},
  161. :validator => Proc.new {|param|
  162. param.is_a?(Array) && !(param[0].blank? || param[1].blank?)
  163. }
  164. See MetaSearch::Where for info on the supported options.
  165. === Accessing custom search methods (and named scopes!)
  166. MetaSearch can be given access to any class method on your model to extend its search capabilities.
  167. The only rule is that the method must return an ActiveRecord::Relation so that MetaSearch can
  168. continue to extend the search with other attributes. Conveniently, scopes (formerly "named scopes")
  169. do this already.
  170. Consider the following model:
  171. class Company < ActiveRecord::Base
  172. has_many :slackers, :class_name => "Developer", :conditions => {:slacker => true}
  173. scope :backwards_name, lambda {|name| where(:name => name.reverse)}
  174. scope :with_slackers_by_name_and_salary_range,
  175. lambda {|name, low, high|
  176. joins(:slackers).where(:developers => {:name => name, :salary => low..high})
  177. }
  178. end
  179. To allow MetaSearch access to a model method, including a named scope, just use
  180. <tt>search_methods</tt> in the model:
  181. search_methods :backwards_name
  182. This will allow you to add a text field named :backwards_name to your search form, and
  183. it will behave as you might expect.
  184. In the case of the second scope, we have multiple parameters to pass in, of different
  185. types. We can pass the following to <tt>search_methods</tt>:
  186. search_methods :with_slackers_by_name_and_salary_range,
  187. :splat_param => true, :type => [:string, :integer, :integer]
  188. MetaSearch needs us to tell it that we don't want to keep the array supplied to it as-is, but
  189. "splat" it when passing it to the model method. Regarding <tt>:types</tt>: In this case,
  190. ActiveRecord would have been smart enough to handle the typecasting for us, but I wanted to
  191. demonstrate how we can tell MetaSearch that a given parameter is of a specific database "column type." This is just a hint MetaSearch uses in the same way it does when casting "Where" params based
  192. on the DB column being searched. It's also important so that things like dates get handled
  193. properly by FormBuilder.
  194. === multiparameter_field
  195. The example Where above adds support for a "between" search, which requires an array with
  196. two parameters. These can be passed using Rails multiparameter attributes. To make life easier,
  197. MetaSearch adds a helper for this:
  198. <%= f.multiparameter_field :moderations_value_between,
  199. {:field_type => :text_field}, {:field_type => :text_field}, :size => 5 %>
  200. <tt>multiparameter_field</tt> works pretty much like the other FormBuilder helpers, but it
  201. lets you sandwich a list of fields, each in hash format, between the attribute and the usual
  202. options hash. See MetaSearch::Helpers::FormBuilder for more info.
  203. === checks and collection_checks
  204. If you need to get an array into your where, and you don't care about parameter order,
  205. you might choose to use a select or collection_select with multiple selection enabled,
  206. but everyone hates multiple selection boxes. MetaSearch adds a couple of additional
  207. helpers, +checks+ and +collection_checks+ to handle multiple selections in a
  208. more visually appealing manner. They can be called with or without a block. Without a
  209. block, you get an array of MetaSearch::Check objects to do with as you please.
  210. With a block, each check is yielded to your template, like so:
  211. <h4>How many heads?</h4>
  212. <ul>
  213. <% f.checks :number_of_heads_in,
  214. [['One', 1], ['Two', 2], ['Three', 3]], :class => 'checkboxy' do |check| %>
  215. <li>
  216. <%= check.box %>
  217. <%= check.label %>
  218. </li>
  219. <% end %>
  220. </ul>
  221. Again, full documentation is in MetaSearch::Helpers::FormBuilder.
  222. === Sorting columns
  223. If you'd like to sort by a specific column in your results (the attributes of the base model)
  224. or an association column then supply the <tt>meta_sort</tt> parameter in your form.
  225. The parameter takes the form <tt>column.direction</tt> where +column+ is the column name or
  226. underscore-separated association_column combination, and +direction+ is one of "asc" or "desc"
  227. for ascending or descending, respectively.
  228. Normally, you won't supply this parameter yourself, but instead will use the helper method
  229. <tt>sort_link</tt> in your views, like so:
  230. <%= sort_link @search, :title %>
  231. Or, if in the context of a form_for against a MetaSearch::Builder:
  232. <%= f.sort_link :title %>
  233. The <tt>@search</tt> object is the instance of MetaSearch::Builder you got back earlier from
  234. your controller. The other required parameter is the attribute name itself. Optionally,
  235. you can provide a string as a 3rd parameter to override the default link name, and then
  236. additional hashed for the +options+ and +html_options+ hashes for link_to.
  237. By default, the link that is created will sort by the given column in ascending order when first clicked. If you'd like to reverse this (so the first click sorts the results in descending order), you can pass +:default_order => :desc+ in the options hash, like so:
  238. <%= sort_link @search, :ratings, "Highest Rated", :default_order => :desc %>
  239. You can sort by more than one column as well, by creating a link like:
  240. <%= sort_link :name_and_salary %>
  241. If you'd like to do a custom sort, you can do so by setting up two scopes in your model:
  242. scope :sort_by_custom_name_asc, order('custom_name ASC')
  243. scope :sort_by_custom_name_desc, order('custom_name DESC')
  244. You can then do <tt>sort_link @search, :custom_name</tt> and it will work as you expect.
  245. All <tt>sort_link</tt>-generated links will have the CSS class sort_link, as well as a
  246. directional class (ascending or descending) if the link is for a currently sorted column,
  247. for your styling enjoyment.
  248. This feature should hopefully help out those of you migrating from Searchlogic, and a thanks
  249. goes out to Ben Johnson for the HTML entities used for the up and down arrows, which provide
  250. a nice default look.
  251. === Including/excluding attributes and associations
  252. If you'd like to allow only certain associations or attributes to be searched, you can do
  253. so inside your models
  254. class Article < ActiveRecord::Base
  255. attr_searchable :some_public_data, :some_more_searchable_stuff
  256. assoc_searchable :search_this_association_why_dontcha
  257. end
  258. If you'd rather blacklist attributes and associations rather than whitelist, use the
  259. <tt>attr_unsearchable</tt> and <tt>assoc_unsearchable</tt> method instead. If a
  260. whitelist is supplied, it takes precedence.
  261. Excluded attributes on a model will be honored across associations, so if an Article
  262. <tt>has_many :comments</tt> and the Comment model looks something like this:
  263. class Comment < ActiveRecord::Base
  264. validates_presence_of :user_id, :body
  265. attr_unsearchable :user_id
  266. end
  267. Then your call to <tt>Article.search</tt> will allow <tt>:comments_body_contains</tt>
  268. but not <tt>:comments_user_id_equals</tt> to be passed.
  269. === Conditional access to searches
  270. <tt>search_methods</tt>, <tt>attr_searchable</tt>, <tt>attr_unsearchable</tt>,
  271. <tt>assoc_searchable</tt>, and <tt>assoc_unsearchable</tt> all accept an <tt>:if</tt>
  272. option. If present, it should specify a Proc (or other object responding to <tt>call</tt>)
  273. that accepts a single parameter. This parameter will be the instance of the MetaSearch::Builder
  274. that gets created by a call to Model.search. Any unused search options (the second hash param)
  275. that get passed to Model.search will be available via the Builder object's <tt>options</tt>
  276. reader, and can be used for access control via this proc/object.
  277. Example:
  278. assoc_unsearchable :notes,
  279. :if => proc {|s| s.options[:access] == 'blocked' || !s.options[:access]}
  280. === Localization
  281. MetaSearch supports i18n localization in a few different ways. Consider this abbreviated
  282. example "flanders" locale:
  283. flanders:
  284. activerecord:
  285. attributes:
  286. company:
  287. name: "Company name-diddly"
  288. developer:
  289. name: "Developer name-diddly"
  290. salary: "Developer salary-doodly"
  291. meta_search:
  292. or: 'or-diddly'
  293. predicates:
  294. contains: "%{attribute} contains-diddly"
  295. equals: "%{attribute} equals-diddly"
  296. attributes:
  297. company:
  298. reverse_name: "Company reverse name-diddly"
  299. developer:
  300. name_contains: "Developer name-diddly contains-aroonie"
  301. First, MetaSearch will use a key found under meta_search.attributes.model_name.attribute_name,
  302. if it exists. As a fallback, it will use a localization based on the predicate type, along with
  303. the usual ActiveRecord attribute localization (the activerecord.attributes.model_name keys above).
  304. Additionally, a localized "or" can be specified for multi-column searches.
  305. == Contributions
  306. There are several ways you can help MetaSearch continue to improve.
  307. * Use MetaSearch in your real-world projects and {submit bug reports or feature suggestions}[http://metautonomous.lighthouseapp.com/projects/53012-metasearch/].
  308. * Better yet, if you’re so inclined, fix the issue yourself and submit a patch! Or you can {fork the project on GitHub}[http://github.com/ernie/meta_search] and send me a pull request (please include tests!)
  309. * If you like MetaSearch, spread the word. More users == more eyes on code == more bugs getting found == more bugs getting fixed (hopefully!)
  310. * Lastly, if MetaSearch has saved you hours of development time on your latest Rails gig, and you’re feeling magnanimous, please consider {making a donation}[http://pledgie.com/campaigns/9647] to the project. I have spent hours of my personal time coding and supporting MetaSearch, and your donation would go a great way toward justifying that time spent to my loving wife. :)
  311. == Copyright
  312. Copyright (c) 2010 {Ernie Miller}[http://metautonomo.us]. See LICENSE for details.