pelargir / finder_filter
- Source
- Commits
- Network (3)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
8039c2a
Matthew Bass (author)
Thu Aug 14 09:34:11 -0700 2008
finder_filter / README
| ad77ae7a » | Matthew Bass | 2008-08-08 | 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 | |||||
| 676afe68 » | Matthew Bass | 2008-08-10 | 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 | |||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 13 | == Usage | |
| 14 | |||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 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 | |||||
| 4541f064 » | Matthew Bass | 2008-08-08 | 21 | def show | |
| 22 | # do something with @user | ||||
| 23 | end | ||||
| 24 | |||||
| 25 | def edit | ||||
| 26 | # do something with @user | ||||
| 27 | end | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 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 | ||||
| a3e074a7 » | codebrulee | 2008-08-09 | 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 | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 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 | ||||
| 335da67e » | Matthew Bass | 2008-08-08 | 55 | # equivalent to: | |
| 56 | # @user = User.find_by_name(params[:id]) | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 57 | ||
| 58 | To find based on a param other than ID: | ||||
| 59 | |||||
| 60 | finder_filter :user, :param => :permalink | ||||
| 335da67e » | Matthew Bass | 2008-08-08 | 61 | # equivalent to: | |
| 62 | # @user = User.find(params[:permalink]) | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 63 | ||
| 8039c2ad » | Matthew Bass | 2008-08-14 | 64 | You can specify that prepend_before_filter is used: | |
| 69c34407 » | Steven Mohapi-Banks | 2008-08-14 | 65 | ||
| 66 | finder_filter :user, :only => [:show, :edit], :prepend => true | ||||
| 67 | # generates: | ||||
| 68 | # prepend_before_filter :find_user, :only => [:show, :edit] | ||||
| 69 | |||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 70 | The standard Rails :only and :except options can also be used: | |
| 71 | |||||
| 72 | before_filter :find_user, :only => [:show, :edit] | ||||
| 73 | before_filter :find_user, :except => [:index] | ||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 74 | ||
| 335da67e » | Matthew Bass | 2008-08-08 | 75 | == Tests | |
| 76 | |||||
| 20c2d766 » | Matthew Bass | 2008-08-13 | 77 | To run the tests, you must have the mocha and test-spec gems installed. | |
| 335da67e » | Matthew Bass | 2008-08-08 | 78 | ||
| 79 | rake test | ||||
| 80 | |||||
| 81 | == Dependencies | ||||
| 82 | |||||
| 83 | actionpack > 2.0.1 | ||||
| 84 | mocha > 0.5.6 | ||||
| 85 | test-spec > 0.4.0 | ||||
| 86 | |||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 87 | == Git | |
| 88 | |||||
| 89 | git://github.com/pelargir/finder_filter.git | ||||
| 90 | |||||
| 8039c2ad » | Matthew Bass | 2008-08-14 | 91 | == Author | |
| 92 | |||||
| 93 | Matthew Bass | ||||
| 94 | email: pelargir at gmail dot com | ||||
| 95 | blog: http://matthewbass.com | ||||
| 96 | |||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 97 | == Contributors | |
| 98 | |||||
| 8039c2ad » | Matthew Bass | 2008-08-14 | 99 | Kevin Smith, Steve Mohapi-Banks | |
