searchable_record 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +11 -0
- data/Manifest +11 -0
- data/README.rdoc +164 -0
- data/Rakefile +25 -21
- data/lib/searchable_record/core.rb +270 -0
- data/lib/{util.rb → searchable_record/util.rb} +1 -1
- data/lib/searchable_record/version.rb +11 -0
- data/lib/searchable_record.rb +5 -263
- data/searchable_record.gemspec +37 -0
- data/spec/searchable_record_spec.rb +115 -115
- data/spec/searchable_record_spec_helper.rb +3 -3
- data/spec/util_spec.rb +12 -12
- metadata +27 -19
- data/History.txt +0 -7
- data/MIT-LICENSE.txt +0 -19
- data/Manifest.txt +0 -10
- data/README.txt +0 -128
data/README.txt
DELETED
@@ -1,128 +0,0 @@
|
|
1
|
-
= SearchableRecord
|
2
|
-
|
3
|
-
SearchableRecord is a small Ruby on Rails plugin that makes the parsing of
|
4
|
-
query parameters from URLs easy for resources, allowing the requester to
|
5
|
-
control the items (records) shown in the resource's representation.
|
6
|
-
|
7
|
-
The implementation is a helper module (a mixin) for ActiveRecord models. It
|
8
|
-
is used by including SearchableRecord module in a model.
|
9
|
-
|
10
|
-
The mixin provides a class method, <tt>SearchableRecord#find_queried</tt>,
|
11
|
-
to the class that includes it. The method is a front-end to
|
12
|
-
ActiveRecord::Base#find: it parses query parameters against the given rules
|
13
|
-
and calls <tt>find</tt> accordingly, returning the results of <tt>find</tt>.
|
14
|
-
|
15
|
-
== A usage example
|
16
|
-
|
17
|
-
The following example, although a bit contrived, allows the client to
|
18
|
-
|
19
|
-
* limit the number of items as the result of the search
|
20
|
-
(<tt>limit</tt> parameter),
|
21
|
-
* set an offset for the items (<tt>offset</tt> parameter, intended to be
|
22
|
-
used together with <tt>limit</tt>),
|
23
|
-
* sort the items either in ascending (<tt>sort</tt> parameter) or
|
24
|
-
descending (<tt>rsort</tt> parameter) order by items' type and name,
|
25
|
-
* to limit the result by matching only items that were update before
|
26
|
-
(<tt>until</tt> parameter) or after (<tt>since</tt> parameter) a certain
|
27
|
-
date, and
|
28
|
-
* to limit the result by matching only items with certain kind of
|
29
|
-
types (<tt>type</tt> parameter) or names (<tt>name</tt> parameter), or
|
30
|
-
both (for a name, a conversion to the client supplied parameter must be
|
31
|
-
applied before matching the name in the database).
|
32
|
-
|
33
|
-
|
34
|
-
These requirements for the query parameters are expressed as the following
|
35
|
-
rules:
|
36
|
-
|
37
|
-
rules = {
|
38
|
-
:limit => nil, # key as a flag; the value for the key is not used
|
39
|
-
:offset => nil, # key as a flag
|
40
|
-
:sort => { 'name' => 'items.name', 'created' => 'items.created_at' },
|
41
|
-
:rsort => nil, # rsort is allowed according to rules in :sort (key as a flag)
|
42
|
-
:since => 'items.created_at', # cast parameter value as the default type
|
43
|
-
:until => 'items.created_at', # cast parameter value as the default type
|
44
|
-
:patterns => { :type => 'items.type', # match the pattern with the default operator and converter
|
45
|
-
:name => { :column => 'items.name',
|
46
|
-
:converter => lambda { |val| "%#{val.gsub('_', '.')}%" } } }
|
47
|
-
# match the pattern with the default operator
|
48
|
-
}
|
49
|
-
|
50
|
-
The client uses the URL
|
51
|
-
<tt>http://example-site.org/items?limit=5&offset=4&rsort=name&since=2008-02-28&name=foo_bar</tt>
|
52
|
-
to fetch a representation of the resource containing the items. The action
|
53
|
-
results to the following parameters:
|
54
|
-
|
55
|
-
# => query_params = {
|
56
|
-
# 'offset' => '4',
|
57
|
-
# 'limit' => '5',
|
58
|
-
# 'rsort' => 'name',
|
59
|
-
# 'until' => '2008-02-28',
|
60
|
-
# 'name' => 'foo_bar',
|
61
|
-
# ...
|
62
|
-
# # plus Rails-specific parameters, such as 'action' and 'controller'
|
63
|
-
# }
|
64
|
-
|
65
|
-
In addition, the application happens to require some options to be passed to
|
66
|
-
<tt>find</tt>:
|
67
|
-
|
68
|
-
options = {
|
69
|
-
:include => [ :owners ],
|
70
|
-
:conditions => "items.flag = 'f'"
|
71
|
-
}
|
72
|
-
|
73
|
-
When <tt>find_queried</tt> is called, with
|
74
|
-
|
75
|
-
Item.find_queried(:all, query_params, rules, options)
|
76
|
-
|
77
|
-
the result is the following call to <tt>find</tt>.
|
78
|
-
|
79
|
-
Item.find(:all,
|
80
|
-
:include => [ :owners ],
|
81
|
-
:order => 'items.name desc',
|
82
|
-
:offset => 4,
|
83
|
-
:limit => 5,
|
84
|
-
:conditions => [ "(items.flag = 'f') and (items.created_at <= cast(:until as datetime)) and (items.name like :name)",
|
85
|
-
{ :until => '2008-02-28', :name => '%foo.bar%' } ])
|
86
|
-
|
87
|
-
The search result for <tt>find</tt> contains at most 5 items that are
|
88
|
-
|
89
|
-
* from offset 4 (that is, items from positions 5 to 9),
|
90
|
-
* sorted in descending order by items' names,
|
91
|
-
* updated since 2008-02-28, and
|
92
|
-
* have <tt>foo.bar</tt> in their name.
|
93
|
-
|
94
|
-
See +find_queried+ method in SearchableRecord::ClassMethods for usage
|
95
|
-
documentation.
|
96
|
-
|
97
|
-
== Installation
|
98
|
-
|
99
|
-
In order to install the plugin as a Ruby gem for a Rails application,
|
100
|
-
edit the <tt>environment.rb</tt> file of the application to contain the
|
101
|
-
following line:
|
102
|
-
|
103
|
-
config.gem "searchable_record"
|
104
|
-
|
105
|
-
(This requires Rails version 2.1 or above.)
|
106
|
-
|
107
|
-
Then install the gem, either using the Rakefile of the Rails application:
|
108
|
-
|
109
|
-
rake gems:install
|
110
|
-
|
111
|
-
...or with the <tt>gem</tt> tool:
|
112
|
-
|
113
|
-
gem install searchable_record
|
114
|
-
|
115
|
-
Use git to get the source code for modifications and hacks:
|
116
|
-
|
117
|
-
git clone git://gitorious.org/searchable-rec/mainline.git
|
118
|
-
|
119
|
-
== Contacting
|
120
|
-
|
121
|
-
Please send comments, suggestions, bugs, or patches by email to Tuomas
|
122
|
-
Kareinen < tkareine (at) gmail (dot) com >.
|
123
|
-
|
124
|
-
== Legal note
|
125
|
-
|
126
|
-
Copyright (c) 2008 Tuomas Kareinen.
|
127
|
-
|
128
|
-
SearchableRecord plugin is licensed under the MIT license.
|