public
Description: An easy way to add common finders to your Rails controllers.
Homepage: http://matthewbass.com/2008/08/08/finder_filter-gem-released/
Clone URL: git://github.com/pelargir/finder_filter.git
finder_filter / README
ad77ae7a » Matthew Bass 2008-08-08 initial import 1 = Synthesis
2
3 An easy way to add common finders to your Rails controllers.
4
5 == Installation
6
04e8aa49 » Matthew Bass 2008-08-14 updated installation instru... 7 Install the gem directly:
8
38334c12 » Matthew Bass 2008-11-08 updated readme 9 sudo gem install pelargir-finder_filter --source=http://gems.github.com
04e8aa49 » Matthew Bass 2008-08-14 updated installation instru... 10
11 Or install the gem in your Rails project:
12
38334c12 » Matthew Bass 2008-11-08 updated readme 13 script/plugin install git://github.com/pelargir/finder_filter.git
04e8aa49 » Matthew Bass 2008-08-14 updated installation instru... 14
15 Or clone the project:
ad77ae7a » Matthew Bass 2008-08-08 initial import 16
04e8aa49 » Matthew Bass 2008-08-14 updated installation instru... 17 git clone git://github.com/pelargir/finder_filter.git
676afe68 » Matthew Bass 2008-08-10 updated gemspec and README 18
ad77ae7a » Matthew Bass 2008-08-08 initial import 19 == Usage
20
77e1cf04 » Matthew Bass 2008-08-08 added usage doc 21 finder_filter is intended to replace one-off before filters that you might
22 commonly write in your Rails controllers. For example:
23
24 class UsersController < ActionController::Base
25 before_filter :find_user, :only => [:show, :edit]
26
4541f064 » Matthew Bass 2008-08-08 tweaked docs 27 def show
28 # do something with @user
29 end
30
31 def edit
32 # do something with @user
33 end
77e1cf04 » Matthew Bass 2008-08-08 added usage doc 34
35 def find_user
36 @user = User.find(params[:id)
37 end
38 end
39
40 finder_filter reduces this pattern to a single line:
41
42 class UsersController < ActionController::Base
a3e074a7 » codebrulee 2008-08-09 made specifying the model o... 43 finder_filter :only => [:show, :edit]
44
45 def show; end
46 def edit; end
47 end
48
49 Or, if you want to specify the model to find:
50
51 class UsersController < ActionController::Base
1bca5ad7 » Matthew Bass 2008-08-15 added support for nested re... 52 finder_filter :person, :only => [:show, :edit]
77e1cf04 » Matthew Bass 2008-08-08 added usage doc 53
54 def show; end
55 def edit; end
56 end
57
58 To find based on a column other than ID:
59
60 finder_filter :user, :by => :name
335da67e » Matthew Bass 2008-08-08 added dependencies 61 # equivalent to:
62 # @user = User.find_by_name(params[:id])
77e1cf04 » Matthew Bass 2008-08-08 added usage doc 63
64 To find based on a param other than ID:
65
66 finder_filter :user, :param => :permalink
335da67e » Matthew Bass 2008-08-08 added dependencies 67 # equivalent to:
68 # @user = User.find(params[:permalink])
77e1cf04 » Matthew Bass 2008-08-08 added usage doc 69
8039c2ad » Matthew Bass 2008-08-14 updated README 70 You can specify that prepend_before_filter is used:
69c34407 » Steven Mohapi-Banks 2008-08-14 Updated README 71
72 finder_filter :user, :only => [:show, :edit], :prepend => true
73 # generates:
74 # prepend_before_filter :find_user, :only => [:show, :edit]
75
77e1cf04 » Matthew Bass 2008-08-08 added usage doc 76 The standard Rails :only and :except options can also be used:
77
78 before_filter :find_user, :only => [:show, :edit]
79 before_filter :find_user, :except => [:index]
ad77ae7a » Matthew Bass 2008-08-08 initial import 80
1bca5ad7 » Matthew Bass 2008-08-15 added support for nested re... 81 == Resource Nesting
82
83 If your controller is a nested resource, you might want the find
84 to be performed on the parent model. For example:
85
86 class PostsController < ActionController::Base
87 before_filter :find_post
88
89 def find_post
90 @topic = Topic.find(params[:topic_id])
91 @post = @topic.posts.find(params[:id])
92 end
93 end
94
95 This can be easily handled using finder_filter:
96
97 finder_filter :nested => :topic
98
a50fa6b9 » ffmike 2008-09-06 Make supplying parent id op... 99 Nested resources will only do a find on the parent model if the parent id is supplied.
100 This allows you to handle routing setups like this:
101
102 map.resources :posts
103 map.resources :topics do |topic|
104 topic.resources :posts
105 end
106
107 With this setup, both /posts/1 and /topics/1/posts/2 will be valid URLs, and will do the
108 right thing if you include
109
110 finder_filter :nested => :topic
111
112 in your Posts controller.
113
fb9e16ce » ffmike 2008-08-27 Added automatic from_param ... 114 == from_param
115
116 If you have Michael Bleigh's from_param (http://github.com/mbleigh/from_param/tree/master)
117 installed, finder_filter will work transparently with it. This gives you the dual benefit
118 of SEO-friendly URLs and DRY controller code.
119
335da67e » Matthew Bass 2008-08-08 added dependencies 120 == Tests
121
97551021 » Matthew Bass 2008-10-30 fixed tests and added sqlit... 122 To run the tests, you must have the mocha, test-spec, and sqlite3 gems installed.
335da67e » Matthew Bass 2008-08-08 added dependencies 123
124 rake test
125
126 == Dependencies
127
128 actionpack > 2.0.1
ad77ae7a » Matthew Bass 2008-08-08 initial import 129
8039c2ad » Matthew Bass 2008-08-14 updated README 130 == Author
131
132 Matthew Bass
133 email: pelargir at gmail dot com
134 blog: http://matthewbass.com
135
ad77ae7a » Matthew Bass 2008-08-08 initial import 136 == Contributors
137
8039c2ad » Matthew Bass 2008-08-14 updated README 138 Kevin Smith, Steve Mohapi-Banks