declarative-find 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,16 +1,16 @@
1
- Declarative Find for Rails
2
- =======================
1
+ Declarative Find
2
+ ================
3
3
 
4
- This Rails plugin makes a `find` method available at the class level in `ApplicationController` which takes the name of an ActiveRecord model. Using this method creates a before filter which finds an ActiveRecord instance and assigns it to an appropriately named instance variable in the controller. The name of the model passed to `find` is used for both the key in `params` assumed to contain the ID of the record to find as well as the name of the instance variable to assign it to. An 404 HTTP code will be returned and the corresponding error page rendered for non-existent records via the [`http-error` gem](http://codyrobbins.com/software/http-error).
4
+ This Rails plugin makes a `find` method available at the class level in `ApplicationController` which takes the name of an ActiveRecord model. Using this method creates a before filter which finds an ActiveRecord instance and assigns it to an appropriately named instance variable in the controller. The name of the model passed to `find` is used for both the key in `params` assumed to contain the ID of the record to find as well as the name of the instance variable to assign it to. This behavior can be customized any which way via options. An 404 HTTP code will be returned and the corresponding error page rendered for non-existent records via the [`http-error` gem](http://codyrobbins.com/software/http-error).
5
5
 
6
6
  Full documentation is at [RubyDoc.info](http://rubydoc.info/gems/declarative-find).
7
7
 
8
8
  Examples
9
9
  --------
10
10
 
11
- The following examples assume the request for the `delete` action has an `:id` or `:user` param passed to it with the ID of the user to delete.
11
+ ### Straight-forward
12
12
 
13
- ### All actions
13
+ The following example assumes the request for the `delete` action has an `:id` or `:user` param passed to it with the ID of the user to delete.
14
14
 
15
15
  class UserController < ApplicationController
16
16
  find(:user)
@@ -20,7 +20,45 @@ The following examples assume the request for the `delete` action has an `:id` o
20
20
  end
21
21
  end
22
22
 
23
- ### Options
23
+ ### Different parameter name
24
+
25
+ The following example assumes the request for the `delete` action has a `:user_id` param passed to it with the ID of the user to delete.
26
+
27
+ class UserController < ApplicationController
28
+ find(:user, :param => :user_id)
29
+
30
+ def delete
31
+ @user.destroy
32
+ end
33
+ end
34
+
35
+ ### Different instance variable name
36
+
37
+ class UserController < ApplicationController
38
+ find(:user, :variable => :user_to_delete)
39
+
40
+ def delete
41
+ @user_to_delete.destroy
42
+ end
43
+ end
44
+
45
+ ### Custom lookup
46
+
47
+ class UserController < ApplicationController
48
+ find(:user)
49
+
50
+ def delete
51
+ @user.destroy
52
+ end
53
+
54
+ protected
55
+
56
+ def find_user
57
+ User.find_by_email(params[:email])
58
+ end
59
+ end
60
+
61
+ ### Pass-through of options
24
62
 
25
63
  The find can be restricted to specific actions the same way a typical before filter can be—in fact, any options passed to `find` are simply passed through to `before_filter`.
26
64
 
@@ -39,6 +77,10 @@ The find can be restricted to specific actions the same way a typical before fil
39
77
  Colophon
40
78
  --------
41
79
 
80
+ ### See also
81
+
82
+ If you like this gem, you may also want to check out [Create New](http://codyrobbins.com/software/create-new), [Save Changes To](http://codyrobbins.com/software/save-changes-to), or [HTTP Error](http://codyrobbins.com/software/http-error).
83
+
42
84
  ### Tested with
43
85
 
44
86
  * Rails 3.0.5 — 20 May 2011
@@ -55,4 +97,5 @@ To send patches, please fork on GitHub and submit a pull request.
55
97
  © 2011 [Cody Robbins](http://codyrobbins.com/). See LICENSE for details.
56
98
 
57
99
  * [Homepage](http://codyrobbins.com/software/declarative-find)
100
+ * [My other gems](http://codyrobbins.com/software#gems)
58
101
  * [Follow me on Twitter](http://twitter.com/codyrobbins)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'declarative-find'
3
- s.version = '1.0'
3
+ s.version = '1.0.1'
4
4
  s.summary = 'Find records for Rails controller actions declaratively.'
5
5
  s.homepage = 'http://codyrobbins.com/software/declarative-find'
6
6
  s.author = 'Cody Robbins'
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
 
9
9
  s.post_install_message = '
10
10
  -------------------------------------------------------------
11
- Follow me on Twitter: http://twitter.com/codyrobbins
11
+ Follow me on Twitter! http://twitter.com/codyrobbins
12
12
  -------------------------------------------------------------
13
13
 
14
14
  '
@@ -6,15 +6,19 @@ module CodyRobbins
6
6
  # Creates a before filter on the controller that finds an ActiveRecord instance and assigns it to an instance variable.
7
7
  #
8
8
  # * You pass the name of the model to look up to the method.
9
- # * The key in `params` assumed to contain the ID of the record to find is either `:id` or, if `:id` is not present, then extrapolated from the model name. For example, if the model is `User` then this key would be `:user`. Preference is given to `:id` to prevent conflicts in situations such as edit actions, where a record is looked up but new values for its attributes are passed via a key in `params` sharing the name of the model. For example, if the model is `User` then `params[:user]` would contain the attributes for the user from the form, and `:id` would have to be used to specify the ID of the user in question.
10
- # * The instance variable that the record is assigned to will be named according to the model. For example, if the model is `User` then the instance variable will be `@user`.
9
+ # * By default the key in `params` assumed to contain the ID of the record to find is either `:id` or, if `:id` is not present, then extrapolated from the model name. For example, if the model is `User` then this key would be `:user`. Preference is given to `:id` to prevent conflicts in situations such as edit actions, where a record is looked up but new values for its attributes are passed via a key in `params` sharing the name of the model. For example, if the model is `User` then `params[:user]` would contain the attributes for the user from the form, and `:id` would have to be used to specify the ID of the user in question.
10
+ # * By default the instance variable that the record is assigned to will be named according to the model. For example, if the model is `User` then the instance variable will be `@user`.
11
+ # * This behavior can be customized any which way via different combinations of options.
11
12
  #
12
13
  # An 404 HTTP code will be returned and the corresponding error page rendered for non-existent records via the [`http-error` gem](http://codyrobbins.com/software/http-error).
13
14
  #
14
15
  # @param name [Symbol, String] The name of the model to look up.
15
- # @param filter_options [Hash] Options to pass through to the underlying `before_filter`. For example, you could limit the lookup to a specific action by using the `:only` option.
16
+ # @param options [Hash] Any options not specifically used by the plugin method are simply passed through to the underlying `before_filter`. For example, you could limit the lookup to a specific action by including the `:only` option.
17
+ # @option options [String, Symbol] :param The key used to look up the record’s ID in the `params` hash. It defaults to the name of the model. For example, if the model is `User` then this option will default to `:user`.
18
+ # @option options [String, Symbol] :variable The name of the instance variable to assign the looked up object to. It defaults to the name of the model. For example, if the model is `User` then this option will default to `:user`.
19
+ # @option options [String, Symbol, Proc] :using If this option is string or symbol, it represents the name of a method on the controller that will be invoked in order to perform the look up. If it’s a `Proc`, then the proc is invoked to perform the look up. In either case, the return value should be the object to assign to the instance variable. This option can be used when custom logic needs to be provided to find a record beyond simply looking up by its ID. This option defaults to `:find_` prepended to the name of the model. For example, if the model is `User` then this option will default to `:find_user`.
16
20
  #
17
- # @example The following examples assume the request for the `delete` action has an `:id` or `:user` param passed to it with the ID of the user to delete.
21
+ # @example Straight-forward lookup. This example assumes the request for the `delete` action has an `:id` or `:user` param passed to it containing the ID of the user to delete.
18
22
  # class UserController < ApplicationController
19
23
  # find(:user)
20
24
  #
@@ -22,7 +26,73 @@ module CodyRobbins
22
26
  # @user.destroy
23
27
  # end
24
28
  # end
25
- # @example Restricting the find to specific actions.
29
+ # @example Overriding the name of the parameter containing the ID of the model. This example assumes the request for the `delete` action has a `:user_id` param passed to it containing the ID of the user to delete.
30
+ # class UserController < ApplicationController
31
+ # find(:user, :param => :user_id)
32
+ #
33
+ # def delete
34
+ # @user.destroy
35
+ # end
36
+ # end
37
+ # @example Overriding the name of the instance variable used to store the record. This example assumes the request for the `delete` action has an `:id` or `:user` param passed to it containing the ID of the user to delete.
38
+ # class UserController < ApplicationController
39
+ # find(:user, :variable => :user_to_delete)
40
+ #
41
+ # def delete
42
+ # @user_to_delete.destroy
43
+ # end
44
+ # end
45
+ # @example Using a method to provide custom logic for the lookup of the model.
46
+ # class UserController < ApplicationController
47
+ # find(:user)
48
+ #
49
+ # def delete
50
+ # @user.destroy
51
+ # end
52
+ #
53
+ # protected
54
+ #
55
+ # def find_user
56
+ # User.find_by_email(params[:email])
57
+ # end
58
+ # end
59
+ # @example Using a custom-named method to provide the logic for the lookup of the model.
60
+ # class UserController < ApplicationController
61
+ # find(:user, :using => :find_user_by_email)
62
+ #
63
+ # def delete
64
+ # @user.destroy
65
+ # end
66
+ #
67
+ # protected
68
+ #
69
+ # def find_user_by_email
70
+ # User.find_by_email(params[:email])
71
+ # end
72
+ # end
73
+ # @example Using a `Proc` to provide custom logic for the lookup of the model.
74
+ # class UserController < ApplicationController
75
+ # find(:user, :using => proc { User.find_by_email(params[:email]) })
76
+ #
77
+ # def delete
78
+ # @user.destroy
79
+ # end
80
+ #
81
+ # protected
82
+ #
83
+ # def find_user_by_email
84
+ # User.find_by_email(params[:email])
85
+ # end
86
+ # end
87
+ # @example Using a block to provide custom logic for the lookup of the model.
88
+ # class UserController < ApplicationController
89
+ # find(:user) { User.find_by_email(params[:email]) }
90
+ #
91
+ # def delete
92
+ # @user.destroy
93
+ # end
94
+ # end
95
+ # @example Restricting the lookup to specific actions.
26
96
  # class UserController < ApplicationController
27
97
  # find(:user, :only => :delete)
28
98
  #
@@ -34,14 +104,30 @@ module CodyRobbins
34
104
  # render(:text => 'Something else.')
35
105
  # end
36
106
  # end
37
- def find(name, filter_options = {})
38
- before_filter(filter_options) do
39
- model = name.to_class
40
- id = params[:id] || params[name]
41
- object = model.find_by_id(id)
107
+ def find(name, options = {}, &block)
108
+ options.reverse_merge!(:param => name,
109
+ :variable => name,
110
+ :using => "find_#{name}")
111
+
112
+ model = name.to_class
113
+ param_name = options.delete(:param)
114
+ variable_name = options.delete(:variable)
115
+ lookup_method = options.delete(:using)
116
+
117
+ before_filter(options) do
118
+ object = if block_given?
119
+ yield
120
+ elsif lookup_method.is_a?(Proc)
121
+ lookup_method.call
122
+ elsif lookup_method.respond_to?(:to_s) && respond_to?(lookup_method.to_s)
123
+ send(lookup_method.to_s)
124
+ else
125
+ id = params[:id] || params[param_name]
126
+ model.find_by_id(id)
127
+ end
42
128
 
43
129
  if object
44
- set_instance_variable(name, object)
130
+ set_instance_variable(variable_name, object)
45
131
  else
46
132
  http_error(404)
47
133
  end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declarative-find
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: "1.0"
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
6
11
  platform: ruby
7
12
  authors:
8
13
  - Cody Robbins
@@ -10,7 +15,7 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-05-24 00:00:00 -04:00
18
+ date: 2011-07-18 00:00:00 -04:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
@@ -21,6 +26,9 @@ dependencies:
21
26
  requirements:
22
27
  - - ">="
23
28
  - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
24
32
  version: "0"
25
33
  type: :runtime
26
34
  version_requirements: *id001
@@ -32,6 +40,9 @@ dependencies:
32
40
  requirements:
33
41
  - - ">="
34
42
  - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
35
46
  version: "0"
36
47
  type: :runtime
37
48
  version_requirements: *id002
@@ -43,6 +54,9 @@ dependencies:
43
54
  requirements:
44
55
  - - ">="
45
56
  - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
46
60
  version: "0"
47
61
  type: :runtime
48
62
  version_requirements: *id003
@@ -67,10 +81,12 @@ has_rdoc: true
67
81
  homepage: http://codyrobbins.com/software/declarative-find
68
82
  licenses: []
69
83
 
70
- post_install_message: "\n\
71
- -------------------------------------------------------------\n\
72
- Follow me on Twitter: http://twitter.com/codyrobbins\n\
73
- -------------------------------------------------------------\n\n"
84
+ post_install_message: |+
85
+
86
+ -------------------------------------------------------------
87
+ Follow me on Twitter! http://twitter.com/codyrobbins
88
+ -------------------------------------------------------------
89
+
74
90
  rdoc_options: []
75
91
 
76
92
  require_paths:
@@ -80,17 +96,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
96
  requirements:
81
97
  - - ">="
82
98
  - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
83
102
  version: "0"
84
103
  required_rubygems_version: !ruby/object:Gem::Requirement
85
104
  none: false
86
105
  requirements:
87
106
  - - ">="
88
107
  - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
89
111
  version: "0"
90
112
  requirements: []
91
113
 
92
114
  rubyforge_project:
93
- rubygems_version: 1.6.2
115
+ rubygems_version: 1.3.7
94
116
  signing_key:
95
117
  specification_version: 3
96
118
  summary: Find records for Rails controller actions declaratively.