simple_resource_controller 0.1.7 → 0.2.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 +4 -4
- data/README.md +35 -0
- data/lib/simple_resource_controller/controller.rb +42 -9
- data/lib/simple_resource_controller/version.rb +1 -1
- data/simple_resource_controller.gemspec +2 -0
- metadata +29 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 11ecf5e2bc0b286fa0709a401ed41a7a204a15cd0cc54e4379a31ae70c77d492
|
|
4
|
+
data.tar.gz: d382ef1a1b0752b8cd8e5e23dcb247e9bfe485d01af6b4149765a0fa26768cd7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 19582cab4d75ae48b61f42d92c3a9485949cd470f9e900b5565d9bcc92e21a8f5dd5c6e8faa9786429b751e0ee8fca2a9c8fa4f8f2b281e58dcf1948c3a0b922
|
|
7
|
+
data.tar.gz: aedab8f8ca87baac41c871668af80e392c7f064c7d27a4429e3864db17fb7bd226afa5a2b9f56f0259852c77aab2dd3a47cc066c4070552a790f5aff2f5c9502
|
data/README.md
CHANGED
|
@@ -63,6 +63,36 @@ class AnotherArticlesController < ApplicationController
|
|
|
63
63
|
end
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
+
### API examples
|
|
67
|
+
|
|
68
|
+
Gem suppports [jbuilder](https://github.com/rails/jbuilder) and [activemodel_serializer](https://github.com/rails-api/active_model_serializers) as API serializers
|
|
69
|
+
|
|
70
|
+
#### JBuilder
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
resource_api jbuilder: true
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
But you will also need to add all view files for your actions, including `new` and `edit`.
|
|
77
|
+
|
|
78
|
+
**You can find example inside tests.**
|
|
79
|
+
|
|
80
|
+
#### ActiveModel::Serializer
|
|
81
|
+
|
|
82
|
+
Minimum config:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
resource_api activemodel_serializer: { }
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
All options:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
resource_api activemodel_serializer: { record_serializer: MyArticleSerializer, error_serializer: MyErrorSerializer }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**You can find example inside tests.**
|
|
95
|
+
|
|
66
96
|
### Controller configuration
|
|
67
97
|
|
|
68
98
|
The first required configuration is an action name.
|
|
@@ -394,6 +424,11 @@ This is my favorite one. The `responders` gem follows the next logic - if a reco
|
|
|
394
424
|
If you will get this exception - check your ActiveRecord callbacks, your code has some smells.
|
|
395
425
|
|
|
396
426
|
|
|
427
|
+
#### `error_serializer should be configured for the activemodel API`
|
|
428
|
+
|
|
429
|
+
You are trying to use the `resource_api` DSL to setup the activemodel_serializer, faced with the validation error in create or update action.
|
|
430
|
+
But didn't configured an error serializer: `resource_api activemodel_serializer: { error_serializer: MyErrorSerializer }`
|
|
431
|
+
|
|
397
432
|
## Contributing
|
|
398
433
|
|
|
399
434
|
Bug reports and pull requests are welcome on GitHub at https://github.com/c3gdlk/simple_resource_controller. This project is intended to be a safe, welcoming space for collaboration.
|
|
@@ -26,8 +26,7 @@ module SimpleResourceController
|
|
|
26
26
|
def create(options={}, &block)
|
|
27
27
|
build_resource
|
|
28
28
|
success = save_resource_and_respond!(options, &block)
|
|
29
|
-
|
|
30
|
-
setup_flash_messages(after_create_messages) if success
|
|
29
|
+
setup_flash_messages(after_create_messages) if success && !current_controller_api?
|
|
31
30
|
end
|
|
32
31
|
alias :create! :create
|
|
33
32
|
end
|
|
@@ -42,8 +41,7 @@ module SimpleResourceController
|
|
|
42
41
|
module Update
|
|
43
42
|
def update(options={}, &block)
|
|
44
43
|
success = save_resource_and_respond!(options, &block)
|
|
45
|
-
|
|
46
|
-
setup_flash_messages(after_update_messages) if success
|
|
44
|
+
setup_flash_messages(after_update_messages) if success && !current_controller_api?
|
|
47
45
|
end
|
|
48
46
|
alias :update! :update
|
|
49
47
|
end
|
|
@@ -52,7 +50,7 @@ module SimpleResourceController
|
|
|
52
50
|
def destroy(options={}, &block)
|
|
53
51
|
destroy_resource_and_respond!(options, &block)
|
|
54
52
|
ensure
|
|
55
|
-
setup_flash_messages(after_destroy_messages)
|
|
53
|
+
setup_flash_messages(after_destroy_messages) unless current_controller_api?
|
|
56
54
|
end
|
|
57
55
|
alias :destroy! :destroy
|
|
58
56
|
end
|
|
@@ -60,6 +58,20 @@ module SimpleResourceController
|
|
|
60
58
|
module CommonMethods
|
|
61
59
|
private
|
|
62
60
|
|
|
61
|
+
def current_controller_api?
|
|
62
|
+
self.class.api_config.present?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def activemodel_serializer?
|
|
66
|
+
current_controller_api? && self.class.api_config.dig(:activemodel_serializer).present?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def render_resource_with_activemodel_serializer(options)
|
|
70
|
+
record_serializer = self.class.api_config.dig(:activemodel_serializer, :record_serializer)
|
|
71
|
+
|
|
72
|
+
render({ json: resource, status: :ok }.merge(record_serializer ? { serializer: record_serializer } : {} ).merge(options))
|
|
73
|
+
end
|
|
74
|
+
|
|
63
75
|
def collection
|
|
64
76
|
return instance_variable_get(:"@#{collection_name}") if instance_variable_get(:"@#{collection_name}").present?
|
|
65
77
|
instance_variable_set(:"@#{collection_name}", apply_scopes_and_pagination(association_chain))
|
|
@@ -159,11 +171,15 @@ module SimpleResourceController
|
|
|
159
171
|
def destroy_resource_and_respond!(options={}, &block)
|
|
160
172
|
resource.destroy
|
|
161
173
|
|
|
162
|
-
unless block_given? || options[:location].present?
|
|
174
|
+
unless block_given? || options[:location].present? || current_controller_api?
|
|
163
175
|
options[:location] = after_destroy_redirect_path
|
|
164
176
|
end
|
|
165
177
|
|
|
166
|
-
|
|
178
|
+
if activemodel_serializer?
|
|
179
|
+
render_resource_with_activemodel_serializer(options)
|
|
180
|
+
else
|
|
181
|
+
respond_with resource, options, &block
|
|
182
|
+
end
|
|
167
183
|
end
|
|
168
184
|
|
|
169
185
|
def save_resource_and_respond!(options={}, &block)
|
|
@@ -175,12 +191,21 @@ module SimpleResourceController
|
|
|
175
191
|
end
|
|
176
192
|
|
|
177
193
|
if result.present?
|
|
178
|
-
unless block_given? || options[:location].present?
|
|
194
|
+
unless block_given? || options[:location].present? || current_controller_api?
|
|
179
195
|
options[:location] = after_save_redirect_path
|
|
180
196
|
end
|
|
197
|
+
elsif activemodel_serializer?
|
|
198
|
+
error_serializer = self.class.api_config.dig(:activemodel_serializer, :error_serializer)
|
|
199
|
+
|
|
200
|
+
raise "error_serializer should be configured for the activemodel API" unless error_serializer.present?
|
|
201
|
+
options[:serializer] = error_serializer
|
|
181
202
|
end
|
|
182
203
|
|
|
183
|
-
|
|
204
|
+
if result.present? && activemodel_serializer?
|
|
205
|
+
render_resource_with_activemodel_serializer(options)
|
|
206
|
+
else
|
|
207
|
+
respond_with resource, options, &block
|
|
208
|
+
end
|
|
184
209
|
|
|
185
210
|
result
|
|
186
211
|
end
|
|
@@ -258,6 +283,14 @@ module SimpleResourceController
|
|
|
258
283
|
def paginate_collection(value)
|
|
259
284
|
@paginate_collection_config = value
|
|
260
285
|
end
|
|
286
|
+
|
|
287
|
+
def api_config
|
|
288
|
+
@api_config
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def resource_api(value)
|
|
292
|
+
@api_config = value
|
|
293
|
+
end
|
|
261
294
|
end
|
|
262
295
|
|
|
263
296
|
class ActionsBuilder
|
|
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
|
|
|
22
22
|
spec.add_development_dependency "bundler"
|
|
23
23
|
spec.add_development_dependency "rake"
|
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
|
25
|
+
spec.add_development_dependency "jbuilder"
|
|
26
|
+
spec.add_development_dependency 'active_model_serializers', '~> 0.10.0'
|
|
25
27
|
|
|
26
28
|
spec.add_dependency "railties"
|
|
27
29
|
spec.add_dependency "actionpack"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_resource_controller
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Oleg Zaporozhchenko
|
|
@@ -52,6 +52,34 @@ dependencies:
|
|
|
52
52
|
- - "~>"
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: jbuilder
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: active_model_serializers
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.10.0
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.10.0
|
|
55
83
|
- !ruby/object:Gem::Dependency
|
|
56
84
|
name: railties
|
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|