search_party 0.4.1
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/.gitignore +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +75 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/search_party.rb +74 -0
- data/search_party.gemspec +51 -0
- data/test/search_party_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +66 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Steve Richert
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
= search_party
|
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
|
+
|
11
|
+
== Installation
|
12
|
+
|
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
|
+
]
|
50
|
+
|
51
|
+
== Example
|
52
|
+
|
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}
|
70
|
+
|
71
|
+
== Tips
|
72
|
+
|
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
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |g|
|
9
|
+
g.name = 'search_party'
|
10
|
+
g.summary = %(Parse your query parameters into more meaningful values)
|
11
|
+
g.description = %(Parse your query parameters into more meaningful values)
|
12
|
+
g.email = 'steve@laserlemon.com'
|
13
|
+
g.homepage = 'http://github.com/laserlemon/search_party'
|
14
|
+
g.authors = %w(laserlemon)
|
15
|
+
g.rubyforge_project = 'laser-lemon'
|
16
|
+
end
|
17
|
+
Jeweler::RubyforgeTasks.new do |r|
|
18
|
+
r.doc_task = 'rdoc'
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::TestTask.new do |t|
|
26
|
+
t.libs = %w(test)
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :test
|
31
|
+
|
32
|
+
Rake::RDocTask.new do |r|
|
33
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : nil
|
34
|
+
r.rdoc_dir = 'rdoc'
|
35
|
+
r.title = ['search_party', version].compact.join(' ')
|
36
|
+
r.rdoc_files.include('README*')
|
37
|
+
r.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.1
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'search_party'
|
data/lib/search_party.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
module LaserLemon
|
2
|
+
module SearchParty
|
3
|
+
module Request
|
4
|
+
def self.included(base)
|
5
|
+
base.cattr_accessor :search_parameter_models
|
6
|
+
base.search_parameter_models = {}
|
7
|
+
|
8
|
+
base.cattr_accessor :search_parameter_finders
|
9
|
+
base.search_parameter_finders = {}
|
10
|
+
base.search_parameter_finders.default = :find_by_id
|
11
|
+
|
12
|
+
base.cattr_accessor :search_parameter_patterns
|
13
|
+
base.search_parameter_patterns = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def search_parameters
|
17
|
+
@search_parameters ||= parse_hash(query_parameters)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def parse_hash(hash)
|
22
|
+
hash.symbolize_keys.inject({}) do |new_hash, (key, value)|
|
23
|
+
parsed_value = case value
|
24
|
+
when Hash then parse_hash(value)
|
25
|
+
when Array then value.map{|v| parse_value(v) }
|
26
|
+
else parse_value(value)
|
27
|
+
end
|
28
|
+
new_value = if model_name = self.class.search_parameter_models[key]
|
29
|
+
model = model_name.to_s.classify.constantize
|
30
|
+
finder = self.class.search_parameter_finders[key]
|
31
|
+
new_values = [*parsed_value].map{|v| model.send(finder, v) }
|
32
|
+
parsed_value.is_a?(Array) ? new_values.dup : new_values.first
|
33
|
+
else
|
34
|
+
parsed_value
|
35
|
+
end
|
36
|
+
new_hash.update(key => new_value)
|
37
|
+
end.with_indifferent_access
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_value(value)
|
41
|
+
if p = self.class.search_parameter_patterns.detect{|r,v| r.match(value) }
|
42
|
+
(v = p.last).respond_to?(:call) ? v.call($~) : v
|
43
|
+
else
|
44
|
+
value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module Controller
|
50
|
+
def self.included(base)
|
51
|
+
base.attr_internal :search_params
|
52
|
+
|
53
|
+
base.class_eval do
|
54
|
+
alias_method_chain :assign_shortcuts, :search_party
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def assign_shortcuts_with_search_party(request, response)
|
59
|
+
@_search_params = request.search_parameters
|
60
|
+
assign_shortcuts_without_search_party(request, response)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
module View
|
65
|
+
def self.included(base)
|
66
|
+
base.delegate :search_params, :to => :controller
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
ActionController::Request.send(:include, LaserLemon::SearchParty::Request)
|
73
|
+
ActionController::Base.send(:include, LaserLemon::SearchParty::Controller)
|
74
|
+
ActionView::Base.send(:include, LaserLemon::SearchParty::View)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{search_party}
|
8
|
+
s.version = "0.4.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["laserlemon"]
|
12
|
+
s.date = %q{2009-10-07}
|
13
|
+
s.description = %q{Parse your query parameters into more meaningful values}
|
14
|
+
s.email = %q{steve@laserlemon.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"init.rb",
|
26
|
+
"lib/search_party.rb",
|
27
|
+
"search_party.gemspec",
|
28
|
+
"test/search_party_test.rb",
|
29
|
+
"test/test_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/laserlemon/search_party}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubyforge_project = %q{laser-lemon}
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{Parse your query parameters into more meaningful values}
|
37
|
+
s.test_files = [
|
38
|
+
"test/search_party_test.rb",
|
39
|
+
"test/test_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: search_party
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- laserlemon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-07 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Parse your query parameters into more meaningful values
|
17
|
+
email: steve@laserlemon.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- init.rb
|
32
|
+
- lib/search_party.rb
|
33
|
+
- search_party.gemspec
|
34
|
+
- test/search_party_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/laserlemon/search_party
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: laser-lemon
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Parse your query parameters into more meaningful values
|
64
|
+
test_files:
|
65
|
+
- test/search_party_test.rb
|
66
|
+
- test/test_helper.rb
|