resting_pug 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +218 -12
- data/Rakefile +3 -1
- data/lib/resting_pug/actions.rb +42 -36
- data/lib/resting_pug/base.rb +1 -0
- data/lib/resting_pug/chains.rb +50 -0
- data/lib/resting_pug/modificators.rb +31 -1
- data/lib/resting_pug/params.rb +114 -22
- data/lib/resting_pug/render.rb +81 -7
- data/lib/resting_pug/subject.rb +84 -0
- data/lib/resting_pug/version.rb +2 -1
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 927bf0f8f98d894ab8421feb896b4a64b44f35eb
|
4
|
+
data.tar.gz: aae8c437373e4c033ba0dc2490a71a6e9e1064f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fbce2102a15bb5ca3c3cbb74417cfb7fb2edeb16f1bc333067cb0e48a8a83be69fc7269e380d9207a357ea1b57f33f0bdba45ed1f0a7a3f4d8d5a48fc3249c7
|
7
|
+
data.tar.gz: 9fd83245c3efaad741b2b61cd6f43130a2c407d5e9a1746327ada9fc6b273f23a013c7e9ed196203cc0c42ab12a37c2d649e9119bf54756a93ace75cea2a8c32
|
data/README.md
CHANGED
@@ -1,15 +1,79 @@
|
|
1
1
|
|
2
2
|
<h1 align="center">
|
3
3
|
<br>
|
4
|
-
<a href="https://korolvs.github.com/resting_pug"><img src="https://korolvs.github.com/resting_pug/
|
4
|
+
<a href="https://korolvs.github.com/resting_pug"><img src="https://korolvs.github.com/resting_pug/logo.svg" alt="Logo" width="80%"></a>
|
5
5
|
<br>
|
6
6
|
</h1>
|
7
7
|
|
8
|
-
|
8
|
+
[](https://badge.fury.io/rb/resting_pug)
|
9
|
+
[](https://codeclimate.com/github/korolvs/resting_pug/maintainability)
|
10
|
+
[](https://travis-ci.org/korolvs/resting_pug)
|
11
|
+
[](https://codecov.io/gh/korolvs/resting_pug)
|
12
|
+
|
13
|
+
## Table of Contents
|
14
|
+
|
15
|
+
- [What is Resting Pug?](#what-is-resting-pug)
|
16
|
+
- [Why should I use it?](#why-should-i-use-it)
|
17
|
+
- [Installation](#installation)
|
18
|
+
- [Usage](#usage)
|
19
|
+
- [Created actions and thing that you can customize](#created-actions-and-things-you-can-customize)
|
20
|
+
- [Create](#create)
|
21
|
+
- [Update](#update)
|
22
|
+
- [Destroy](#destroy)
|
23
|
+
- [Show](#show)
|
24
|
+
- [Index](#index)
|
25
|
+
- [Contributing](#contributing)
|
26
|
+
- [Changelog](#changelog)
|
27
|
+
- [License](#license)
|
28
|
+
|
29
|
+
## What is Resting Pug?
|
9
30
|
Resting Pug allows you to create a JSON API with just adding one line to your controller and fully customize it with overriding small and simple helper methods.
|
10
31
|
|
32
|
+
## Why should I use it?
|
33
|
+
Often when you want to try a new idea or create a simple app you want to implement basic features very fast.
|
34
|
+
Resting Pug allows you to do it with just couple lines of code.
|
35
|
+
But even though it is small and simple(and because it is small and simple) you can vastly customize created API.
|
36
|
+
Attributes user can see, attributes user can edit, number of items on page, how to render items and errors - thats just a small part of things you can change overriding basic Resting Pug methods.
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
Add this line to your application's Gemfile:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
gem 'resting_pug'
|
43
|
+
```
|
44
|
+
|
45
|
+
And then execute:
|
46
|
+
```bash
|
47
|
+
$ bundle
|
48
|
+
```
|
49
|
+
|
11
50
|
## Usage
|
12
|
-
|
51
|
+
|
52
|
+
**1. Create a model that will be accesible through API:**
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class Book < ApplicationRecord
|
56
|
+
validates :title, presence: true, length: { minimum: 3 }
|
57
|
+
validates :author, presence: true, length: { minimum: 3 }
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class CreateBooks < ActiveRecord::Migration
|
63
|
+
def change
|
64
|
+
create_table :books do |t|
|
65
|
+
t.string :title
|
66
|
+
t.string :author
|
67
|
+
t.integer :year
|
68
|
+
t.integer :rating
|
69
|
+
|
70
|
+
t.timestamps
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
**2. Create a new controller and include ```RestingPug::Base``` into it to add CRUD actions:**
|
13
77
|
|
14
78
|
```ruby
|
15
79
|
class BooksController < ApplicationController
|
@@ -25,7 +89,7 @@ class ApplicationController < ActionController::Base
|
|
25
89
|
end
|
26
90
|
```
|
27
91
|
|
28
|
-
Don't forget to add a route to ```config/routes.rb
|
92
|
+
**3. Don't forget to add a route to ```config/routes.rb```**
|
29
93
|
|
30
94
|
```ruby
|
31
95
|
Rails.application.routes.draw do
|
@@ -33,20 +97,162 @@ Rails.application.routes.draw do
|
|
33
97
|
end
|
34
98
|
```
|
35
99
|
|
36
|
-
|
37
|
-
|
100
|
+
**4. Enjoy your new API and don't hesitate to change it whatever you want!**
|
101
|
+
|
102
|
+
## List of actions and things that you can customize
|
103
|
+
Actions created after including ```RestingPug::Base```:
|
104
|
+
|
105
|
+
### Create
|
38
106
|
|
39
|
-
```ruby
|
40
|
-
gem 'resting_pug'
|
41
107
|
```
|
108
|
+
Request:
|
109
|
+
POST http://your.awesome/api/books
|
110
|
+
{
|
111
|
+
"name": "11/22/63",
|
112
|
+
"author": "Stephen King"
|
113
|
+
}
|
114
|
+
|
115
|
+
Response:
|
116
|
+
200 OK
|
117
|
+
{
|
118
|
+
"book": {
|
119
|
+
"id": 1,
|
120
|
+
"name": "11/22/63",
|
121
|
+
"author": "Stephen King"
|
122
|
+
}
|
123
|
+
}
|
124
|
+
```
|
125
|
+
|
126
|
+
Things you can customize:
|
127
|
+
|
128
|
+
- override [permitted_fields_for_create](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_create) to set which params can be set in a new subject
|
129
|
+
- override [permitted_fields_for_show](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_show) to set which params will be shown in response
|
130
|
+
- override [render_subject](https://korolvs.github.com/resting_pug/0.1.1/render/render_subject) to set how subject will be rendered
|
131
|
+
- override [render_errors](https://korolvs.github.com/resting_pug/0.1.1/render/render_errors) to set how errors will be rendered
|
132
|
+
- override [subject_model](https://korolvs.github.com/resting_pug/0.1.1/subject/subject_model) to set what model will be created
|
133
|
+
- override [create_chain](https://korolvs.github.com/resting_pug/0.1.1/chains/create_chain) to add or remove methods which will be called while creating a subject
|
134
|
+
- override [decide_what_to_render](https://korolvs.github.com/resting_pug/0.1.1/render/decide_what_to_render) to set how it will be decided what to render in response
|
135
|
+
|
136
|
+
### Update
|
137
|
+
|
138
|
+
```
|
139
|
+
Request:
|
140
|
+
PATCH http://your.awesome/api/books/1
|
141
|
+
{
|
142
|
+
"name": "The Green Mile",
|
143
|
+
}
|
144
|
+
|
145
|
+
Response:
|
146
|
+
200 OK
|
147
|
+
{
|
148
|
+
"book": {
|
149
|
+
"id": 1,
|
150
|
+
"name": "The Green Mile",
|
151
|
+
"author": "Stephen King"
|
152
|
+
}
|
153
|
+
}
|
154
|
+
```
|
155
|
+
|
156
|
+
Things you can customize:
|
157
|
+
|
158
|
+
- override [permitted_fields_for_update](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_update) to set which params can be updated
|
159
|
+
- override [permitted_fields_for_show](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_show) to set which params will be shown in response
|
160
|
+
- override [render_subject](https://korolvs.github.com/resting_pug/0.1.1/render/render_subject) to set how subject will be rendered
|
161
|
+
- override [render_errors](https://korolvs.github.com/resting_pug/0.1.1/render/render_errors) to set how errors will be rendered
|
162
|
+
- override [render_not_found](https://korolvs.github.com/resting_pug/0.1.1/render/render_not_found) to set what to render when subject with ID from params is not found
|
163
|
+
- override [subject_model](https://korolvs.github.com/resting_pug/0.1.1/subject/subject_model) to set what model will be updated
|
164
|
+
- override [update_chain](https://korolvs.github.com/resting_pug/0.1.1/chains/update_chain) to add or remove methods which will be called while updating a subject
|
165
|
+
- override [decide_what_to_render](https://korolvs.github.com/resting_pug/0.1.1/render/decide_what_to_render) to set how it will be decided what to render in response
|
166
|
+
|
167
|
+
### Destroy
|
168
|
+
|
169
|
+
```
|
170
|
+
Request:
|
171
|
+
DELETE http://your.awesome/api/books/1
|
172
|
+
|
173
|
+
Response:
|
174
|
+
204 No Content
|
175
|
+
```
|
176
|
+
|
177
|
+
Things you can customize:
|
178
|
+
|
179
|
+
- override [destroy_subject](https://korolvs.github.com/resting_pug/0.1.1/subject/destroy_subject) to set how it will be destroyed
|
180
|
+
- override [render_nothing](https://korolvs.github.com/resting_pug/0.1.1/render/render_nothing) to set what to render when subject is destroyed
|
181
|
+
- override [render_errors](https://korolvs.github.com/resting_pug/0.1.1/render/render_errors) to set how errors will be rendered
|
182
|
+
- override [render_not_found](https://korolvs.github.com/resting_pug/0.1.1/render/render_not_found) to set what to render when subject with ID from params is not found
|
183
|
+
- override [subject_model](https://korolvs.github.com/resting_pug/0.1.1/subject/subject_model) to set what model will be deleted
|
184
|
+
- override [destroy_chain](https://korolvs.github.com/resting_pug/0.1.1/chains/destroy_chain) to add or remove methods which will be called while deleting a subject
|
185
|
+
|
186
|
+
### Show
|
42
187
|
|
43
|
-
And then execute:
|
44
|
-
```bash
|
45
|
-
$ bundle
|
46
188
|
```
|
189
|
+
Request:
|
190
|
+
GET http://your.awesome/api/books/1
|
191
|
+
|
192
|
+
Response:
|
193
|
+
200 OK
|
194
|
+
{
|
195
|
+
"book": {
|
196
|
+
"id": 1,
|
197
|
+
"name": "The Green Mile",
|
198
|
+
"author": "Stephen King"
|
199
|
+
}
|
200
|
+
}
|
201
|
+
```
|
202
|
+
|
203
|
+
Things you can customize:
|
204
|
+
|
205
|
+
- override [permitted_fields_for_show](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_show) to set which params will be shown in response
|
206
|
+
- override [render_subject](https://korolvs.github.com/resting_pug/0.1.1/render/render_subject) to set how subject will be rendered
|
207
|
+
- override [render_not_found](https://korolvs.github.com/resting_pug/0.1.1/render/render_not_found) to set what to render when subject with ID from params is not found
|
208
|
+
- override [subject_model](https://korolvs.github.com/resting_pug/0.1.1/subject/subject_model) to set what model will be shown
|
209
|
+
- override [show_chain](https://korolvs.github.com/resting_pug/0.1.1/chains/show_chain) to add or remove methods which will be called while updating a subject
|
210
|
+
|
211
|
+
### Index
|
212
|
+
|
213
|
+
```
|
214
|
+
Request:
|
215
|
+
GET http://your.awesome/api/books
|
216
|
+
GET http://your.awesome/api/books?filter[author]="King"
|
217
|
+
GET http://your.awesome/api/books?filter[author][]="King"&filter[author][]="Kesey"
|
218
|
+
GET http://your.awesome/api/books?sort=-id,name,-author
|
219
|
+
GET http://your.awesome/api/books?page=3&per_page=10
|
220
|
+
GET http://your.awesome/api/books?filter[author]="King"&sort=-id?page=3&per_page=10
|
221
|
+
|
222
|
+
Response:
|
223
|
+
200 OK
|
224
|
+
{
|
225
|
+
"books": [{
|
226
|
+
"id": 2,
|
227
|
+
"name": "The Green Mile",
|
228
|
+
"author": "Stephen King"
|
229
|
+
}, {
|
230
|
+
"id": 1,
|
231
|
+
"name": "11/22/63",
|
232
|
+
"author": "Stephen King"
|
233
|
+
}]
|
234
|
+
}
|
235
|
+
```
|
236
|
+
|
237
|
+
Things you can customize:
|
238
|
+
|
239
|
+
- override [permitted_fields_for_show](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_show) to set which params will be shown in response
|
240
|
+
- override [permitted_fields_for_filter](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_filter) to set which params can be used for filtering
|
241
|
+
- override [permitted_fields_for_sort](https://korolvs.github.com/resting_pug/0.1.1/params/permitted_fields_for_sort) to set which params can be used for sorting
|
242
|
+
- override [default_sort_params](https://korolvs.github.com/resting_pug/0.1.1/params/default_sort_params) to set default sort params
|
243
|
+
- override [per_page_default](https://korolvs.github.com/resting_pug/0.1.1/params/per_page_default) to set default per_page param
|
244
|
+
- override [per_page_range](https://korolvs.github.com/resting_pug/0.1.1/params/per_page_range) to set minimum and maximum possible per_page value
|
245
|
+
- override [render_subjects](https://korolvs.github.com/resting_pug/0.1.1/render/render_subjects) to set how subjects will be rendered
|
246
|
+
- override [subject_model](https://korolvs.github.com/resting_pug/0.1.1/subject/subject_model) to set what model will be shown
|
247
|
+
- override [index_chain](https://korolvs.github.com/resting_pug/0.1.1/chains/index_chain) to add or remove methods which will be called while updating a subject
|
47
248
|
|
48
249
|
## Contributing
|
49
|
-
|
250
|
+
You're encouraged to submit [pull requests](https://github.com/korolvs/resting_pug/pulls), [propose features and discuss issues](https://github.com/korolvs/resting_pug/issues).
|
251
|
+
|
252
|
+
See [CONTRIBUTING](CONTRIBUTING.md)
|
253
|
+
|
254
|
+
## Changelog
|
255
|
+
To see what has changed in recent versions of Resting Pug, see the [CHANGELOG](CHANGELOG.md).
|
50
256
|
|
51
257
|
## License
|
52
258
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
data/lib/resting_pug/actions.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module RestingPug
|
2
|
-
# Describes basic CRUD actions
|
2
|
+
# Describes basic CRUD actions.
|
3
|
+
# Each action is represented by a chain of methods definied in {Chains} module.
|
3
4
|
# @see https://en.wikipedia.org/wiki/Create,_read,_update_and_delete
|
4
5
|
# @used_in {Base.included}
|
5
6
|
module Actions
|
@@ -22,13 +23,13 @@ module RestingPug
|
|
22
23
|
# }
|
23
24
|
# }
|
24
25
|
# @note You can sustomize creating action:
|
25
|
-
# - override {
|
26
|
-
# - override {
|
27
|
-
# - override {
|
28
|
-
# - override {
|
29
|
-
# - override {
|
30
|
-
# - override {
|
31
|
-
# - override {Render#
|
26
|
+
# - override {Params#permitted_fields_for_create permitted_fields_for_create} to set which params can be set in a new subject
|
27
|
+
# - override {Params#permitted_fields_for_show permitted_fields_for_show} to set which params will be shown in response
|
28
|
+
# - override {Render#render_subject render_subject} to set how subject will be rendered
|
29
|
+
# - override {Render#render_errors render_errors} to set how errors will be rendered
|
30
|
+
# - override {Subject#subject_model subject_model} to set what model will be created
|
31
|
+
# - override {Chains#create_chain create_chain} to add or remove methods which will be called while creating a subject
|
32
|
+
# - override {Render#decide_what_to_render decide_what_to_render} to set how it will be decided what to render in response
|
32
33
|
# @use {#run_chain}
|
33
34
|
# @use {Chains#create_chain}
|
34
35
|
def create
|
@@ -53,14 +54,14 @@ module RestingPug
|
|
53
54
|
# }
|
54
55
|
# }
|
55
56
|
# @note You can sustomize creating action:
|
56
|
-
# - override {
|
57
|
-
# - override {
|
58
|
-
# - override {
|
59
|
-
# - override {
|
60
|
-
# - override {Render#
|
61
|
-
# - override {
|
62
|
-
# - override {
|
63
|
-
# - override {Render#
|
57
|
+
# - override {Params#permitted_fields_for_update permitted_fields_for_update} to set which params can be updated
|
58
|
+
# - override {Params#permitted_fields_for_show permitted_fields_for_show} to set which params will be shown in response
|
59
|
+
# - override {Render#render_subject render_subject} to set how subject will be rendered
|
60
|
+
# - override {Render#render_errors render_errors} to set how errors will be rendered
|
61
|
+
# - override {Render#render_not_found render_not_found} to set what to render when subject with ID from params is not found
|
62
|
+
# - override {Subject#subject_model subject_model} to set what model will be updated
|
63
|
+
# - override {Chains#update_chain update_chain} to add or remove methods which will be called while updating a subject
|
64
|
+
# - override {Render#decide_what_to_render decide_what_to_render} to set how it will be decided what to render in response
|
64
65
|
# @use {#run_chain}
|
65
66
|
# @use {Chains#update_chain}
|
66
67
|
def update
|
@@ -75,12 +76,12 @@ module RestingPug
|
|
75
76
|
# Response:
|
76
77
|
# 204 No Content
|
77
78
|
# @note You can sustomize creating action:
|
78
|
-
# - override {Subject#
|
79
|
-
# - override {
|
80
|
-
# - override {
|
81
|
-
# - override {Render#
|
82
|
-
# - override {
|
83
|
-
# - override {
|
79
|
+
# - override {Subject#destroy_subject destroy_subject} to set how it will be destroyed
|
80
|
+
# - override {Render#render_nothing render_nothing} to set what to render when subject is destroyed
|
81
|
+
# - override {Render#render_errors render_errors} to set how errors will be rendered
|
82
|
+
# - override {Render#render_not_found render_not_found} to set what to render when subject with ID from params is not found
|
83
|
+
# - override {Subject#subject_model subject_model} to set what model will be deleted
|
84
|
+
# - override {Chains#destroy_chain destroy_chain} to add or remove methods which will be called while deleting a subject
|
84
85
|
# @use {#run_chain}
|
85
86
|
# @use {Chains#destroy_chain}
|
86
87
|
def destroy
|
@@ -102,11 +103,11 @@ module RestingPug
|
|
102
103
|
# }
|
103
104
|
# }
|
104
105
|
# @note You can sustomize creating action:
|
105
|
-
# - override {
|
106
|
-
# - override {
|
107
|
-
# - override {
|
108
|
-
# - override {
|
109
|
-
# - override {
|
106
|
+
# - override {Params#permitted_fields_for_show permitted_fields_for_show} to set which params will be shown in response
|
107
|
+
# - override {Render#render_subject render_subject} to set how subject will be rendered
|
108
|
+
# - override {Render#render_not_found render_not_found} to set what to render when subject with ID from params is not found
|
109
|
+
# - override {Subject#subject_model subject_model} to set what model will be shown
|
110
|
+
# - override {Chains#show_chain show_chain} to add or remove methods which will be called while updating a subject
|
110
111
|
# @use {#run_chain}
|
111
112
|
# @use {Chains#show_chain}
|
112
113
|
def show
|
@@ -140,15 +141,15 @@ module RestingPug
|
|
140
141
|
# }]
|
141
142
|
# }
|
142
143
|
# @note You can sustomize creating action:
|
143
|
-
# - override {
|
144
|
-
# - override {
|
145
|
-
# - override {Params#
|
146
|
-
# - override {Params#
|
147
|
-
# - override {Params#
|
148
|
-
# - override {Params#
|
149
|
-
# - override {
|
150
|
-
# - override {
|
151
|
-
# - override {
|
144
|
+
# - override {Params#permitted_fields_for_show permitted_fields_for_show} to set which params will be shown in response
|
145
|
+
# - override {Params#permitted_fields_for_filter permitted_fields_for_filter} to set which params can be used for filtering
|
146
|
+
# - override {Params#permitted_fields_for_sort permitted_fields_for_sort} to set which params can be used for sorting
|
147
|
+
# - override {Params#default_sort_params default_sort_params} to set default sort params
|
148
|
+
# - override {Params#per_page_default per_page_default} to set default per_page param
|
149
|
+
# - override {Params#per_page_range per_page_range} to set minimum and maximum possible per_page value
|
150
|
+
# - override {Render#render_subjects render_subjects} to set how subjects will be rendered
|
151
|
+
# - override {Subject#subject_model subject_model} to set what model will be shown
|
152
|
+
# - override {Chains#index_chain index_chain} to add or remove methods which will be called while updating a subject
|
152
153
|
# @use {#run_chain}
|
153
154
|
# @use {Chains#index_chain}
|
154
155
|
def index
|
@@ -159,6 +160,11 @@ module RestingPug
|
|
159
160
|
|
160
161
|
# Executes each method in a chain one by one
|
161
162
|
# @param chain [Array] array of methods to execute
|
163
|
+
# @used_in {#create}
|
164
|
+
# @used_in {#update}
|
165
|
+
# @used_in {#destroy}
|
166
|
+
# @used_in {#show}
|
167
|
+
# @used_in {#index}
|
162
168
|
def run_chain(chain)
|
163
169
|
chain.each do |action|
|
164
170
|
self.send(action)
|
data/lib/resting_pug/base.rb
CHANGED
data/lib/resting_pug/chains.rb
CHANGED
@@ -9,6 +9,16 @@ module RestingPug
|
|
9
9
|
# @used_in {Actions#create}
|
10
10
|
# @use {Subject#create_subject}
|
11
11
|
# @use {Render#decide_what_to_render}
|
12
|
+
# @example
|
13
|
+
# class BooksController < ApplicationController
|
14
|
+
# include RestingPug::Base
|
15
|
+
#
|
16
|
+
# private
|
17
|
+
# # Override a create chain adding logging to the end
|
18
|
+
# def create_chain
|
19
|
+
# super + [:log_creating_subject]
|
20
|
+
# end
|
21
|
+
# end
|
12
22
|
def create_chain
|
13
23
|
[:create_subject, :decide_what_to_render]
|
14
24
|
end
|
@@ -19,6 +29,16 @@ module RestingPug
|
|
19
29
|
# @use {Subject#fetch_subject}
|
20
30
|
# @use {Subject#update_subject}
|
21
31
|
# @use {Render#decide_what_to_render}
|
32
|
+
# @example
|
33
|
+
# class BooksController < ApplicationController
|
34
|
+
# include RestingPug::Base
|
35
|
+
#
|
36
|
+
# private
|
37
|
+
# # Override an update chain adding logging to the end
|
38
|
+
# def update_chain
|
39
|
+
# super + [:log_updating_subject]
|
40
|
+
# end
|
41
|
+
# end
|
22
42
|
def update_chain
|
23
43
|
[:fetch_subject, :update_subject, :decide_what_to_render]
|
24
44
|
end
|
@@ -28,6 +48,16 @@ module RestingPug
|
|
28
48
|
# @used_in {Actions#destroy}
|
29
49
|
# @use {Subject#fetch_subject}
|
30
50
|
# @use {Subject#destroy_subject}
|
51
|
+
# @example
|
52
|
+
# class BooksController < ApplicationController
|
53
|
+
# include RestingPug::Base
|
54
|
+
#
|
55
|
+
# private
|
56
|
+
# # Override a destroy chain adding logging to the end
|
57
|
+
# def destroy_chain
|
58
|
+
# super + [:log_destroying_subject]
|
59
|
+
# end
|
60
|
+
# end
|
31
61
|
def destroy_chain
|
32
62
|
[:fetch_subject, :destroy_subject]
|
33
63
|
end
|
@@ -37,6 +67,16 @@ module RestingPug
|
|
37
67
|
# @used_in {Actions#show}
|
38
68
|
# @use {Subject#fetch_subject}
|
39
69
|
# @use {Render#render_subject}
|
70
|
+
# @example
|
71
|
+
# class BooksController < ApplicationController
|
72
|
+
# include RestingPug::Base
|
73
|
+
#
|
74
|
+
# private
|
75
|
+
# # Override a show chain adding logging to the end
|
76
|
+
# def show_chain
|
77
|
+
# super + [:log_showing_subject]
|
78
|
+
# end
|
79
|
+
# end
|
40
80
|
def show_chain
|
41
81
|
[:fetch_subject, :render_subject]
|
42
82
|
end
|
@@ -49,6 +89,16 @@ module RestingPug
|
|
49
89
|
# @use {Modificators#sort_subjects}
|
50
90
|
# @use {Modificators#paginate_subjects}
|
51
91
|
# @use {Render#render_subjects}
|
92
|
+
# @example
|
93
|
+
# class BooksController < ApplicationController
|
94
|
+
# include RestingPug::Base
|
95
|
+
#
|
96
|
+
# private
|
97
|
+
# # Override an index chain adding logging to the end
|
98
|
+
# def index_chain
|
99
|
+
# super + [:log_indexing_subjects]
|
100
|
+
# end
|
101
|
+
# end
|
52
102
|
def index_chain
|
53
103
|
[:fetch_subjects, :filter_subjects, :sort_subjects, :paginate_subjects, :render_subjects]
|
54
104
|
end
|
@@ -8,6 +8,16 @@ module RestingPug
|
|
8
8
|
# @note Override this method to change pagination of subjects
|
9
9
|
# @used_in {Chains#index_chain}
|
10
10
|
# @use {Params#pagination_params}
|
11
|
+
# @example
|
12
|
+
# class BooksController < ApplicationController
|
13
|
+
# include RestingPug::Base
|
14
|
+
#
|
15
|
+
# private
|
16
|
+
# # Override a paginate_subjects method to use kaminari instead of will_paginate
|
17
|
+
# def paginate_subjects
|
18
|
+
# @subjects = @subjects.page(pagination_params[:page]).per(pagination_params[:per_page])
|
19
|
+
# end
|
20
|
+
# end
|
11
21
|
def paginate_subjects
|
12
22
|
@subjects = @subjects.paginate(page: pagination_params[:page], per_page: pagination_params[:per_page])
|
13
23
|
end
|
@@ -16,6 +26,16 @@ module RestingPug
|
|
16
26
|
# @note Override this method to change sorting of subjects
|
17
27
|
# @used_in {Chains#index_chain}
|
18
28
|
# @use {Params#sort_params}
|
29
|
+
# @example
|
30
|
+
# class BooksController < ApplicationController
|
31
|
+
# include RestingPug::Base
|
32
|
+
#
|
33
|
+
# private
|
34
|
+
# # Override a sort_subjects method to remove default ordering beforehand.
|
35
|
+
# def sort_subjects
|
36
|
+
# @subjects = @subjects.reorder(nil).order(sort_params)
|
37
|
+
# end
|
38
|
+
# end
|
19
39
|
def sort_subjects
|
20
40
|
@subjects = @subjects.order(sort_params)
|
21
41
|
end
|
@@ -23,7 +43,17 @@ module RestingPug
|
|
23
43
|
# Filters subjects using filter params
|
24
44
|
# @note Override this method to change how subjects are filtered
|
25
45
|
# @used_in {Chains#index_chain}
|
26
|
-
# @use {Params#
|
46
|
+
# @use {Params#filter_params}
|
47
|
+
# @example
|
48
|
+
# class BooksController < ApplicationController
|
49
|
+
# include RestingPug::Base
|
50
|
+
#
|
51
|
+
# private
|
52
|
+
# # Override a filter_subjects method to remove default scope beforehand.
|
53
|
+
# def filter_subjects
|
54
|
+
# @subjects = @subjects.unscoped.where(filter_params)
|
55
|
+
# end
|
56
|
+
# end
|
27
57
|
def filter_subjects
|
28
58
|
@subjects = @subjects.where(filter_params)
|
29
59
|
end
|
data/lib/resting_pug/params.rb
CHANGED
@@ -8,50 +8,95 @@ module RestingPug
|
|
8
8
|
|
9
9
|
# Returns an array with all fields that API can use
|
10
10
|
# @note Override this method to add or remove fields that you don't want API to use.
|
11
|
-
# @return [Array] of symbols representing attributes
|
12
11
|
# @used_in {#permitted_fields_for_show}
|
13
12
|
# @used_in {#permitted_fields_for_create}
|
14
13
|
# @used_in {#permitted_fields_for_update}
|
15
14
|
# @used_in {#permitted_fields_for_sort}
|
16
15
|
# @used_in {#permitted_fields_for_filter}
|
17
16
|
# @use {Subject#subject_model}
|
17
|
+
# @example
|
18
|
+
# class BooksController < ApplicationController
|
19
|
+
# include RestingPug::Base
|
20
|
+
#
|
21
|
+
# private
|
22
|
+
# # Override a permitted_fields method to use only :id and :title
|
23
|
+
# def permitted_fields
|
24
|
+
# [:id, :title]
|
25
|
+
# end
|
26
|
+
# end
|
18
27
|
def permitted_fields
|
19
28
|
subject_model.column_names.map(&:to_sym)
|
20
29
|
end
|
21
30
|
|
22
31
|
# Returns an array with all fields that API will show
|
23
32
|
# @note Override this method to add or remove fields that you don't want API to show.
|
24
|
-
# @return [Array] of symbols representing attributes
|
25
33
|
# @used_in {Render#render_subject}
|
26
34
|
# @used_in {Render#render_subjects}
|
27
35
|
# @use {#permitted_fields}
|
36
|
+
# @example
|
37
|
+
# class BooksController < ApplicationController
|
38
|
+
# include RestingPug::Base
|
39
|
+
#
|
40
|
+
# private
|
41
|
+
# # Override a permitted_fields_for_show method to show every permitted attribute except :title
|
42
|
+
# def permitted_fields_for_show
|
43
|
+
# permitted_fields - [:title]
|
44
|
+
# end
|
45
|
+
# end
|
28
46
|
def permitted_fields_for_show
|
29
47
|
permitted_fields
|
30
48
|
end
|
31
49
|
|
32
50
|
# Returns an array with all fields that API will allow to use while creating a subject
|
33
51
|
# @note Override this method to add or remove fields that you don't want API to use for creating.
|
34
|
-
# @return [Array] of symbols representing attributes
|
35
52
|
# @used_in {#params_for_create}
|
36
53
|
# @use {#permitted_fields}
|
54
|
+
# @example
|
55
|
+
# class BooksController < ApplicationController
|
56
|
+
# include RestingPug::Base
|
57
|
+
#
|
58
|
+
# private
|
59
|
+
# # Override a permitted_fields_for_create method to use every permitted attribute except :title
|
60
|
+
# def permitted_fields_for_create
|
61
|
+
# permitted_fields - [:title]
|
62
|
+
# end
|
63
|
+
# end
|
37
64
|
def permitted_fields_for_create
|
38
65
|
permitted_fields - [:id, :created_at, :updated_at]
|
39
66
|
end
|
40
67
|
|
41
68
|
# Returns an array with all fields that API will allow to use while updating a subject
|
42
69
|
# @note Override this method to add or remove fields that you don't want API to use for updating.
|
43
|
-
# @return [Array] of symbols representing attributes
|
44
70
|
# @used_in {#params_for_update}
|
45
71
|
# @use {#permitted_fields}
|
72
|
+
# @example
|
73
|
+
# class BooksController < ApplicationController
|
74
|
+
# include RestingPug::Base
|
75
|
+
#
|
76
|
+
# private
|
77
|
+
# # Override a permitted_fields_for_update method to use every permitted attribute except :title
|
78
|
+
# def permitted_fields_for_update
|
79
|
+
# permitted_fields - [:title]
|
80
|
+
# end
|
81
|
+
# end
|
46
82
|
def permitted_fields_for_update
|
47
83
|
permitted_fields - [:id, :created_at, :updated_at]
|
48
84
|
end
|
49
85
|
|
50
86
|
# Returns an array with all fields that API will allow to use while sorting subjects
|
51
87
|
# @note Override this method to add or remove fields that you don't want API to use for sorting.
|
52
|
-
# @return [Array] of symbols representing attributes
|
53
88
|
# @used_in {#sort_params}
|
54
89
|
# @use {#permitted_fields}
|
90
|
+
# @example
|
91
|
+
# class BooksController < ApplicationController
|
92
|
+
# include RestingPug::Base
|
93
|
+
#
|
94
|
+
# private
|
95
|
+
# # Override a permitted_fields_for_sort method to use every permitted attribute except :title
|
96
|
+
# def permitted_fields_for_sort
|
97
|
+
# permitted_fields - [:title]
|
98
|
+
# end
|
99
|
+
# end
|
55
100
|
def permitted_fields_for_sort
|
56
101
|
permitted_fields
|
57
102
|
end
|
@@ -59,10 +104,19 @@ module RestingPug
|
|
59
104
|
# Returns an array with all fields that API will allow to use while filtering subjects by single value
|
60
105
|
# @note Override this method to add or remove fields that you don't want API to use for filtering by single value.
|
61
106
|
# If you want to change attributes that can be filtered by arrays of values check {#permitted_fields_for_filter_arrays}
|
62
|
-
# @return [Array] of symbols representing attributes
|
63
107
|
# @used_in {#permitted_fields_for_filter_arrays}
|
64
108
|
# @used_in {#permitted_fields_for_filter_with_arrays}
|
65
109
|
# @use {#permitted_fields}
|
110
|
+
# @example
|
111
|
+
# class BooksController < ApplicationController
|
112
|
+
# include RestingPug::Base
|
113
|
+
#
|
114
|
+
# private
|
115
|
+
# # Override a permitted_fields_for_sort method to use every permitted attribute except :title
|
116
|
+
# def permitted_fields_for_sort
|
117
|
+
# permitted_fields - [:title]
|
118
|
+
# end
|
119
|
+
# end
|
66
120
|
def permitted_fields_for_filter
|
67
121
|
permitted_fields - [:created_at, :updated_at]
|
68
122
|
end
|
@@ -70,16 +124,24 @@ module RestingPug
|
|
70
124
|
# Returns an array with all fields that API will allow to use while filtering subjects by array of values
|
71
125
|
# @note Override this method to add or remove fields that you don't want API to use for filtering by array of values.
|
72
126
|
# If you want to change attributes that can be filtered by single value check {#permitted_fields_for_filter}
|
73
|
-
# @return [Array] of symbols representing attributes
|
74
127
|
# @used_in {#permitted_fields_for_filter_with_arrays}
|
75
128
|
# @use {#permitted_fields_for_filter}
|
129
|
+
# @example
|
130
|
+
# class BooksController < ApplicationController
|
131
|
+
# include RestingPug::Base
|
132
|
+
#
|
133
|
+
# private
|
134
|
+
# # Override a permitted_fields_for_filter_arrays method to use only :id and :title
|
135
|
+
# def permitted_fields_for_filter_arrays
|
136
|
+
# { id: [], title: []}
|
137
|
+
# end
|
138
|
+
# end
|
76
139
|
def permitted_fields_for_filter_arrays
|
77
140
|
permitted_fields_for_filter.map { |field| { field => [] } }
|
78
141
|
end
|
79
142
|
|
80
143
|
# Returns an array with all fields that API will allow to use while filtering subjects both by single values and by array of values
|
81
144
|
# @note To set fields that can be used check {#permitted_fields_for_filter} and {#permitted_fields_for_filter_arrays}
|
82
|
-
# @return [Array] of symbols representing attributes
|
83
145
|
# @used_in {#filter_params}
|
84
146
|
# @use {#permitted_fields_for_filter}
|
85
147
|
# @use {#permitted_fields_for_filter_arrays}
|
@@ -90,8 +152,8 @@ module RestingPug
|
|
90
152
|
# PARAMS
|
91
153
|
|
92
154
|
# Returns strong parameters which are used in creating action
|
93
|
-
# @note Override {#permitted_fields_for_create} to set allowed fields.
|
94
|
-
# Override {Subject#subject_model_sym} to change the root attrubute of params.
|
155
|
+
# @note Override {#permitted_fields_for_create permitted_fields_for_create} to set allowed fields.
|
156
|
+
# Override {Subject#subject_model_sym subject_model_sym} to change the root attrubute of params.
|
95
157
|
# @see http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
|
96
158
|
# @used_in {Subject#create_subject}
|
97
159
|
# @use {Subject#subject_model_sym}
|
@@ -101,8 +163,8 @@ module RestingPug
|
|
101
163
|
end
|
102
164
|
|
103
165
|
# Returns strong parameters which are used in updating action
|
104
|
-
# @note Override {#permitted_fields_for_update} to set allowed fields.
|
105
|
-
# Override {Subject#subject_model_sym} to change the root attrubute of params.
|
166
|
+
# @note Override {#permitted_fields_for_update permitted_fields_for_update} to set allowed fields.
|
167
|
+
# Override {Subject#subject_model_sym subject_model_sym} to change the root attrubute of params.
|
106
168
|
# @see http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
|
107
169
|
# @used_in {Subject#update_subject}
|
108
170
|
# @use {Subject#subject_model_sym}
|
@@ -114,7 +176,7 @@ module RestingPug
|
|
114
176
|
# FILTERING
|
115
177
|
|
116
178
|
# Returns strong parameters which are used for filtering subjects
|
117
|
-
# @note Override {#permitted_fields_for_filter} and {permitted_fields_for_filter_arrays} to set allowed fields.
|
179
|
+
# @note Override {#permitted_fields_for_filter permitted_fields_for_filter} and {#permitted_fields_for_filter_arrays permitted_fields_for_filter_arrays} to set allowed fields.
|
118
180
|
# @see http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
|
119
181
|
# @used_in {Modificators#filter_subjects}
|
120
182
|
# @use {#permitted_fields_for_filter_with_arrays}
|
@@ -125,8 +187,8 @@ module RestingPug
|
|
125
187
|
# SORT
|
126
188
|
|
127
189
|
# Transforms a string like "-id,name" to a hash { id: :desc, name: :asc }
|
128
|
-
# @note Override {#permitted_fields_for_sort} to set allowed fields to sort.
|
129
|
-
# Override {#default_sort_params} to set default sort params.
|
190
|
+
# @note Override {#permitted_fields_for_sort permitted_fields_for_sort} to set allowed fields to sort.
|
191
|
+
# Override {#default_sort_params default_sort_params} to set default sort params.
|
130
192
|
# @used_in {Modificators#sort_subjects}
|
131
193
|
# @use {#permitted_fields_for_sort}
|
132
194
|
# @use {#default_sort_params}
|
@@ -151,9 +213,19 @@ module RestingPug
|
|
151
213
|
end
|
152
214
|
|
153
215
|
# Returns a hash with default sorting params
|
154
|
-
# @note Override {#permitted_fields_for_sort} to set allowed fields to sort.
|
216
|
+
# @note Override {#permitted_fields_for_sort permitted_fields_for_sort} to set allowed fields to sort.
|
155
217
|
# @see http://guides.rubyonrails.org/active_record_querying.html#ordering
|
156
218
|
# @used_in {#sort_params}
|
219
|
+
# @example
|
220
|
+
# class BooksController < ApplicationController
|
221
|
+
# include RestingPug::Base
|
222
|
+
#
|
223
|
+
# private
|
224
|
+
# # Override a default_sort_params to set default sorting to sorted descending by title
|
225
|
+
# def default_sort_params
|
226
|
+
# { title: :desc }
|
227
|
+
# end
|
228
|
+
# end
|
157
229
|
def default_sort_params
|
158
230
|
{id: :desc}
|
159
231
|
end
|
@@ -161,8 +233,8 @@ module RestingPug
|
|
161
233
|
# PAGINATION
|
162
234
|
|
163
235
|
# Handles pagination params and return a hash like { page: 1, per_page: 10 }
|
164
|
-
# @note Override {#per_page_range} to set minimum and maximum per_page param.
|
165
|
-
# Override {#per_page_default} to set default per_page param.
|
236
|
+
# @note Override {#per_page_range per_page_range} to set minimum and maximum per_page param.
|
237
|
+
# Override {#per_page_default per_page_default} to set default per_page param.
|
166
238
|
# @used_in {Modificators#paginate_subjects}
|
167
239
|
# @use {#per_page_range}
|
168
240
|
# @use {#per_page_default}
|
@@ -172,18 +244,38 @@ module RestingPug
|
|
172
244
|
return { page: page, per_page: per_page }
|
173
245
|
end
|
174
246
|
|
175
|
-
# Returns a hash minimum and maximum per_page param like { page: 1, per_page: 10 }
|
176
|
-
# @note Override {#per_page_default} to set default per_page param.
|
247
|
+
# Returns a hash with minimum and maximum per_page param like { page: 1, per_page: 10 }
|
248
|
+
# @note Override {#per_page_default per_page_default} to set default per_page param.
|
177
249
|
# @used_in {#pagination_params}
|
250
|
+
# @example
|
251
|
+
# class BooksController < ApplicationController
|
252
|
+
# include RestingPug::Base
|
253
|
+
#
|
254
|
+
# private
|
255
|
+
# # Override a per_page_range to set minimum possible value for per_page 10 and maximum 20
|
256
|
+
# def per_page_range
|
257
|
+
# { min: 10, max: 20 }
|
258
|
+
# end
|
259
|
+
# end
|
178
260
|
def per_page_range
|
179
261
|
{ min: 5, max: 100 }
|
180
262
|
end
|
181
263
|
|
182
264
|
# Returns a default per_page param
|
183
|
-
# @note Override {#per_page_range} to set minimum and maximum per_page param
|
265
|
+
# @note Override {#per_page_range per_page_range} to set minimum and maximum per_page param
|
184
266
|
# @used_in {#pagination_params}
|
267
|
+
# @example
|
268
|
+
# class BooksController < ApplicationController
|
269
|
+
# include RestingPug::Base
|
270
|
+
#
|
271
|
+
# private
|
272
|
+
# # Override a per_page_default to set default page size to 15
|
273
|
+
# def per_page_default
|
274
|
+
# 15
|
275
|
+
# end
|
276
|
+
# end
|
185
277
|
def per_page_default
|
186
278
|
10
|
187
279
|
end
|
188
280
|
end
|
189
|
-
end
|
281
|
+
end
|
data/lib/resting_pug/render.rb
CHANGED
@@ -5,11 +5,25 @@ module RestingPug
|
|
5
5
|
protected
|
6
6
|
|
7
7
|
# Calls {#render_subject} if subject is valid and {#render_errors} otherwise
|
8
|
-
# @note Override {#render_subject} and {#render_errors} to change rendering behaviour
|
8
|
+
# @note Override {#render_subject render_subject} and {#render_errors render_errors} to change rendering behaviour
|
9
9
|
# @used_in {Chains#create_chain}
|
10
10
|
# @used_in {Chains#update_chain}
|
11
11
|
# @use {#render_subject}
|
12
12
|
# @use {#render_errors}
|
13
|
+
# @example
|
14
|
+
# class BooksController < ApplicationController
|
15
|
+
# include RestingPug::Base
|
16
|
+
#
|
17
|
+
# private
|
18
|
+
# # Override a decide_what_to_render to render something special if @subject id is 3
|
19
|
+
# def decide_what_to_render
|
20
|
+
# if @subject.valid?
|
21
|
+
# @subject.id == 3 ? render_something_special : render_subject
|
22
|
+
# else
|
23
|
+
# render_errors
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
# end
|
13
27
|
def decide_what_to_render
|
14
28
|
if @subject.valid?
|
15
29
|
render_subject
|
@@ -20,23 +34,43 @@ module RestingPug
|
|
20
34
|
|
21
35
|
# Renders json with a subject
|
22
36
|
# @note Override this method to add more complicated logic of rendering.
|
23
|
-
# Override {Subject#subject_model_sym} to change root attribute.
|
24
|
-
# Override {Params#permitted_fields_for_show} to change shown attributes.
|
37
|
+
# Override {Subject#subject_model_sym subject_model_sym} to change root attribute.
|
38
|
+
# Override {Params#permitted_fields_for_show permitted_fields_for_show} to change shown attributes.
|
25
39
|
# @used_in {#decide_what_to_render}
|
26
40
|
# @used_in {Chains#show_chain}
|
27
41
|
# @use {Subject#subject_model_sym}
|
28
42
|
# @use {Params#permitted_fields_for_show}
|
43
|
+
# @example
|
44
|
+
# class BooksController < ApplicationController
|
45
|
+
# include RestingPug::Base
|
46
|
+
#
|
47
|
+
# private
|
48
|
+
# # Override a render_subject to render without a root object
|
49
|
+
# def render_subject
|
50
|
+
# render json: @subject.as_json(only: permitted_fields_for_show)
|
51
|
+
# end
|
52
|
+
# end
|
29
53
|
def render_subject
|
30
54
|
render json: { subject_model_sym => @subject.as_json(only: permitted_fields_for_show) }
|
31
55
|
end
|
32
56
|
|
33
57
|
# Renders json with subjects
|
34
58
|
# @note Override this method to add more complicated logic of rendering.
|
35
|
-
# Override {Subject#subject_model_sym_plural} to change root attribute.
|
36
|
-
# Override {Params#permitted_fields_for_show} to change shown attributes.
|
59
|
+
# Override {Subject#subject_model_sym_plural subject_model_sym_plural} to change root attribute.
|
60
|
+
# Override {Params#permitted_fields_for_show permitted_fields_for_show} to change shown attributes.
|
37
61
|
# @used_in {Chains#index_chain}
|
38
62
|
# @use {Subject#subject_model_sym_plural}
|
39
63
|
# @use {Params#permitted_fields_for_show}
|
64
|
+
# @example
|
65
|
+
# class BooksController < ApplicationController
|
66
|
+
# include RestingPug::Base
|
67
|
+
#
|
68
|
+
# private
|
69
|
+
# # Override a render_subjects to render with 'items' as a root object
|
70
|
+
# def render_subjects
|
71
|
+
# render json: { items: @subjects.as_json(only: permitted_fields_for_show) }
|
72
|
+
# end
|
73
|
+
# end
|
40
74
|
def render_subjects
|
41
75
|
render json: { subject_model_sym_plural => @subjects.as_json(only: permitted_fields_for_show) }
|
42
76
|
end
|
@@ -44,6 +78,16 @@ module RestingPug
|
|
44
78
|
# Renders empty response
|
45
79
|
# @note Override this method to add more complicated logic of rendering.
|
46
80
|
# @used_in {Subject#destroy_subject}
|
81
|
+
# @example
|
82
|
+
# class BooksController < ApplicationController
|
83
|
+
# include RestingPug::Base
|
84
|
+
#
|
85
|
+
# private
|
86
|
+
# # Override a render_nothing to render with status 200
|
87
|
+
# def render_nothing
|
88
|
+
# render json: {}, status: 200
|
89
|
+
# end
|
90
|
+
# end
|
47
91
|
def render_nothing
|
48
92
|
render json: {}, status: 204
|
49
93
|
end
|
@@ -51,6 +95,16 @@ module RestingPug
|
|
51
95
|
# Renders errors
|
52
96
|
# @note Override this method to add more complicated logic of rendering.
|
53
97
|
# @used_in {#decide_what_to_render}
|
98
|
+
# @example
|
99
|
+
# class BooksController < ApplicationController
|
100
|
+
# include RestingPug::Base
|
101
|
+
#
|
102
|
+
# private
|
103
|
+
# # Override a render_errors to render with status 400
|
104
|
+
# def render_errors
|
105
|
+
# render json: { errors: @subject.errors }, status: 400
|
106
|
+
# end
|
107
|
+
# end
|
54
108
|
def render_errors
|
55
109
|
render json: { errors: @subject.errors }, status: 422
|
56
110
|
end
|
@@ -58,15 +112,35 @@ module RestingPug
|
|
58
112
|
# Renders error when required params are missing
|
59
113
|
# @note Override this method to add more complicated logic of rendering.
|
60
114
|
# @used_in {Base.included}
|
115
|
+
# @example
|
116
|
+
# class BooksController < ApplicationController
|
117
|
+
# include RestingPug::Base
|
118
|
+
#
|
119
|
+
# private
|
120
|
+
# # Override a render_param_missing to render with status 422
|
121
|
+
# def render_param_missing
|
122
|
+
# render json: { errors: { subject_model_sym => ['param is missing'] } }, status: 422
|
123
|
+
# end
|
124
|
+
# end
|
61
125
|
def render_param_missing
|
62
126
|
render json: { errors: { subject_model_sym => ['param is missing'] } }, status: 400
|
63
127
|
end
|
64
128
|
|
65
|
-
# Renders error when required
|
129
|
+
# Renders error when required object is not found
|
66
130
|
# @note Override this method to add more complicated logic of rendering.
|
67
131
|
# @used_in {Base.included}
|
132
|
+
# @example
|
133
|
+
# class BooksController < ApplicationController
|
134
|
+
# include RestingPug::Base
|
135
|
+
#
|
136
|
+
# private
|
137
|
+
# # Override a render_not_found to render with message 'Not found'
|
138
|
+
# def render_not_found
|
139
|
+
# render json: { message: 'Not found' }, status: 404
|
140
|
+
# end
|
141
|
+
# end
|
68
142
|
def render_not_found
|
69
143
|
render json: {}, status: 404
|
70
144
|
end
|
71
145
|
end
|
72
|
-
end
|
146
|
+
end
|
data/lib/resting_pug/subject.rb
CHANGED
@@ -12,6 +12,16 @@ module RestingPug
|
|
12
12
|
# @used_in {#fetch_subject}
|
13
13
|
# @used_in {#fetch_subjects}
|
14
14
|
# @used_in {Params#permitted_fields}
|
15
|
+
# @example
|
16
|
+
# class BooksController < ApplicationController
|
17
|
+
# include RestingPug::Base
|
18
|
+
#
|
19
|
+
# private
|
20
|
+
# # Override a subject_model to use Novel as a model
|
21
|
+
# def subject_model
|
22
|
+
# Novel
|
23
|
+
# end
|
24
|
+
# end
|
15
25
|
def subject_model
|
16
26
|
controller_name.classify.constantize
|
17
27
|
end
|
@@ -23,6 +33,16 @@ module RestingPug
|
|
23
33
|
# @used_in {Params#params_for_update}
|
24
34
|
# @used_in {Render#render_subject}
|
25
35
|
# @used_in {Render#render_param_missing}
|
36
|
+
# @example
|
37
|
+
# class BooksController < ApplicationController
|
38
|
+
# include RestingPug::Base
|
39
|
+
#
|
40
|
+
# private
|
41
|
+
# # Override a subject_model to use :novel as a symbol used in params and rendering
|
42
|
+
# def subject_model_sym
|
43
|
+
# :novel
|
44
|
+
# end
|
45
|
+
# end
|
26
46
|
def subject_model_sym
|
27
47
|
subject_model.to_s.underscore.to_sym
|
28
48
|
end
|
@@ -31,6 +51,16 @@ module RestingPug
|
|
31
51
|
# @note Override this method to get another root JSON response.
|
32
52
|
# @use {#subject_model}
|
33
53
|
# @used_in {Render#render_subjects}
|
54
|
+
# @example
|
55
|
+
# class BooksController < ApplicationController
|
56
|
+
# include RestingPug::Base
|
57
|
+
#
|
58
|
+
# private
|
59
|
+
# # Override a subject_model_sym_plural to use :novels as a symbol used in index action
|
60
|
+
# def subject_model_sym_plural
|
61
|
+
# :novels
|
62
|
+
# end
|
63
|
+
# end
|
34
64
|
def subject_model_sym_plural
|
35
65
|
subject_model.to_s.pluralize.underscore.to_sym
|
36
66
|
end
|
@@ -40,6 +70,16 @@ module RestingPug
|
|
40
70
|
# @used_in {Chains#create_chain}
|
41
71
|
# @use {#subject_model}
|
42
72
|
# @use {Params#params_for_create}
|
73
|
+
# @example
|
74
|
+
# class BooksController < ApplicationController
|
75
|
+
# include RestingPug::Base
|
76
|
+
#
|
77
|
+
# private
|
78
|
+
# # Override a create_subject to create only if title is not 'None'
|
79
|
+
# def create_subject
|
80
|
+
# @subject = subject_model.create(params_for_create) unless params_for_create[:title] == 'None'
|
81
|
+
# end
|
82
|
+
# end
|
43
83
|
def create_subject
|
44
84
|
@subject = subject_model.create(params_for_create)
|
45
85
|
end
|
@@ -48,6 +88,16 @@ module RestingPug
|
|
48
88
|
# @note Override this method to add some custom logic to updating process.
|
49
89
|
# @used_in {Chains#update_chain}
|
50
90
|
# @use {Params#params_for_update}
|
91
|
+
# @example
|
92
|
+
# class BooksController < ApplicationController
|
93
|
+
# include RestingPug::Base
|
94
|
+
#
|
95
|
+
# private
|
96
|
+
# # Override a update_subject to update only if title is not 'None'
|
97
|
+
# def update_subject
|
98
|
+
# @subject.update(params_for_update) unless params_for_update[:title] == 'None'
|
99
|
+
# end
|
100
|
+
# end
|
51
101
|
def update_subject
|
52
102
|
@subject.update(params_for_update)
|
53
103
|
end
|
@@ -57,6 +107,20 @@ module RestingPug
|
|
57
107
|
# @used_in {Chains#destroy_chain}
|
58
108
|
# @use {Render#render_nothing}
|
59
109
|
# @use {Render#render_errors}
|
110
|
+
# @example
|
111
|
+
# class BooksController < ApplicationController
|
112
|
+
# include RestingPug::Base
|
113
|
+
#
|
114
|
+
# private
|
115
|
+
# # Override a destroy_subject to destroy only if title is not 'None'
|
116
|
+
# def destroy_subject
|
117
|
+
# if @subject.title != 'None' && @subject.destroy
|
118
|
+
# render_nothing
|
119
|
+
# else
|
120
|
+
# render_errors
|
121
|
+
# end
|
122
|
+
# end
|
123
|
+
# end
|
60
124
|
def destroy_subject
|
61
125
|
if @subject.destroy
|
62
126
|
render_nothing
|
@@ -72,6 +136,16 @@ module RestingPug
|
|
72
136
|
# @used_in {Chains#destroy_chain}
|
73
137
|
# @used_in {Chains#show_chain}
|
74
138
|
# @use {#subject_model}
|
139
|
+
# @example
|
140
|
+
# class BooksController < ApplicationController
|
141
|
+
# include RestingPug::Base
|
142
|
+
#
|
143
|
+
# private
|
144
|
+
# # Override a fetch_subject to return a default subject if requested is not found
|
145
|
+
# def fetch_subject
|
146
|
+
# @subject = subject_model.find_by_id(params[:id]) || default_subject
|
147
|
+
# end
|
148
|
+
# end
|
75
149
|
def fetch_subject
|
76
150
|
@subject = subject_model.find(params[:id])
|
77
151
|
end
|
@@ -80,6 +154,16 @@ module RestingPug
|
|
80
154
|
# @note Override this method to add some custom logic to fetching process.
|
81
155
|
# @used_in {Chains#index_chain}
|
82
156
|
# @use {#subject_model}
|
157
|
+
# @example
|
158
|
+
# class BooksController < ApplicationController
|
159
|
+
# include RestingPug::Base
|
160
|
+
#
|
161
|
+
# private
|
162
|
+
# # Override a fetch_subjects to return subject where title is not 'None'
|
163
|
+
# def fetch_subjects
|
164
|
+
# @subjects = subject_model.where.not(title: 'None')
|
165
|
+
# end
|
166
|
+
# end
|
83
167
|
def fetch_subjects
|
84
168
|
@subjects = subject_model.all
|
85
169
|
end
|
data/lib/resting_pug/version.rb
CHANGED
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resting_pug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Slava Korolev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: will_paginate
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '1.8'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: codecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
description: Resting Pug allows you to create a JSON API with just adding one line
|
112
126
|
to your controller and fully customize it with overriding small and simple helper
|
113
127
|
methods.
|