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