simple_resource_controller 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -3
- data/lib/simple_resource_controller/controller/actions.rb +27 -1
- data/lib/simple_resource_controller/controller/implementation/format/api.rb +23 -4
- data/lib/simple_resource_controller/controller/implementation/format/html.rb +6 -0
- data/lib/simple_resource_controller/controller/implementation.rb +8 -16
- data/lib/simple_resource_controller/controller.rb +0 -2
- data/lib/simple_resource_controller/railtie.rb +2 -0
- data/lib/simple_resource_controller/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41e99ec931e6de3bf1a4d68cb23fe2357e8671c97ebbcf1bf7aecdb001bbc1f5
|
4
|
+
data.tar.gz: 1d18e94778a8f51b3a7bfe404e3a91b07a6889df7b632968e5735a34050d3e26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 020cfb5e4304c2f7527eecee4f62973d320837fc725a0d8b503440c674e67f1d88eac61bfb8553da6f663434f737787e10dcdc2560686e83949ee4b2e0f8ffac
|
7
|
+
data.tar.gz: cac6d60eff27e7688dc685a0f2193f196a47e4bc36ee05de6eeb4131d1943e83671e4157a2d565bcb90d13aa83ed2dc9339a1355596f541ac5f51417f49d91d3
|
data/README.md
CHANGED
@@ -70,7 +70,16 @@ Gem suppports [jbuilder](https://github.com/rails/jbuilder) and [activemodel_ser
|
|
70
70
|
#### JBuilder
|
71
71
|
|
72
72
|
```ruby
|
73
|
-
|
73
|
+
class AnotherArticlesController < ApplicationController
|
74
|
+
resource_actions :crud
|
75
|
+
resource_api jbuilder: true
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def permitted_params
|
80
|
+
params.require(:article).permit(:title)
|
81
|
+
end
|
82
|
+
end
|
74
83
|
```
|
75
84
|
|
76
85
|
But you will also need to add all view files for your actions, including `new` and `edit`.
|
@@ -82,13 +91,54 @@ But you will also need to add all view files for your actions, including `new` a
|
|
82
91
|
Minimum config:
|
83
92
|
|
84
93
|
```ruby
|
85
|
-
|
94
|
+
class AnotherArticlesController < ApplicationController
|
95
|
+
resource_actions :crud
|
96
|
+
resource_api activemodel_serializer: { }
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def permitted_params
|
101
|
+
params.require(:article).permit(:title)
|
102
|
+
end
|
103
|
+
end
|
86
104
|
```
|
87
105
|
|
88
106
|
All options:
|
89
107
|
|
90
108
|
```ruby
|
91
|
-
|
109
|
+
class AnotherArticlesController < ApplicationController
|
110
|
+
resource_actions :crud
|
111
|
+
resource_api activemodel_serializer: { collection_serializer: MyCollectionSerializer, resource_serializer: MyArticleSerializer, error_serializer: MyErrorSerializer }
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def permitted_params
|
116
|
+
params.require(:article).permit(:title)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
With redefined options:
|
122
|
+
|
123
|
+
**Important note: you should always set the error_serializer**
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
class AnotherArticlesController < ApplicationController
|
127
|
+
resource_actions :crud
|
128
|
+
resource_api activemodel_serializer: { collection_serializer: MyCollectionSerializer, resource_serializer: MyArticleSerializer, error_serializer: MyErrorSerializer }
|
129
|
+
|
130
|
+
# will use the AnotherArticleSerializer when success
|
131
|
+
# will use the MyErrorSerializer when failure
|
132
|
+
def update
|
133
|
+
update! serializer: AnotherArticleSerializer
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
def permitted_params
|
139
|
+
params.require(:article).permit(:title)
|
140
|
+
end
|
141
|
+
end
|
92
142
|
```
|
93
143
|
|
94
144
|
**You can find example inside tests.**
|
@@ -3,6 +3,14 @@ module SimpleResourceController
|
|
3
3
|
module Actions
|
4
4
|
module Index
|
5
5
|
def index(options={}, &block)
|
6
|
+
unless block_given?
|
7
|
+
if current_controller_api?
|
8
|
+
api_before_index_response_callback(options)
|
9
|
+
else
|
10
|
+
html_before_index_response_callback(options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
6
14
|
respond_with collection, options, &block
|
7
15
|
end
|
8
16
|
alias :index! :index
|
@@ -10,6 +18,14 @@ module SimpleResourceController
|
|
10
18
|
|
11
19
|
module Show
|
12
20
|
def show(options={}, &block)
|
21
|
+
unless block_given?
|
22
|
+
if current_controller_api?
|
23
|
+
api_before_show_response_callback(options)
|
24
|
+
else
|
25
|
+
html_before_show_response_callback(options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
13
29
|
respond_with resource, options, &block
|
14
30
|
end
|
15
31
|
alias :show! :show
|
@@ -47,7 +63,17 @@ module SimpleResourceController
|
|
47
63
|
|
48
64
|
module Destroy
|
49
65
|
def destroy(options={}, &block)
|
50
|
-
|
66
|
+
resource.destroy
|
67
|
+
|
68
|
+
unless block_given?
|
69
|
+
if current_controller_api?
|
70
|
+
api_before_destroy_response_callback(options)
|
71
|
+
else
|
72
|
+
html_before_destroy_response_callback(options)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
respond_with resource, options, &block
|
51
77
|
end
|
52
78
|
alias :destroy! :destroy
|
53
79
|
end
|
@@ -5,13 +5,32 @@ module SimpleResourceController
|
|
5
5
|
module Api
|
6
6
|
private
|
7
7
|
|
8
|
+
def api_before_index_response_callback(options)
|
9
|
+
if activemodel_serializer?
|
10
|
+
resource_serializer = self.class.api_config.dig(:activemodel_serializer, :resource_serializer)
|
11
|
+
collection_serializer = self.class.api_config.dig(:activemodel_serializer, :collection_serializer)
|
12
|
+
|
13
|
+
options[:json] ||= collection
|
14
|
+
options[:each_serializer] ||= resource_serializer if resource_serializer.present?
|
15
|
+
options[:serializer] ||= collection_serializer if collection_serializer.present?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def api_before_show_response_callback(options)
|
20
|
+
if activemodel_serializer?
|
21
|
+
resource_serializer = self.class.api_config.dig(:activemodel_serializer, :resource_serializer)
|
22
|
+
options[:json] ||= resource
|
23
|
+
options[:serializer] ||= resource_serializer if resource_serializer.present?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
8
27
|
def api_before_save_success_response_callback(options)
|
9
28
|
options[:status] ||= :created if action_name == "create"
|
10
29
|
|
11
30
|
if activemodel_serializer?
|
12
|
-
|
31
|
+
resource_serializer = self.class.api_config.dig(:activemodel_serializer, :resource_serializer)
|
13
32
|
options[:json] ||= resource
|
14
|
-
options[:serializer] ||=
|
33
|
+
options[:serializer] ||= resource_serializer if resource_serializer.present?
|
15
34
|
end
|
16
35
|
end
|
17
36
|
|
@@ -27,9 +46,9 @@ module SimpleResourceController
|
|
27
46
|
|
28
47
|
def api_before_destroy_response_callback(options)
|
29
48
|
if activemodel_serializer?
|
30
|
-
|
49
|
+
resource_serializer = self.class.api_config.dig(:activemodel_serializer, :resource_serializer)
|
31
50
|
options[:json] ||= resource
|
32
|
-
options[:serializer] ||=
|
51
|
+
options[:serializer] ||= resource_serializer if resource_serializer.present?
|
33
52
|
end
|
34
53
|
end
|
35
54
|
|
@@ -5,6 +5,12 @@ module SimpleResourceController
|
|
5
5
|
module Html
|
6
6
|
private
|
7
7
|
|
8
|
+
def html_before_index_response_callback(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def html_before_show_response_callback(options)
|
12
|
+
end
|
13
|
+
|
8
14
|
def html_before_save_success_response_callback(options)
|
9
15
|
options[:location] ||= after_save_redirect_path
|
10
16
|
setup_flash_messages(after_create_messages) if action_name == "create"
|
@@ -96,31 +96,23 @@ module SimpleResourceController
|
|
96
96
|
def build_resource
|
97
97
|
return instance_variable_get(:"@#{resource_name}") if instance_variable_get(:"@#{resource_name}").present?
|
98
98
|
|
99
|
-
|
100
|
-
|
99
|
+
instance = association_chain.is_a?(ActiveRecord::Relation) ? association_chain.build : association_chain.new
|
100
|
+
instance.assign_attributes(permitted_params) if action_name == "create"
|
101
|
+
|
102
|
+
instance_variable_set(:"@#{resource_name}", instance)
|
101
103
|
end
|
102
104
|
|
103
105
|
def resource
|
104
106
|
return instance_variable_get(:"@#{resource_name}") if instance_variable_get(:"@#{resource_name}").present?
|
105
|
-
instance_variable_set(:"@#{resource_name}", association_chain.find(params[:id]))
|
106
|
-
end
|
107
107
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
unless block_given?
|
112
|
-
if current_controller_api?
|
113
|
-
api_before_destroy_response_callback(options)
|
114
|
-
else
|
115
|
-
html_before_destroy_response_callback(options)
|
116
|
-
end
|
117
|
-
end
|
108
|
+
instance = association_chain.find(params[:id])
|
109
|
+
instance.assign_attributes(permitted_params) if action_name == "update"
|
118
110
|
|
119
|
-
|
111
|
+
instance_variable_set(:"@#{resource_name}", instance)
|
120
112
|
end
|
121
113
|
|
122
114
|
def save_resource_and_respond!(options={}, &block)
|
123
|
-
success = resource.
|
115
|
+
success = resource.save
|
124
116
|
|
125
117
|
# process not saved result because of failed callback or another ActiveRecord magic
|
126
118
|
if resource.valid? && !success
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module SimpleResourceController
|
2
2
|
require 'rails'
|
3
|
+
require_relative './controller/config'
|
3
4
|
|
4
5
|
class Railtie < Rails::Railtie
|
5
6
|
initializer 'insert SimpleResourceController to ActionController' do
|
@@ -13,6 +14,7 @@ module SimpleResourceController
|
|
13
14
|
def self.insert
|
14
15
|
if defined?(::ActionController)
|
15
16
|
::ActionController::Base.extend(Configurator)
|
17
|
+
::ActionController::Base.extend(SimpleResourceController::Controller::Config)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|