laserlemon-search_party 0.3.3 → 0.4.0
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/README.rdoc +65 -3
- data/Rakefile +1 -1
- data/VERSION.yml +2 -2
- data/lib/search_party.rb +1 -1
- data/search_party.gemspec +2 -2
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,13 +1,75 @@
|
|
1
1
|
= search_party
|
2
2
|
|
3
|
+
Rails params that actually _mean_ something!
|
4
|
+
|
5
|
+
<tt>search_party</tt> parses a Rails request's GET variables according to your own rules. This allows you to:
|
6
|
+
|
7
|
+
* Keep your controllers skinny,
|
8
|
+
* Remove models' ID numbers from your URLs,
|
9
|
+
* Prepare your query parameters for better use in <tt>searchlogic</tt>[http://github.com/binarylogic/searchlogic] by binarylogic[http://github.com/binarylogic].
|
10
|
+
|
3
11
|
== Installation
|
4
12
|
|
5
|
-
|
13
|
+
In <tt>environment.rb</tt>:
|
14
|
+
|
15
|
+
Rails::Initializer.run do |config|
|
16
|
+
config.gem 'laserlemon-search_party', :lib => 'search_party', :source => 'http://gems.github.com'
|
17
|
+
end
|
18
|
+
|
19
|
+
At your application root, run:
|
20
|
+
|
21
|
+
$ sudo rake gems:install
|
22
|
+
|
23
|
+
All the parameter parsing is application-defined, so you'll also need an initializer. <tt>search_party</tt> adds three class variables to <tt>ActionController::Request</tt>:
|
24
|
+
|
25
|
+
* <tt>search_parameter_models</tt>
|
26
|
+
* <tt>search_parameter_finders</tt>
|
27
|
+
* <tt>search_parameter_patterns</tt>
|
28
|
+
|
29
|
+
An example <tt>search_party</tt> initializer:
|
30
|
+
|
31
|
+
ActionController::Request.search_parameter_models = {
|
32
|
+
:user => 'User',
|
33
|
+
:friend => 'User',
|
34
|
+
:home_state => 'State'
|
35
|
+
}
|
36
|
+
|
37
|
+
ActionController::Request.search_parameter_finders = {
|
38
|
+
:user => :find_by_login,
|
39
|
+
:friend => :find_by_login,
|
40
|
+
:home_state => :find_by_abbreviation
|
41
|
+
}
|
42
|
+
|
43
|
+
ActionController::Request.search_parameter_patterns = [
|
44
|
+
[/^\d+$/, lambda{|m| m.to_s.to_i }],
|
45
|
+
[/^\d+\.\d+$/, lambda{|m| m.to_s.to_f }],
|
46
|
+
[/^(?:true|t|yes|y|on)$/i, true],
|
47
|
+
[/^(?:false|f|no|n|off)$/i, false],
|
48
|
+
[/^\s+$/, nil]
|
49
|
+
]
|
6
50
|
|
7
51
|
== Example
|
8
52
|
|
9
|
-
|
53
|
+
With <tt>search_party</tt>, a <tt>search_params</tt> hash is added to the controller instance in addition to the normal (and boring) <tt>params</tt>. The new <tt>search_params</tt> are available in the view as well, just like <tt>params</tt>, and are also accessible in the <tt>Request</tt> object via <tt>search_parameters</tt>. The <tt>search_params</tt> are simply the query parameters from a given request, run through the initializer configuration to silently make more sense out of what's being really being asked for.
|
54
|
+
|
55
|
+
For instance, the following <tt>params</tt>:
|
56
|
+
|
57
|
+
{"friend" => "laserlemon", "home_state" => "WA"}
|
58
|
+
|
59
|
+
would product the following <tt>search_params</tt>:
|
60
|
+
|
61
|
+
{"friend" => #<User id: 1, first_name: "Steve", last_name: "Richert", login: "laserlemon">, "home_state" => #<State id: 48, name: "Washington", abbreviation: "WA">}
|
62
|
+
|
63
|
+
using User.find_by_login and State.find_by_abbreviation. With the <tt>search_parameter_patterns</tt> variable, non-model query parameters are also cleaned up:
|
64
|
+
|
65
|
+
{"price" => "12.50", "negotiable" => "no"}
|
66
|
+
|
67
|
+
produces:
|
68
|
+
|
69
|
+
{"price" => 12.5, "negotiable" => false}
|
10
70
|
|
11
71
|
== Tips
|
12
72
|
|
13
|
-
|
73
|
+
* <tt>search_parameter_patterns</tt> are evaluated in the order they're defined.
|
74
|
+
* Lambdas may be used as the value corresponding to a pattern, with the MatchData object being passed as a single argument.
|
75
|
+
* <tt>search_party</tt> will parse the query parameters recursively in the case of multidimensional parameters.
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('search_party', '0.
|
5
|
+
Echoe.new('search_party', '0.4.0') do |g|
|
6
6
|
g.description = %(Parse your query parameters into more meaningful values)
|
7
7
|
g.url = 'http://github.com/laserlemon/search_party'
|
8
8
|
g.author = 'Steve Richert'
|
data/VERSION.yml
CHANGED
data/lib/search_party.rb
CHANGED
@@ -24,7 +24,7 @@ module LaserLemon
|
|
24
24
|
parsed_value = case value
|
25
25
|
when Hash then parse_hash(value)
|
26
26
|
when Array then value.map{|v| parse_value(v) }
|
27
|
-
else parse_value(
|
27
|
+
else parse_value(value)
|
28
28
|
end
|
29
29
|
new_value = if model_name = self.class.search_parameter_models[key]
|
30
30
|
model = model_name.to_s.classify.constantize
|
data/search_party.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{search_party}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.4.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Steve Richert"]
|
9
|
-
s.date = %q{2009-07-
|
9
|
+
s.date = %q{2009-07-31}
|
10
10
|
s.description = %q{Parse your query parameters into more meaningful values}
|
11
11
|
s.email = %q{steve@laserlemon.com}
|
12
12
|
s.extra_rdoc_files = ["lib/search_party.rb", "README.rdoc"]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: laserlemon-search_party
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Richert
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|