implicit_resource 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +20 -0
- data/README.md +78 -0
- data/Rakefile +3 -0
- data/lib/implicit_resource/railtie.rb +4 -0
- data/lib/implicit_resource/version.rb +3 -0
- data/lib/implicit_resource.rb +10 -41
- data/lib/tasks/implicit_resource_tasks.rake +4 -0
- metadata +56 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cd6ad0e6c6212045d4b6d3fb2eec3ca9c901d4624533f7ed9fa7f2feedc179b
|
4
|
+
data.tar.gz: b4efa30d7b1c6a7706be07ee362d0cfb600dbd4f53a3cfe44f12b42cc9f8bfc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4044df37cf13a185176605427545fee16359ffa68eb8d0b1f88e31355b815b3634fa0af163aa15f4c932f31e36beefe5c522af7582244b47649b30426c4a2292
|
7
|
+
data.tar.gz: 9af3ce0fe668ca9b748f744d759b2f3f47a306bf3cda310f24ddca4341b10084e502e9490d2d010bcdbbf44bfda64914239fa7cd78a5187956bfa380f0a8802a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
## ImplicitResource
|
2
|
+
|
3
|
+
In most cases you will have the same old code in your controllers, this gem allows some very basic assumptions while giving you the option to override the behavior. This allows your controllers to be limited to just the required code.
|
4
|
+
|
5
|
+
Assumptions this code makes:
|
6
|
+
1. The controller is named for the model. e.g. `ArticlesController` will assume `Article` model.
|
7
|
+
2. Standard restful actions: `index`, `show`, `new`, `create`, `edit`, `update`, `destroy`.
|
8
|
+
3. A method named `permitted_attributes` exists for `permitted_params`.
|
9
|
+
|
10
|
+
Here's an example of the bare minimum code required to get started:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
ArticlesController < ApplicationController
|
14
|
+
include ImplicitResource # Opt in to implicit behavior, or include it in ApplicationController.
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def permitted_params
|
19
|
+
params.require(:article).permit(:title, :body)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
## Installationc
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem "implicit_resource"
|
29
|
+
```
|
30
|
+
|
31
|
+
Add this line to your controller
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
include ImplicitResource
|
35
|
+
```
|
36
|
+
|
37
|
+
### Customization
|
38
|
+
|
39
|
+
ImplicitResource only targets basic controller functionality. Everything that `implicit_resource` does can be overriden in the controller.
|
40
|
+
|
41
|
+
#### Overriding resource methods
|
42
|
+
|
43
|
+
You can override the `collection` and `resource` methods to achieve more flexibility in how things are queried.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
ArticlesController < ApplicationController
|
47
|
+
include ImplicitResource
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def collection # Collection is a placeholder for any collection of objects to be returned
|
52
|
+
model_klass.includes(:comments).search(title: params[:title_search_terms])
|
53
|
+
end
|
54
|
+
|
55
|
+
def resource # Resource is a placeholder for any single object to be returned
|
56
|
+
collection.find_by(token_id: params[:token_id])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
#### Overriding the model class
|
62
|
+
|
63
|
+
You can override the `model_klass` method to allow for controller/model naming mismatch.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
ArticlesController < ApplicationController
|
67
|
+
include ImplicitResource
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def model_klass
|
72
|
+
Story
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
## License
|
78
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/implicit_resource.rb
CHANGED
@@ -1,21 +1,17 @@
|
|
1
|
+
require "implicit_resource/version"
|
2
|
+
require "implicit_resource/railtie"
|
3
|
+
require "responders"
|
4
|
+
require "has_scope"
|
5
|
+
|
1
6
|
module ImplicitResource
|
2
7
|
extend ActiveSupport::Concern
|
3
8
|
|
4
|
-
|
5
|
-
%i[index show destroy].each do |action|
|
6
|
-
define_method(action) do
|
7
|
-
response_object = (action == :index ? collection : act_on_resource)
|
8
|
-
respond_with(response_object)
|
9
|
-
end
|
10
|
-
end
|
9
|
+
ACTIONS = [:index, :show, :new, :create, :edit, :update, :destroy]
|
11
10
|
|
12
|
-
|
11
|
+
included do
|
12
|
+
ACTIONS.each do |action|
|
13
13
|
define_method(action) do
|
14
|
-
|
15
|
-
respond_with('', location: resource_location)
|
16
|
-
else
|
17
|
-
respond_with(act_on_resource)
|
18
|
-
end
|
14
|
+
respond_with(action == :index ? collection : act_on_resource)
|
19
15
|
end
|
20
16
|
end
|
21
17
|
|
@@ -25,10 +21,6 @@ module ImplicitResource
|
|
25
21
|
|
26
22
|
protected
|
27
23
|
|
28
|
-
def resource_location
|
29
|
-
(namespace << act_on_resource)
|
30
|
-
end
|
31
|
-
|
32
24
|
def namespace
|
33
25
|
@namespace ||= controller_path.split('/')[0..-2].map(&:to_sym)
|
34
26
|
end
|
@@ -38,7 +30,7 @@ module ImplicitResource
|
|
38
30
|
end
|
39
31
|
|
40
32
|
def collection
|
41
|
-
@collection ||=
|
33
|
+
@collection ||= apply_scopes(model_klass.all)
|
42
34
|
end
|
43
35
|
|
44
36
|
def resource
|
@@ -49,25 +41,7 @@ module ImplicitResource
|
|
49
41
|
end
|
50
42
|
end
|
51
43
|
|
52
|
-
# If the resource references user(s) go ahead and set with the current user
|
53
|
-
def assign_current_user
|
54
|
-
return unless current_user && resource.new_record?
|
55
|
-
resource.users << current_user if resource.respond_to?(:users=)
|
56
|
-
resource.user ||= current_user if resource.respond_to?(:user=)
|
57
|
-
end
|
58
|
-
|
59
|
-
# Assign some common associations if needed. This
|
60
|
-
# needs to be done before authorization
|
61
|
-
#
|
62
|
-
def bootstrap_resource
|
63
|
-
resource.tap do
|
64
|
-
assign_current_organization
|
65
|
-
assign_current_user
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
44
|
# Persist the passed in resource to the database
|
70
|
-
#
|
71
45
|
def persist_resource
|
72
46
|
return unless %w[create update].include?(action_name)
|
73
47
|
|
@@ -78,19 +52,14 @@ module ImplicitResource
|
|
78
52
|
end
|
79
53
|
|
80
54
|
# Delete the given resource from the database
|
81
|
-
#
|
82
55
|
def destroy_resource
|
83
56
|
resource.destroy! if action_name == 'destroy'
|
84
57
|
end
|
85
58
|
|
86
59
|
# Build a resource based on contextual info
|
87
|
-
#
|
88
60
|
def act_on_resource
|
89
61
|
@act_on_resource ||= resource.tap do
|
90
|
-
authorize(resource)
|
91
|
-
|
92
62
|
unless destroy_resource
|
93
|
-
bootstrap_resource
|
94
63
|
persist_resource
|
95
64
|
end
|
96
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: implicit_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Kelley
|
@@ -9,18 +9,69 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2024-03-29 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: responders
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: has_scope
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '7'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '7'
|
13
55
|
description: Rails Controller Boilerplate
|
14
|
-
email:
|
56
|
+
email:
|
57
|
+
- mike@codezombie.org
|
15
58
|
executables: []
|
16
59
|
extensions: []
|
17
60
|
extra_rdoc_files: []
|
18
61
|
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
19
65
|
- lib/implicit_resource.rb
|
20
|
-
|
66
|
+
- lib/implicit_resource/railtie.rb
|
67
|
+
- lib/implicit_resource/version.rb
|
68
|
+
- lib/tasks/implicit_resource_tasks.rake
|
69
|
+
homepage: https://github.com/codezomb/implicit_resource
|
21
70
|
licenses:
|
22
71
|
- MIT
|
23
|
-
metadata:
|
72
|
+
metadata:
|
73
|
+
source_code_uri: https://github.com/codezomb/implicit_resource
|
74
|
+
homepage_uri: https://github.com/codezomb/implicit_resource
|
24
75
|
post_install_message:
|
25
76
|
rdoc_options: []
|
26
77
|
require_paths:
|