controller_resources 0.0.6 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +13 -5
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/.travis.yml +2 -2
- data/README.md +54 -68
- data/Rakefile +17 -4
- data/bin/coderay +16 -0
- data/bin/pry +16 -0
- data/bin/yard +16 -0
- data/bin/yardoc +16 -0
- data/bin/yri +16 -0
- data/controller_resources.gemspec +16 -13
- data/lib/controller_resources/engine.rb +5 -4
- data/lib/controller_resources/not_defined_error.rb +12 -0
- data/lib/controller_resources/version.rb +2 -2
- data/lib/controller_resources.rb +141 -6
- data/spec/dummy/app/controllers/posts_controller.rb +6 -7
- data/spec/dummy/app/views/posts/_form.html.erb +4 -20
- data/spec/dummy/app/views/posts/edit.html.erb +1 -1
- data/spec/dummy/app/views/posts/index.html.erb +1 -3
- data/spec/dummy/app/views/posts/show.html.erb +3 -8
- data/spec/dummy/config/secrets.yml +0 -12
- data/spec/dummy/db/seeds.rb +2 -0
- data/spec/features/posts_spec.rb +48 -0
- data/spec/lib/controller_resources_spec.rb +44 -0
- data/spec/spec_helper.rb +97 -16
- data/spec/support/posts.rb +31 -0
- metadata +106 -55
- data/lib/controller_resources/extension.rb +0 -111
- data/lib/controller_resources/resource.rb +0 -99
- data/spec/dummy/test/controllers/posts_controller_test.rb +0 -49
- data/spec/dummy/test/fixtures/posts.yml +0 -11
- data/spec/dummy/test/models/post_test.rb +0 -7
- data/spec/lib/controller_resources/extension_spec.rb +0 -42
- data/spec/lib/controller_resources/resource_spec.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MmIwYmQ2N2FjMzJiMTdkMjkzY2EzNjhmMWZiOWUxMDhiYmRlYjJkNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGI1YzdhZjA0OGZkYzc4ZjM0NmRkNDg3MmEzNDI0M2JiNWY0MjE4MQ==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NmQyOWQyMzJmYzEzM2QwZGQ0NDBiNWE1ZTIyZjEyNDMwYzI5NDkwMjBhYTc5
|
10
|
+
ZjA4YWI2YzUzN2U4NjhiNGNlZmQxZDE3MjIzZDk2ZjQwYjUwYTk2ZTQ1MDEw
|
11
|
+
NzU1MWUwZjhmNzk2NDlkZTBkNTlhOTk1ZTZiZTdmZGY3YTIyMGY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjQxOGY3Nzc4YzJmZTBmNWYxZWQyZjNiYzBlZDAwNzg2NzYxMWEwNWYzMGI1
|
14
|
+
ZWYzYzY3ZjAzNWFjMGYwMWMxMWI4MTE3ZWViYmFkYWIwZjRjNDM2YzFjMDMz
|
15
|
+
MzAwNzY5ZTgxYTRhNmVhOGRmMmJkMzdhYmY4OGRlN2FlZWViYTA=
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.rubocop.yml
CHANGED
@@ -3,5 +3,15 @@ AllCops:
|
|
3
3
|
- spec/dummy/**/*
|
4
4
|
- vendor/**/*
|
5
5
|
- bin/**/*
|
6
|
+
- spec/support/**/*
|
7
|
+
|
6
8
|
Metrics/AbcSize:
|
7
9
|
Max: 32
|
10
|
+
|
11
|
+
Style/Documentation:
|
12
|
+
Exclude:
|
13
|
+
- lib/controller_resources/version.rb
|
14
|
+
- spec/**/*.rb
|
15
|
+
|
16
|
+
Style/StringLiterals:
|
17
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,10 +3,11 @@
|
|
3
3
|
[](https://travis-ci.org/tubbo/controller_resources)
|
4
4
|
[](https://codeclimate.com/github/tubbo/controller_resources)
|
5
5
|
[](https://codeclimate.com/github/tubbo/controller_resources)
|
6
|
+
[](http://inch-ci.org/github/tubbo/controller_resources)
|
6
7
|
|
7
8
|
A Rails engine providing a common DSL for fetching model resources in
|
8
9
|
the controller and view layers. It leverages
|
9
|
-
[DecentExposure][de],[StrongParameters][sp] and assumes an
|
10
|
+
[DecentExposure][de], [StrongParameters][sp] and assumes an
|
10
11
|
[ActiveRecord][ar]-like DSL for querying model objects.
|
11
12
|
ControllerResources does not assume any part of your stack, instead
|
12
13
|
providing generic tools and extensions to ActionController which allow
|
@@ -14,8 +15,7 @@ you to use the fetched resources however you want.
|
|
14
15
|
|
15
16
|
```ruby
|
16
17
|
resource :post do
|
17
|
-
|
18
|
-
modify :title, :category, :body, :is_published
|
18
|
+
permit :title, :category, :body, :is_published
|
19
19
|
end
|
20
20
|
```
|
21
21
|
|
@@ -31,10 +31,8 @@ methods are passed down to DecentExposure:
|
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
expose :post, param: :post_id
|
34
|
-
resource :comment do
|
35
|
-
|
36
|
-
search :body
|
37
|
-
modify :body, :user_id
|
34
|
+
resource :comment, ancestor: :post do
|
35
|
+
permit :body, :user_id
|
38
36
|
end
|
39
37
|
```
|
40
38
|
|
@@ -46,22 +44,10 @@ Add this line to your application's Gemfile:
|
|
46
44
|
gem 'controller_resources'
|
47
45
|
```
|
48
46
|
|
49
|
-
|
50
|
-
Responders:
|
47
|
+
And run
|
51
48
|
|
52
49
|
```bash
|
53
|
-
$
|
54
|
-
```
|
55
|
-
|
56
|
-
This will also insert Responders into your `ApplicationController`. If
|
57
|
-
you do not want this, be sure to include the following code in a base
|
58
|
-
controller somewhere...this is the best way for `ControllerResources` to
|
59
|
-
function:
|
60
|
-
|
61
|
-
```ruby
|
62
|
-
class ApplicationController < ActionController::Base
|
63
|
-
responders :flash, :http_cache
|
64
|
-
end
|
50
|
+
$ bundle install
|
65
51
|
```
|
66
52
|
|
67
53
|
## Usage
|
@@ -73,8 +59,7 @@ methods!
|
|
73
59
|
```ruby
|
74
60
|
class ItemsController < ApplicationController
|
75
61
|
resource :item do
|
76
|
-
|
77
|
-
modify :name, :user, :is_private
|
62
|
+
permit :name, :user, :is_private
|
78
63
|
end
|
79
64
|
|
80
65
|
def index
|
@@ -99,50 +84,16 @@ access the model objects passed down into the template:
|
|
99
84
|
<%= user.name %>
|
100
85
|
```
|
101
86
|
|
102
|
-
|
103
|
-
You can also use some given helpers for establishing authorization logic
|
104
|
-
in ApplicationController. Since `ControllerResources` is included by
|
105
|
-
default, you can use the given `current_resource` method to tell Pundit
|
106
|
-
(for example) which policy to use in authorization:
|
107
|
-
|
108
|
-
```ruby
|
109
|
-
class ApplicationController < ActionController::Base
|
110
|
-
before_action :authenticate_user!
|
111
|
-
before_action :authorize_user!
|
112
|
-
|
113
|
-
rescue_from Pundit::NotAuthorizedError, with: :forbidden
|
114
|
-
|
115
|
-
def forbidden
|
116
|
-
render 'forbidden', status: :forbidden
|
117
|
-
end
|
118
|
-
|
119
|
-
private
|
120
|
-
|
121
|
-
def authorize_user!
|
122
|
-
authorize current_user, current_resource, current_action
|
123
|
-
end
|
124
|
-
|
125
|
-
def current_action
|
126
|
-
:"#{action_name}?"
|
127
|
-
end
|
128
|
-
end
|
129
|
-
```
|
130
|
-
|
131
|
-
This `current_resource` method is populated by the given configured
|
132
|
-
Resource object, which will attempt to classify its `model_name` and
|
133
|
-
provide a class constant that can be used here. In the
|
134
|
-
`PostsController#show` action, for example, that call to `authorize` would be
|
135
|
-
partially expanded like this:
|
136
|
-
|
137
|
-
```ruby
|
138
|
-
authorize current_user, Post, :show?
|
139
|
-
```
|
87
|
+
### Meta-Programming Capabilities
|
140
88
|
|
141
|
-
|
89
|
+
Since `ControllerResources` is programatically defining exposure methods
|
90
|
+
for you, it also keeps references to the name given in the `resource`
|
91
|
+
block so you can infer what kind of object you are using in your view.
|
92
|
+
The `model_name`, `collection_name` and `model` / `collection` methods
|
93
|
+
are included as helpers along with the rest of the exposure methods in
|
94
|
+
your view as well as the controller.
|
142
95
|
|
143
|
-
|
144
|
-
authentication helpers, it does provide the necessary methods to aid
|
145
|
-
your own authorization and authentication frameworks in their job.
|
96
|
+
For more, consult the [RDoc Documentation][rdoc]
|
146
97
|
|
147
98
|
## Contributing
|
148
99
|
|
@@ -150,16 +101,51 @@ Contributions to `ControllerResources` may be made using GitHub pull
|
|
150
101
|
requests. You must include accompanying tests, and all tests must pass
|
151
102
|
for any contribution to be considered.
|
152
103
|
|
104
|
+
**NOTE:** Running tests requires that you have [PhantomJS][pjs]
|
105
|
+
installed.
|
106
|
+
|
153
107
|
To run tests:
|
154
108
|
|
155
109
|
```bash
|
156
|
-
$ rake test
|
110
|
+
$ bin/rake test
|
157
111
|
```
|
158
112
|
|
159
113
|
This will also use Rubocop to lint-check your code so it adheres to our
|
160
114
|
style guide.
|
161
115
|
|
116
|
+
## Releasing
|
117
|
+
|
118
|
+
All acceptance testing and final RubyGems.org releasing is performed
|
119
|
+
automatically by [Travis CI][ci]. When a new gem version needs to be
|
120
|
+
released, one with push access to the repo can update the version by
|
121
|
+
running the following Rake task, which will tag the latest version of
|
122
|
+
the gem and push all commits to GitHub, where it will be picked up by
|
123
|
+
Travis and auto-deployed to [RubyGems][rg]:
|
124
|
+
|
125
|
+
```bash
|
126
|
+
$ bin/rake release
|
127
|
+
```
|
128
|
+
|
129
|
+
## Compatibility
|
130
|
+
|
131
|
+
ControllerResources adheres to the [Semantic Versioning][sv]
|
132
|
+
standard for publishing new versions of the library. Bug fixes will be
|
133
|
+
pushed in patch updates, while new features that maintain compatibility
|
134
|
+
will be available in minor updates. Major updates are reserved for new
|
135
|
+
features that break existing compatibility.
|
136
|
+
|
137
|
+
### Ruby version support
|
138
|
+
|
139
|
+
This project will be tested against any version of MRI that is currently
|
140
|
+
being supported by the Ruby core team. It is not currently being tested
|
141
|
+
on JRuby or Rubinius. For more information on what versions of Ruby
|
142
|
+
we're testing, see [Travis CI][ci].
|
143
|
+
|
162
144
|
[de]: https://github.com/hashrocket/decent_exposure
|
163
|
-
[rp]: https://github.com/plataformatec/responders
|
164
145
|
[sp]: https://github.com/rails/strong_parameters
|
165
|
-
|
146
|
+
[ci]: https://travis-ci.org
|
147
|
+
[rg]: https://rubygems.org
|
148
|
+
[pjs]: http://phantomjs.org
|
149
|
+
[sv]: http://semver.org
|
150
|
+
[rdoc]: http://rubydoc.info/github/tubbo/controller_resources/master
|
151
|
+
[ar]: http://guides.rubyonrails.org/active_record_basics.html
|
data/Rakefile
CHANGED
@@ -5,12 +5,25 @@ require 'rubocop/rake_task'
|
|
5
5
|
|
6
6
|
desc 'Set up the test database for the dummy app'
|
7
7
|
task :db do
|
8
|
-
sh 'cd spec/dummy && bundle exec rake db:
|
8
|
+
sh 'cd spec/dummy && bundle exec rake db:setup'
|
9
9
|
end
|
10
10
|
|
11
|
-
RSpec
|
11
|
+
# Run RSpec code examples
|
12
|
+
RSpec::Core::RakeTask.new :test
|
12
13
|
|
14
|
+
# Run RuboCop lint checks
|
13
15
|
RuboCop::RakeTask.new :lint
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
# Clear out default Bundler release task
|
18
|
+
Rake::Task['release'].clear
|
19
|
+
|
20
|
+
desc "Release version to RubyGems via Travis-CI"
|
21
|
+
task release: %w(
|
22
|
+
build release:guard_clean release:source_control_push
|
23
|
+
) do
|
24
|
+
Bundler.ui.confirm 'Please wait for Travis-CI to build the gem'
|
25
|
+
Bundler.ui.confirm 'https://travis-ci.org/tubbo/controller_resources'
|
26
|
+
end
|
27
|
+
|
28
|
+
# CI task
|
29
|
+
task default: %i(lint db test build)
|
data/bin/coderay
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'coderay' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('coderay', 'coderay')
|
data/bin/pry
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'pry' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('pry', 'pry')
|
data/bin/yard
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'yard' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('yard', 'yard')
|
data/bin/yardoc
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'yardoc' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('yard', 'yardoc')
|
data/bin/yri
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'yri' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('yard', 'yri')
|
@@ -4,36 +4,39 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'controller_resources/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
7
|
+
spec.name = 'controller_resources'
|
8
|
+
spec.version = ControllerResources::VERSION
|
9
|
+
spec.authors = ['Tom Scott']
|
10
|
+
spec.email = ['tubbo@psychedeli.ca']
|
11
|
+
spec.summary = '
|
12
12
|
A controller DSL for Rails that allows you to easily and quickly
|
13
13
|
define both singular and collection model resources that can be
|
14
14
|
operated on within the controller.
|
15
15
|
'.strip
|
16
|
-
spec.description
|
16
|
+
spec.description = spec.summary + '
|
17
17
|
Attempts to DRY up most of the boilerplate code at the top of
|
18
18
|
each controller used to set up its state.
|
19
19
|
'.strip
|
20
|
-
spec.homepage
|
21
|
-
spec.license
|
20
|
+
spec.homepage = 'https://github.com/tubbo/controller_resources'
|
21
|
+
spec.license = 'MIT'
|
22
22
|
|
23
|
-
spec.files
|
24
|
-
spec.executables
|
25
|
-
spec.test_files
|
23
|
+
spec.files = `git ls-files -z`.split("\x0")
|
24
|
+
# spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.test_files = spec.files.grep(%r{^|spec/})
|
26
26
|
spec.require_paths = ['lib']
|
27
27
|
|
28
28
|
spec.add_development_dependency 'bundler'
|
29
29
|
spec.add_development_dependency 'rake'
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3'
|
31
|
+
spec.add_development_dependency 'rspec-rails'
|
31
32
|
spec.add_development_dependency 'pg'
|
32
|
-
spec.add_development_dependency 'generator_spec'
|
33
33
|
spec.add_development_dependency 'codeclimate-test-reporter'
|
34
34
|
spec.add_development_dependency 'rubocop'
|
35
|
+
spec.add_development_dependency 'capybara'
|
36
|
+
spec.add_development_dependency 'database_cleaner'
|
37
|
+
spec.add_development_dependency 'pry'
|
38
|
+
spec.add_development_dependency 'yard'
|
35
39
|
|
36
40
|
spec.add_dependency 'rails'
|
37
41
|
spec.add_dependency 'decent_exposure'
|
38
|
-
spec.add_dependency 'responders'
|
39
42
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module ControllerResources
|
2
|
-
#
|
2
|
+
# Extends +ActionController::Base+ in the host app to include the
|
3
|
+
# +ControllerResources+ mixin by default.
|
3
4
|
class Engine < ::Rails::Engine
|
4
5
|
isolate_namespace ControllerResources
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
include ControllerResources
|
7
|
+
ActiveSupport.on_load :action_controller do
|
8
|
+
ActionController::Base.class_eval do
|
9
|
+
include ControllerResources
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ControllerResources
|
2
|
+
# Thrown when a +resource+ has not been defined, yet the controller
|
3
|
+
# attempts to call the generated +edit_params+ method.
|
4
|
+
class NotDefinedError < RuntimeError
|
5
|
+
def initialize(*args)
|
6
|
+
super
|
7
|
+
@message = %(
|
8
|
+
Call to #edit_params failed: Resource not defined.
|
9
|
+
)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/controller_resources.rb
CHANGED
@@ -1,16 +1,151 @@
|
|
1
1
|
require 'active_support/all'
|
2
2
|
require 'decent_exposure'
|
3
|
-
require 'responders'
|
4
3
|
require 'controller_resources/version'
|
4
|
+
require 'controller_resources/engine'
|
5
|
+
require 'controller_resources/not_defined_error'
|
5
6
|
|
6
|
-
#
|
7
|
+
# The DSL mixin for ActionController that allows you to quickly define
|
7
8
|
# both singular and collection model resources that can be operated on
|
8
9
|
# within the controller. Attempts to DRY up most of the boilerplate code
|
9
10
|
# at the top of each controller used to set up its state.
|
11
|
+
#
|
12
|
+
# @author Tom Scott
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
#
|
16
|
+
# class PostsController < ApplicationController
|
17
|
+
# resource :post do
|
18
|
+
# permit :title, :body, :author_id
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# def index
|
22
|
+
# respond_with posts
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# def show
|
26
|
+
# respond_with post
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# def create
|
30
|
+
# post.save
|
31
|
+
# respond_with post
|
32
|
+
# end
|
33
|
+
#
|
10
34
|
module ControllerResources
|
11
|
-
extend ActiveSupport::
|
35
|
+
extend ActiveSupport::Concern
|
12
36
|
|
13
|
-
|
14
|
-
|
15
|
-
|
37
|
+
included do
|
38
|
+
# Given name of the model as supplied to the +resource+ directive.
|
39
|
+
#
|
40
|
+
# @attr_accessor [Symbol]
|
41
|
+
cattr_accessor :model_name
|
42
|
+
|
43
|
+
# Collection name computed by pluralizing the given model name.
|
44
|
+
#
|
45
|
+
# @attr_accessor [Symbol]
|
46
|
+
cattr_accessor :collection_name
|
47
|
+
|
48
|
+
# A collection of arguments supplied to +StrongParameters#permit+
|
49
|
+
# that is used in the +edit_params+ method to easily define
|
50
|
+
# attributes which need to be whitelisted for mass assignment.
|
51
|
+
#
|
52
|
+
# @attr_accessor [Array]
|
53
|
+
cattr_accessor :params_to_permit
|
54
|
+
|
55
|
+
# Configures +DecentExposure+ to use the +edit_params+ method
|
56
|
+
# defined by this module as the default attributes for mass
|
57
|
+
# assignment.
|
58
|
+
decent_configuration do
|
59
|
+
attributes :edit_params
|
60
|
+
end
|
61
|
+
|
62
|
+
helper_method :model, :collection
|
63
|
+
end
|
64
|
+
|
65
|
+
class_methods do
|
66
|
+
# Define resources based on the given name and decent exposure
|
67
|
+
# options. Uses +DecentExposure+ to define said resources and
|
68
|
+
# stores the generated names in the controller's configuration.
|
69
|
+
#
|
70
|
+
# If a block is given, one can also call additional DSL methods
|
71
|
+
# like +permit+.
|
72
|
+
#
|
73
|
+
# @param [Symbol] name - the name of the model exposure, which
|
74
|
+
# is pluralized to compute the name of the collection exposure and
|
75
|
+
# stored to +model_name+.
|
76
|
+
# @param [Hash] options - passed wholesale to both +expose+ methods,
|
77
|
+
# and useful for defining custom +DecentExposure+ options.
|
78
|
+
# @param &block - a block that is yielded after +expose+ methods are
|
79
|
+
# defined, and used primarily to set permittable params.
|
80
|
+
# @example
|
81
|
+
#
|
82
|
+
# expose :author
|
83
|
+
# resource :post, ancestor: :author
|
84
|
+
#
|
85
|
+
def resource(name, options = {})
|
86
|
+
self.model_name = name
|
87
|
+
self.collection_name = "#{name}".pluralize.to_sym
|
88
|
+
|
89
|
+
expose model_name, options
|
90
|
+
expose collection_name, options
|
91
|
+
|
92
|
+
yield if block_given?
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# Permit the given attributes in +StrongParameters+.
|
98
|
+
#
|
99
|
+
# @example
|
100
|
+
#
|
101
|
+
# expose :author
|
102
|
+
# resource :post, ancestor: :author do
|
103
|
+
# permit :title, :body
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
def permit(*permittable_params)
|
107
|
+
self.params_to_permit = permittable_params
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Reader method for the defined singular resource.
|
112
|
+
#
|
113
|
+
# @return [Object] the model object or +nil+ if no resource has been
|
114
|
+
# defined.
|
115
|
+
def model
|
116
|
+
return unless resource?
|
117
|
+
public_send model_name
|
118
|
+
end
|
119
|
+
|
120
|
+
# Reader method for the defined collection resource.
|
121
|
+
#
|
122
|
+
# @return [Object] the collection of models or +nil+ if no resource
|
123
|
+
# has been defined.
|
124
|
+
def collection
|
125
|
+
return unless resource?
|
126
|
+
public_send collection_name
|
127
|
+
end
|
128
|
+
|
129
|
+
# White-listed parameters for mass assignment as defined by
|
130
|
+
# +StrongParameters+. Throws a +ControllerResources::NotDefinedError+
|
131
|
+
# if no +resource+ block has been defined on this controller.
|
132
|
+
#
|
133
|
+
# @return [ActionController::Parameters] the +StrongParameters+ hash.
|
134
|
+
# @raise [ControllerResources::NotDefinedError] when no +resource+ has
|
135
|
+
# been defined on this controller.
|
136
|
+
def edit_params
|
137
|
+
fail NotDefinedError unless resource?
|
138
|
+
return params.require(model_name).permit! unless params_to_permit.present?
|
139
|
+
params.require(model_name).permit(*params_to_permit)
|
140
|
+
end
|
141
|
+
|
142
|
+
private
|
143
|
+
|
144
|
+
# Whether a +resource+ directive has been invoked on this controller.
|
145
|
+
#
|
146
|
+
# @private
|
147
|
+
# @return [Boolean]
|
148
|
+
def resource?
|
149
|
+
model_name.present?
|
150
|
+
end
|
16
151
|
end
|
@@ -1,17 +1,16 @@
|
|
1
1
|
class PostsController < ApplicationController
|
2
2
|
resource :post do
|
3
|
-
|
4
|
-
modify :title, :body
|
3
|
+
permit :title, :body
|
5
4
|
end
|
6
5
|
|
7
6
|
# GET /posts
|
8
7
|
def index
|
9
|
-
|
8
|
+
render :index
|
10
9
|
end
|
11
10
|
|
12
11
|
# GET /posts/1
|
13
12
|
def show
|
14
|
-
|
13
|
+
render :show
|
15
14
|
end
|
16
15
|
|
17
16
|
# GET /posts/new
|
@@ -27,18 +26,18 @@ class PostsController < ApplicationController
|
|
27
26
|
# POST /posts
|
28
27
|
def create
|
29
28
|
post.save
|
30
|
-
|
29
|
+
redirect_to post
|
31
30
|
end
|
32
31
|
|
33
32
|
# PATCH/PUT /posts/1
|
34
33
|
def update
|
35
34
|
post.update(edit_params)
|
36
|
-
|
35
|
+
redirect_to post
|
37
36
|
end
|
38
37
|
|
39
38
|
# DELETE /posts/1
|
40
39
|
def destroy
|
41
40
|
post.destroy
|
42
|
-
|
41
|
+
redirect_to posts_path
|
43
42
|
end
|
44
43
|
end
|