pelargir / finder_filter
- Source
- Commits
- Network (3)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
04e8aa4
Matthew Bass (author)
Thu Aug 14 18:36:12 -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 | |||||
| 04e8aa49 » | Matthew Bass | 2008-08-14 | 7 | Install the gem directly: | |
| 8 | |||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 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 | 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 | 18 | ||
| 04e8aa49 » | Matthew Bass | 2008-08-14 | 19 | git clone git://github.com/pelargir/finder_filter.git | |
| 676afe68 » | Matthew Bass | 2008-08-10 | 20 | ||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 21 | == Usage | |
| 22 | |||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 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 | 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 | 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 | 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 | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 54 | finder_filter :user, :only => [:show, :edit] | |
| 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 | 63 | # equivalent to: | |
| 64 | # @user = User.find_by_name(params[:id]) | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 65 | ||
| 66 | To find based on a param other than ID: | ||||
| 67 | |||||
| 68 | finder_filter :user, :param => :permalink | ||||
| 335da67e » | Matthew Bass | 2008-08-08 | 69 | # equivalent to: | |
| 70 | # @user = User.find(params[:permalink]) | ||||
| 77e1cf04 » | Matthew Bass | 2008-08-08 | 71 | ||
| 8039c2ad » | Matthew Bass | 2008-08-14 | 72 | You can specify that prepend_before_filter is used: | |
| 69c34407 » | Steven Mohapi-Banks | 2008-08-14 | 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 | 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 | 82 | ||
| 335da67e » | Matthew Bass | 2008-08-08 | 83 | == Tests | |
| 84 | |||||
| 20c2d766 » | Matthew Bass | 2008-08-13 | 85 | To run the tests, you must have the mocha and test-spec gems installed. | |
| 335da67e » | Matthew Bass | 2008-08-08 | 86 | ||
| 87 | rake test | ||||
| 88 | |||||
| 89 | == Dependencies | ||||
| 90 | |||||
| 91 | actionpack > 2.0.1 | ||||
| 92 | mocha > 0.5.6 | ||||
| 93 | test-spec > 0.4.0 | ||||
| 94 | |||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 95 | == Git | |
| 96 | |||||
| 97 | git://github.com/pelargir/finder_filter.git | ||||
| 98 | |||||
| 8039c2ad » | Matthew Bass | 2008-08-14 | 99 | == Author | |
| 100 | |||||
| 101 | Matthew Bass | ||||
| 102 | email: pelargir at gmail dot com | ||||
| 103 | blog: http://matthewbass.com | ||||
| 104 | |||||
| ad77ae7a » | Matthew Bass | 2008-08-08 | 105 | == Contributors | |
| 106 | |||||
| 8039c2ad » | Matthew Bass | 2008-08-14 | 107 | Kevin Smith, Steve Mohapi-Banks | |
