declarative-find 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .yardoc
2
+ doc
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --charset UTF-8 --markup markdown lib/**/*.rb - LICENSE
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ The MIT License
2
+ ===============
3
+
4
+ © 2011 Cody Robbins
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,58 @@
1
+ Declarative Find for Rails
2
+ =======================
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).
5
+
6
+ Full documentation is at [RubyDoc.info](http://rubydoc.info/gems/declarative-find).
7
+
8
+ Examples
9
+ --------
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.
12
+
13
+ ### All actions
14
+
15
+ class UserController < ApplicationController
16
+ find(:user)
17
+
18
+ def delete
19
+ @user.destroy
20
+ end
21
+ end
22
+
23
+ ### Options
24
+
25
+ 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
+
27
+ class UserController < ApplicationController
28
+ find(:user, :only => :delete)
29
+
30
+ def delete
31
+ @user.destroy
32
+ end
33
+
34
+ def something_else
35
+ render(:text => 'Something else.')
36
+ end
37
+ end
38
+
39
+ Colophon
40
+ --------
41
+
42
+ ### Tested with
43
+
44
+ * Rails 3.0.5 — 20 May 2011
45
+
46
+ ### Contributing
47
+
48
+ * [Source](https://github.com/codyrobbins/declarative-find)
49
+ * [Bug reports](https://github.com/codyrobbins/declarative-find/issues)
50
+
51
+ To send patches, please fork on GitHub and submit a pull request.
52
+
53
+ ### Credits
54
+
55
+ © 2011 [Cody Robbins](http://codyrobbins.com/). See LICENSE for details.
56
+
57
+ * [Homepage](http://codyrobbins.com/software/declarative-find)
58
+ * [Follow me on Twitter](http://twitter.com/codyrobbins)
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'declarative-find'
3
+ s.version = '1.0'
4
+ s.summary = 'Find records for Rails controller actions declaratively.'
5
+ s.homepage = 'http://codyrobbins.com/software/declarative-find'
6
+ s.author = 'Cody Robbins'
7
+ s.email = 'cody@codyrobbins.com'
8
+
9
+ s.post_install_message = '
10
+ -------------------------------------------------------------
11
+ Follow me on Twitter: http://twitter.com/codyrobbins
12
+ -------------------------------------------------------------
13
+
14
+ '
15
+
16
+ s.files = `git ls-files`.split
17
+
18
+ s.add_dependency('easier-instance-variable-access')
19
+ s.add_dependency('to-class')
20
+ s.add_dependency('http-error')
21
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: UTF-8
2
+
3
+ module CodyRobbins
4
+ module DeclarativeFind
5
+ module ClassMethods
6
+ # Creates a before filter on the controller that finds an ActiveRecord instance and assigns it to an instance variable.
7
+ #
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`.
11
+ #
12
+ # 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
+ # @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
+ #
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.
18
+ # class UserController < ApplicationController
19
+ # find(:user)
20
+ #
21
+ # def delete
22
+ # @user.destroy
23
+ # end
24
+ # end
25
+ # @example Restricting the find to specific actions.
26
+ # class UserController < ApplicationController
27
+ # find(:user, :only => :delete)
28
+ #
29
+ # def delete
30
+ # @user.destroy
31
+ # end
32
+ #
33
+ # def something_else
34
+ # render(:text => 'Something else.')
35
+ # end
36
+ # 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)
42
+
43
+ if object
44
+ set_instance_variable(name, object)
45
+ else
46
+ http_error(404)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ module CodyRobbins
2
+ module DeclarativeFind
3
+ class Railtie < Rails::Railtie
4
+ initializer('cody_robbins.declarative_find') do
5
+ ActiveSupport.on_load(:action_controller) do
6
+ extend(ClassMethods)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ require('cody_robbins/declarative_find/class_methods')
2
+ require('cody_robbins/declarative_find/railtie')
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: declarative-find
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "1.0"
6
+ platform: ruby
7
+ authors:
8
+ - Cody Robbins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-24 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: easier-instance-variable-access
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: to-class
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: http-error
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ description:
50
+ email: cody@codyrobbins.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - .yardopts
60
+ - LICENSE
61
+ - README.markdown
62
+ - declarative-find.gemspec
63
+ - lib/cody_robbins/declarative_find/class_methods.rb
64
+ - lib/cody_robbins/declarative_find/railtie.rb
65
+ - lib/declarative-find.rb
66
+ has_rdoc: true
67
+ homepage: http://codyrobbins.com/software/declarative-find
68
+ licenses: []
69
+
70
+ post_install_message: "\n\
71
+ -------------------------------------------------------------\n\
72
+ Follow me on Twitter: http://twitter.com/codyrobbins\n\
73
+ -------------------------------------------------------------\n\n"
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.6.2
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Find records for Rails controller actions declaratively.
97
+ test_files: []
98
+