ffmike / finder_filter forked from pelargir/finder_filter

An easy way to add common finders to your Rails controllers.

This URL has Read+Write access

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