finder_filter 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README +87 -0
- data/Rakefile +9 -0
- data/finder_filter.gemspec +23 -0
- data/lib/finder_filter.rb +18 -0
- data/test/finder_filter_test.rb +55 -0
- data/test/test_helper.rb +6 -0
- metadata +69 -0
data/CHANGELOG
ADDED
data/README
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
= Synthesis
|
2
|
+
|
3
|
+
An easy way to add common finders to your Rails controllers.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem sources -a http://gems.github.com (you only have to do this once)
|
8
|
+
sudo gem install pelargir-finder_filter
|
9
|
+
|
10
|
+
To use with a Rails 2.1 app, edit the environment.rb file and add an
|
11
|
+
entry for the gem to the Rails::Initializer config block.
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
finder_filter is intended to replace one-off before filters that you might
|
16
|
+
commonly write in your Rails controllers. For example:
|
17
|
+
|
18
|
+
class UsersController < ActionController::Base
|
19
|
+
before_filter :find_user, :only => [:show, :edit]
|
20
|
+
|
21
|
+
def show
|
22
|
+
# do something with @user
|
23
|
+
end
|
24
|
+
|
25
|
+
def edit
|
26
|
+
# do something with @user
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_user
|
30
|
+
@user = User.find(params[:id)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
finder_filter reduces this pattern to a single line:
|
35
|
+
|
36
|
+
class UsersController < ActionController::Base
|
37
|
+
finder_filter :only => [:show, :edit]
|
38
|
+
|
39
|
+
def show; end
|
40
|
+
def edit; end
|
41
|
+
end
|
42
|
+
|
43
|
+
Or, if you want to specify the model to find:
|
44
|
+
|
45
|
+
class UsersController < ActionController::Base
|
46
|
+
finder_filter :user, :only => [:show, :edit]
|
47
|
+
|
48
|
+
def show; end
|
49
|
+
def edit; end
|
50
|
+
end
|
51
|
+
|
52
|
+
To find based on a column other than ID:
|
53
|
+
|
54
|
+
finder_filter :user, :by => :name
|
55
|
+
# equivalent to:
|
56
|
+
# @user = User.find_by_name(params[:id])
|
57
|
+
|
58
|
+
To find based on a param other than ID:
|
59
|
+
|
60
|
+
finder_filter :user, :param => :permalink
|
61
|
+
# equivalent to:
|
62
|
+
# @user = User.find(params[:permalink])
|
63
|
+
|
64
|
+
The standard Rails :only and :except options can also be used:
|
65
|
+
|
66
|
+
before_filter :find_user, :only => [:show, :edit]
|
67
|
+
before_filter :find_user, :except => [:index]
|
68
|
+
|
69
|
+
== Tests
|
70
|
+
|
71
|
+
To run the tests, you must have the mocha and test-spec gems installed.
|
72
|
+
|
73
|
+
rake test
|
74
|
+
|
75
|
+
== Dependencies
|
76
|
+
|
77
|
+
actionpack > 2.0.1
|
78
|
+
mocha > 0.5.6
|
79
|
+
test-spec > 0.4.0
|
80
|
+
|
81
|
+
== Git
|
82
|
+
|
83
|
+
git://github.com/pelargir/finder_filter.git
|
84
|
+
|
85
|
+
== Contributors
|
86
|
+
|
87
|
+
Matthew Bass, Kevin Smith
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "finder_filter"
|
3
|
+
s.version = "0.5"
|
4
|
+
s.date = "2008-08-08"
|
5
|
+
s.summary = "An easy way to add common finders to your Rails controllers."
|
6
|
+
s.email = "pelargir@gmail.com"
|
7
|
+
s.homepage = "http://github.com/pelargir/finder_filter"
|
8
|
+
s.description = "An easy way to add common finders to your Rails controllers."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Matthew Bass"]
|
11
|
+
s.files = [
|
12
|
+
"CHANGELOG",
|
13
|
+
"README",
|
14
|
+
"Rakefile",
|
15
|
+
"finder_filter.gemspec",
|
16
|
+
"lib/finder_filter.rb",
|
17
|
+
"test/test_helper.rb",
|
18
|
+
"test/finder_filter_test.rb"
|
19
|
+
]
|
20
|
+
s.rdoc_options = ["--main", "README"]
|
21
|
+
s.extra_rdoc_files = ["README"]
|
22
|
+
s.add_dependency("actionpack", ["> 2.0.1"])
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module FinderFilter
|
2
|
+
def finder_filter(*args)
|
3
|
+
options = args.extract_options!
|
4
|
+
name = args.empty? ? controller_name.singularize : args.first
|
5
|
+
by = options.delete(:by)
|
6
|
+
param = options.delete(:param) || :id
|
7
|
+
|
8
|
+
before_filter "find_#{name}", options
|
9
|
+
|
10
|
+
define_method "find_#{name}" do
|
11
|
+
klass = name.to_s.classify.constantize
|
12
|
+
item = by ? klass.send("find_by_#{by}", params[param]) : klass.find(params[param])
|
13
|
+
instance_variable_set("@#{name}", item)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
ActionController::Base.extend FinderFilter
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class TestController < ActionController::Base; extend FinderFilter; end
|
4
|
+
class TestsController < ActionController::Base; extend FinderFilter; end
|
5
|
+
|
6
|
+
describe FinderFilter do
|
7
|
+
before { @controller = TestController }
|
8
|
+
|
9
|
+
it "should assign before filter when model specified" do
|
10
|
+
@controller.expects(:before_filter).with("find_foo", {})
|
11
|
+
@controller.finder_filter :foo
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should assign before filter when model not specified" do
|
15
|
+
@controller.expects(:before_filter).with("find_test", {})
|
16
|
+
@controller.finder_filter
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should assign before filter when model not specified and controller name is plural" do
|
20
|
+
controller = TestsController
|
21
|
+
controller.expects(:before_filter).with("find_test", {})
|
22
|
+
controller.finder_filter
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should assign before filter when options are specified" do
|
26
|
+
@controller.expects(:before_filter).with("find_test", {})
|
27
|
+
@controller.finder_filter :by => :permalink, :param => :name
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should assign before filter with options" do
|
31
|
+
@controller.expects(:before_filter).with("find_test", :only => [:show, :edit])
|
32
|
+
@controller.finder_filter :only => [:show, :edit]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should define method when model specified" do
|
36
|
+
@controller.expects(:define_method).with("find_foo")
|
37
|
+
@controller.finder_filter :foo
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should define method when model not specified" do
|
41
|
+
@controller.expects(:define_method).with("find_test")
|
42
|
+
@controller.finder_filter
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should define method when model not specified and controller name is plural" do
|
46
|
+
controller = TestsController
|
47
|
+
controller.expects(:define_method).with("find_test")
|
48
|
+
controller.finder_filter
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should define method when options are specified" do
|
52
|
+
@controller.expects(:define_method).with("find_test")
|
53
|
+
@controller.finder_filter :by => :permalink, :param => :name
|
54
|
+
end
|
55
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: finder_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.5"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Bass
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-08 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.1
|
24
|
+
version:
|
25
|
+
description: An easy way to add common finders to your Rails controllers.
|
26
|
+
email: pelargir@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README
|
33
|
+
files:
|
34
|
+
- CHANGELOG
|
35
|
+
- README
|
36
|
+
- Rakefile
|
37
|
+
- finder_filter.gemspec
|
38
|
+
- lib/finder_filter.rb
|
39
|
+
- test/test_helper.rb
|
40
|
+
- test/finder_filter_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/pelargir/finder_filter
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --main
|
46
|
+
- README
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: An easy way to add common finders to your Rails controllers.
|
68
|
+
test_files: []
|
69
|
+
|