controller_resources 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.ruby-version +1 -1
- data/.travis.yml +2 -5
- data/README.md +76 -41
- data/Rakefile +4 -5
- data/bin/cc-tddium-post-worker +6 -5
- data/bin/coderay +6 -5
- data/bin/erubis +6 -5
- data/bin/htmldiff +6 -5
- data/bin/ldiff +6 -5
- data/bin/nokogiri +6 -5
- data/bin/pry +6 -5
- data/bin/rackup +6 -5
- data/bin/rails +6 -5
- data/bin/rake +6 -5
- data/bin/rspec +6 -5
- data/bin/rubocop +6 -5
- data/bin/ruby-parse +6 -5
- data/bin/ruby-rewrite +6 -5
- data/bin/sprockets +6 -5
- data/bin/thor +6 -5
- data/bin/yard +6 -5
- data/bin/yardoc +6 -5
- data/bin/yri +6 -5
- data/controller_resources.gemspec +1 -3
- data/lib/controller_resources.rb +120 -10
- data/lib/controller_resources/version.rb +1 -1
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/controllers/posts_controller.rb +16 -9
- data/spec/dummy/app/views/posts/_form.html.erb +1 -1
- data/spec/dummy/app/views/posts/edit.html.erb +1 -1
- data/spec/dummy/app/views/posts/index.html.erb +2 -2
- data/spec/dummy/app/views/posts/show.html.erb +3 -3
- data/spec/dummy/db/schema.rb +0 -1
- data/spec/dummy/db/seeds.rb +2 -2
- data/spec/features/posts_spec.rb +13 -6
- data/spec/lib/controller_resources_spec.rb +36 -0
- data/spec/spec_helper.rb +1 -15
- metadata +8 -28
- data/lib/controller_resources/engine.rb +0 -9
- data/lib/controller_resources/extension.rb +0 -147
- data/lib/controller_resources/not_defined_error.rb +0 -12
- data/spec/lib/controller_resources/extension_spec.rb +0 -46
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDliMjAxNWIzOGY5NjE2NzZmNTg3ZDRjMGEwZDdmOTA3NWZjOTM3Mw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzY5YWZkY2YwMDQxNjc2NjA4MjlmMjNkMmRmZDBhNzI0NzZmZGM2YQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZTYxMmUxYjZiODFmMmRkNTRlNmM0YTc3ZGNmNmQ3MjhjNjA0NjE1NzYzNTI1
|
10
|
+
MWU2MWY1MDE0ZGFhNzkyMGY0YWQ2NDZkYjg4MDYwNDExMTZjODQxMjZkYTlh
|
11
|
+
Zjc1ZDFiZjRhOGY0NTMzZTAyNjY2MTRhYmMxNjVhMTJkMzBlMTE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OWJmYzY1MDQ5ODgwY2RlYTBiNWQ2N2Y3YTk0YTFjOTRhNjI3YjhkMTM4OWQ3
|
14
|
+
MjdkM2Q5NjA3OWRjODZlNTUxYTZjYTRlMDQ1ZDIwNGVjZmY2ZDkyYzJmMDkx
|
15
|
+
YmUwMmIzYmY4M2UxMTRiYzNmNjRkOTBhOGRjYjUyNGI1NjRkMDg=
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.3.1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -6,35 +6,26 @@
|
|
6
6
|
[![Inline docs](http://inch-ci.org/github/tubbo/controller_resources.svg?branch=master)](http://inch-ci.org/github/tubbo/controller_resources)
|
7
7
|
|
8
8
|
A Rails engine providing a common DSL for fetching model resources in
|
9
|
-
the controller and view layers.
|
10
|
-
[DecentExposure][de], [StrongParameters][sp] and assumes an
|
11
|
-
ActiveRecord-like DSL for querying model objects.
|
12
|
-
ControllerResources does not assume any part of your stack, instead
|
13
|
-
providing generic tools and extensions to ActionController which allow
|
14
|
-
you to use the fetched resources however you want.
|
9
|
+
the controller and view layers.
|
15
10
|
|
16
11
|
```ruby
|
17
|
-
|
18
|
-
|
12
|
+
class PostsController < ApplicationController
|
13
|
+
resource :post
|
14
|
+
respond_to :html
|
15
|
+
|
16
|
+
def index
|
17
|
+
respond_with @posts
|
18
|
+
end
|
19
19
|
end
|
20
20
|
```
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
we're just using the `expose` macro to set up these resources, and using
|
25
|
-
the `resource` macro to populate what we expose and additionally what
|
26
|
-
parameters to pass through.
|
22
|
+
You can also specify an ancestor, which is used to look up the given
|
23
|
+
resource.
|
27
24
|
|
28
|
-
You can establish DecentExposure configuration with the `resource` block
|
29
|
-
by calling methods which do not exist on the Resource. All of these
|
30
|
-
methods are passed down to DecentExposure:
|
31
25
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
permit :body, :user_id
|
36
|
-
end
|
37
|
-
```
|
26
|
+
In this example, `@comment` is being looked up using
|
27
|
+
`@post.comments.find` rather than the default `Comment.find`.
|
28
|
+
|
38
29
|
|
39
30
|
## Installation
|
40
31
|
|
@@ -50,38 +41,54 @@ And run
|
|
50
41
|
$ bundle install
|
51
42
|
```
|
52
43
|
|
44
|
+
Then, include the module in your base controller class:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class ApplicationController < ActionController::Base
|
48
|
+
include ControllerResources
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
53
52
|
## Usage
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
Using the `resource` macro, you can define the name of your resource
|
55
|
+
which will be used to derive instance variable names and class names for
|
56
|
+
your models. ControllerResources assumes that your instance variables
|
57
|
+
are named conventionally, and assumes that you follow Rails best
|
58
|
+
practices when assigning instance variables in the controller.
|
58
59
|
|
59
60
|
```ruby
|
60
|
-
class
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
class CommentsController < ApplicationController
|
62
|
+
before_action :find_post
|
63
|
+
resource :comment, ancestor: :post
|
64
|
+
respond_to :html
|
64
65
|
|
65
|
-
def
|
66
|
-
respond_with
|
66
|
+
def show
|
67
|
+
respond_with @comment
|
67
68
|
end
|
68
69
|
|
69
|
-
def
|
70
|
-
|
70
|
+
def new
|
71
|
+
@comment = @post.comments.build
|
71
72
|
end
|
72
73
|
|
73
74
|
def create
|
74
|
-
|
75
|
-
|
75
|
+
authorize @comment
|
76
|
+
@comment = @post.comments.create permitted_params(Comment)
|
77
|
+
respond_with @comment
|
78
|
+
end
|
79
|
+
|
80
|
+
def update
|
81
|
+
authorize @comment
|
82
|
+
@comment.update permitted_params(Comment)
|
83
|
+
respond_with @comment
|
76
84
|
end
|
77
|
-
end
|
78
|
-
```
|
79
85
|
|
80
|
-
|
81
|
-
access the model objects passed down into the template:
|
86
|
+
private
|
82
87
|
|
83
|
-
|
84
|
-
|
88
|
+
def find_post
|
89
|
+
@post = Post.find params[:post_id]
|
90
|
+
end
|
91
|
+
end
|
85
92
|
```
|
86
93
|
|
87
94
|
### Meta-Programming Capabilities
|
@@ -95,6 +102,34 @@ your view as well as the controller.
|
|
95
102
|
|
96
103
|
For more, consult the [RDoc Documentation][rdoc]
|
97
104
|
|
105
|
+
### Customization
|
106
|
+
|
107
|
+
As said before, the `model` and `collection` methods are exposed for you
|
108
|
+
in the controller, and are what is used as the values for the instance
|
109
|
+
variables set in `:find_resource`. You can override these methods in
|
110
|
+
your base controller class to perform authorization or decoration logic
|
111
|
+
in a consistent manner, like so:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
class ApplicationController < ActionController::Base
|
115
|
+
include ControllerResources
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def model
|
120
|
+
super.tap do |record|
|
121
|
+
authorize record
|
122
|
+
end.decorate
|
123
|
+
end
|
124
|
+
|
125
|
+
def collection
|
126
|
+
super.tap do |records|
|
127
|
+
policy_scope records
|
128
|
+
end.decorate
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
98
133
|
## Contributing
|
99
134
|
|
100
135
|
Contributions to `ControllerResources` may be made using GitHub pull
|
data/Rakefile
CHANGED
@@ -6,10 +6,9 @@ require 'controller_resources/version'
|
|
6
6
|
require 'yard'
|
7
7
|
require 'travis/release/task'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
9
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
10
|
+
load 'rails/tasks/engine.rake'
|
11
|
+
load 'rails/tasks/statistics.rake'
|
13
12
|
|
14
13
|
# Run RSpec code examples
|
15
14
|
RSpec::Core::RakeTask.new :test
|
@@ -27,7 +26,7 @@ YARD::Rake::YardocTask.new :doc
|
|
27
26
|
Travis::Release::Task.new
|
28
27
|
|
29
28
|
# CI task
|
30
|
-
task default: %i(
|
29
|
+
task default: %i(app:db:setup test build)
|
31
30
|
|
32
31
|
namespace :ci do
|
33
32
|
desc "Rename gem package so it can be released with GitHub."
|
data/bin/cc-tddium-post-worker
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("codeclimate-test-reporter", "cc-tddium-post-worker")
|
data/bin/coderay
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("coderay", "coderay")
|
data/bin/erubis
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("erubis", "erubis")
|
data/bin/htmldiff
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("diff-lcs", "htmldiff")
|
data/bin/ldiff
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("diff-lcs", "ldiff")
|
data/bin/nokogiri
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("nokogiri", "nokogiri")
|
data/bin/pry
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("pry", "pry")
|
data/bin/rackup
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("rack", "rackup")
|
data/bin/rails
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("railties", "rails")
|
data/bin/rake
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
#
|
3
4
|
# This file was generated by Bundler.
|
4
5
|
#
|
@@ -6,11 +7,11 @@
|
|
6
7
|
# this file is here to facilitate running it.
|
7
8
|
#
|
8
9
|
|
9
|
-
require
|
10
|
-
ENV[
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
11
12
|
Pathname.new(__FILE__).realpath)
|
12
13
|
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
15
16
|
|
16
|
-
load Gem.bin_path(
|
17
|
+
load Gem.bin_path("rake", "rake")
|