simple_resource_controller 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_resource_controller/configurator.rb +1 -1
- data/lib/simple_resource_controller/controller/actions.rb +56 -0
- data/lib/simple_resource_controller/controller/config.rb +37 -0
- data/lib/simple_resource_controller/controller/implementation/format/api.rb +47 -0
- data/lib/simple_resource_controller/controller/implementation/format/html.rb +63 -0
- data/lib/simple_resource_controller/controller/implementation.rb +160 -0
- data/lib/simple_resource_controller/controller.rb +30 -320
- data/lib/simple_resource_controller/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8362241a63c88604c893743e4dddc6e87ef4f51c036ac2f3d597af0a1857099d
|
4
|
+
data.tar.gz: 0f439681b0637be16b0f1194bf4545cc93b3cf10bdd8044502ff69470c967b7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a20db372c476b423a58774cbb55fb0bb0b6ee518cc9bec64c94f4e405ba0f4a7617953971f4d3e7d1bacb40e8524ce3dd5c61fffea8ba05cf137d9b8fc284007
|
7
|
+
data.tar.gz: a93645e48f00c63fd7282dc7b8139532d7659197c7826e507e6b98bfc8202e20aa89dcecca5f03cad98cd1686a63bb091153d606a362a844fbd92b8180c440cc
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module SimpleResourceController
|
2
|
+
module Controller
|
3
|
+
module Actions
|
4
|
+
module Index
|
5
|
+
def index(options={}, &block)
|
6
|
+
respond_with collection, options, &block
|
7
|
+
end
|
8
|
+
alias :index! :index
|
9
|
+
end
|
10
|
+
|
11
|
+
module Show
|
12
|
+
def show(options={}, &block)
|
13
|
+
respond_with resource, options, &block
|
14
|
+
end
|
15
|
+
alias :show! :show
|
16
|
+
end
|
17
|
+
|
18
|
+
module New
|
19
|
+
def new(options={}, &block)
|
20
|
+
build_resource
|
21
|
+
respond_with resource, options, &block
|
22
|
+
end
|
23
|
+
alias :new! :new
|
24
|
+
end
|
25
|
+
|
26
|
+
module Create
|
27
|
+
def create(options={}, &block)
|
28
|
+
build_resource
|
29
|
+
save_resource_and_respond!(options, &block)
|
30
|
+
end
|
31
|
+
alias :create! :create
|
32
|
+
end
|
33
|
+
|
34
|
+
module Edit
|
35
|
+
def edit(options={}, &block)
|
36
|
+
respond_with resource, options, &block
|
37
|
+
end
|
38
|
+
alias :edit! :edit
|
39
|
+
end
|
40
|
+
|
41
|
+
module Update
|
42
|
+
def update(options={}, &block)
|
43
|
+
save_resource_and_respond!(options, &block)
|
44
|
+
end
|
45
|
+
alias :update! :update
|
46
|
+
end
|
47
|
+
|
48
|
+
module Destroy
|
49
|
+
def destroy(options={}, &block)
|
50
|
+
destroy_resource_and_respond!(options, &block)
|
51
|
+
end
|
52
|
+
alias :destroy! :destroy
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module SimpleResourceController
|
2
|
+
module Controller
|
3
|
+
module Config
|
4
|
+
def resource_class_config
|
5
|
+
@resource_class_config
|
6
|
+
end
|
7
|
+
|
8
|
+
def resource_class(value)
|
9
|
+
@resource_class_config = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def resource_context_config
|
13
|
+
@resource_context_config
|
14
|
+
end
|
15
|
+
|
16
|
+
def resource_context(*values)
|
17
|
+
@resource_context_config = values
|
18
|
+
end
|
19
|
+
|
20
|
+
def paginate_collection_config
|
21
|
+
@paginate_collection_config
|
22
|
+
end
|
23
|
+
|
24
|
+
def paginate_collection(value)
|
25
|
+
@paginate_collection_config = value
|
26
|
+
end
|
27
|
+
|
28
|
+
def api_config
|
29
|
+
@api_config
|
30
|
+
end
|
31
|
+
|
32
|
+
def resource_api(value)
|
33
|
+
@api_config = value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module SimpleResourceController
|
2
|
+
module Controller
|
3
|
+
module Implementation
|
4
|
+
module Format
|
5
|
+
module Api
|
6
|
+
private
|
7
|
+
|
8
|
+
def api_before_save_success_response_callback(options)
|
9
|
+
options[:status] ||= :created if action_name == "create"
|
10
|
+
|
11
|
+
if activemodel_serializer?
|
12
|
+
record_serializer = self.class.api_config.dig(:activemodel_serializer, :record_serializer)
|
13
|
+
options[:json] ||= resource
|
14
|
+
options[:serializer] ||= record_serializer
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def api_before_save_failure_response_callback(options)
|
19
|
+
if activemodel_serializer?
|
20
|
+
error_serializer = self.class.api_config.dig(:activemodel_serializer, :error_serializer)
|
21
|
+
|
22
|
+
raise "error_serializer should be configured for the activemodel API" unless error_serializer.present?
|
23
|
+
options[:json] ||= resource
|
24
|
+
options[:serializer] = error_serializer
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def api_before_destroy_response_callback(options)
|
29
|
+
if activemodel_serializer?
|
30
|
+
record_serializer = self.class.api_config.dig(:activemodel_serializer, :record_serializer)
|
31
|
+
options[:json] ||= resource
|
32
|
+
options[:serializer] ||= record_serializer
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def current_controller_api?
|
37
|
+
self.class.api_config.present?
|
38
|
+
end
|
39
|
+
|
40
|
+
def activemodel_serializer?
|
41
|
+
current_controller_api? && self.class.api_config.dig(:activemodel_serializer).present?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module SimpleResourceController
|
2
|
+
module Controller
|
3
|
+
module Implementation
|
4
|
+
module Format
|
5
|
+
module Html
|
6
|
+
private
|
7
|
+
|
8
|
+
def html_before_save_success_response_callback(options)
|
9
|
+
options[:location] ||= after_save_redirect_path
|
10
|
+
setup_flash_messages(after_create_messages) if action_name == "create"
|
11
|
+
setup_flash_messages(after_update_messages) if action_name == "update"
|
12
|
+
end
|
13
|
+
|
14
|
+
def api_before_save_failure_response_callback(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def html_before_destroy_response_callback(options)
|
18
|
+
options[:location] ||= after_destroy_redirect_path
|
19
|
+
setup_flash_messages(after_destroy_messages)
|
20
|
+
end
|
21
|
+
|
22
|
+
# :nocov:
|
23
|
+
def after_save_redirect_path
|
24
|
+
raise NotImplementedError, "Need to implement the after_save_redirect_path method"
|
25
|
+
end
|
26
|
+
# :nocov:
|
27
|
+
|
28
|
+
# :nocov:
|
29
|
+
def after_destroy_redirect_path
|
30
|
+
raise NotImplementedError, "Need to implement the after_destroy_redirect_path method"
|
31
|
+
end
|
32
|
+
# :nocov:
|
33
|
+
|
34
|
+
def after_create_messages
|
35
|
+
after_save_messages
|
36
|
+
end
|
37
|
+
|
38
|
+
def after_update_messages
|
39
|
+
after_save_messages
|
40
|
+
end
|
41
|
+
|
42
|
+
def after_destroy_messages
|
43
|
+
nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def after_save_messages
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
|
50
|
+
def setup_flash_messages(messages)
|
51
|
+
return unless messages.present?
|
52
|
+
|
53
|
+
raise 'Messages should be specified with Hash' unless messages.is_a?(Hash)
|
54
|
+
|
55
|
+
messages.each do |key, message|
|
56
|
+
flash[key] = message
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require_relative './implementation/format/api'
|
2
|
+
require_relative './implementation/format/html'
|
3
|
+
|
4
|
+
module SimpleResourceController
|
5
|
+
module Controller
|
6
|
+
module Implementation
|
7
|
+
include Format::Api
|
8
|
+
include Format::Html
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def collection
|
13
|
+
return instance_variable_get(:"@#{collection_name}") if instance_variable_get(:"@#{collection_name}").present?
|
14
|
+
instance_variable_set(:"@#{collection_name}", apply_scopes_and_pagination(association_chain))
|
15
|
+
end
|
16
|
+
|
17
|
+
def apply_scopes_and_pagination(chain)
|
18
|
+
if respond_to?(:apply_scopes, true)
|
19
|
+
chain = apply_scopes(chain)
|
20
|
+
end
|
21
|
+
|
22
|
+
per_page = self.class.paginate_collection_config
|
23
|
+
|
24
|
+
if per_page.present?
|
25
|
+
raise 'Please install Kaminari' unless defined?(::Kaminari)
|
26
|
+
|
27
|
+
chain = chain.page(params[:page]).per(params[:per_page] || per_page)
|
28
|
+
else
|
29
|
+
chain = chain.all
|
30
|
+
end
|
31
|
+
|
32
|
+
chain
|
33
|
+
end
|
34
|
+
|
35
|
+
def collection_name
|
36
|
+
resource_name.pluralize
|
37
|
+
end
|
38
|
+
|
39
|
+
def resource_class_name
|
40
|
+
self.class.resource_class_config || self.class.name.split('::').last.gsub('Controller', '').singularize
|
41
|
+
end
|
42
|
+
|
43
|
+
def resource_class
|
44
|
+
resource_class_name.constantize
|
45
|
+
end
|
46
|
+
|
47
|
+
def resource_name
|
48
|
+
resource_class_name.split('::').last.underscore
|
49
|
+
end
|
50
|
+
|
51
|
+
def resource_relation_name
|
52
|
+
resource_class_name.underscore.pluralize
|
53
|
+
end
|
54
|
+
|
55
|
+
def association_chain
|
56
|
+
self.class.resource_context_config.present? ? association_chain_by_context : resource_class
|
57
|
+
end
|
58
|
+
|
59
|
+
def association_chain_by_context
|
60
|
+
specified_scopes.reduce(default_context) do |context, scope|
|
61
|
+
raise "#{context.inspect} hasn't relation #{scope}" unless context.respond_to? scope
|
62
|
+
|
63
|
+
context.public_send(scope)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def default_context
|
68
|
+
context_method = self.class.resource_context_config.first
|
69
|
+
|
70
|
+
if self.respond_to? context_method, true
|
71
|
+
context = send(context_method)
|
72
|
+
elsif params["#{context_method}_id"].present?
|
73
|
+
class_name = context_method.to_s.classify
|
74
|
+
context_class = class_name.safe_constantize
|
75
|
+
|
76
|
+
if context_class.present?
|
77
|
+
context = context_class.find(params["#{context_method}_id"])
|
78
|
+
else
|
79
|
+
raise "Could not find model #{class_name} by param #{context_method}_id"
|
80
|
+
end
|
81
|
+
else
|
82
|
+
raise "Undefined context method #{context_method}"
|
83
|
+
end
|
84
|
+
|
85
|
+
context
|
86
|
+
end
|
87
|
+
|
88
|
+
def specified_scopes
|
89
|
+
if self.class.resource_context_config.size > 1
|
90
|
+
self.class.resource_context_config[1..-1]
|
91
|
+
else
|
92
|
+
[resource_relation_name.to_sym]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def build_resource
|
97
|
+
return instance_variable_get(:"@#{resource_name}") if instance_variable_get(:"@#{resource_name}").present?
|
98
|
+
|
99
|
+
new_instance = association_chain.is_a?(ActiveRecord::Relation) ? association_chain.build : association_chain.new
|
100
|
+
instance_variable_set(:"@#{resource_name}", new_instance)
|
101
|
+
end
|
102
|
+
|
103
|
+
def resource
|
104
|
+
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
|
+
|
108
|
+
def destroy_resource_and_respond!(options={}, &block)
|
109
|
+
resource.destroy
|
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
|
118
|
+
|
119
|
+
respond_with resource, options, &block
|
120
|
+
end
|
121
|
+
|
122
|
+
def save_resource_and_respond!(options={}, &block)
|
123
|
+
success = resource.update(permitted_params)
|
124
|
+
|
125
|
+
# process not saved result because of failed callback or another ActiveRecord magic
|
126
|
+
if resource.valid? && !success
|
127
|
+
resource.errors[:base] << resource_wasnt_saved_message
|
128
|
+
end
|
129
|
+
|
130
|
+
unless block_given?
|
131
|
+
if success
|
132
|
+
if current_controller_api?
|
133
|
+
api_before_save_success_response_callback(options)
|
134
|
+
else
|
135
|
+
html_before_save_success_response_callback(options)
|
136
|
+
end
|
137
|
+
else
|
138
|
+
if current_controller_api?
|
139
|
+
api_before_save_failure_response_callback(options)
|
140
|
+
else
|
141
|
+
api_before_save_failure_response_callback(options)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
respond_with resource, options, &block
|
147
|
+
end
|
148
|
+
|
149
|
+
def resource_wasnt_saved_message
|
150
|
+
raise 'Too many Rails magic. Please check your code'
|
151
|
+
end
|
152
|
+
|
153
|
+
# :nocov:
|
154
|
+
def permitted_params
|
155
|
+
raise NotImplementedError, "Need to implement the permitted_params method"
|
156
|
+
end
|
157
|
+
# :nocov:
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -1,338 +1,48 @@
|
|
1
|
+
require_relative './controller/actions'
|
2
|
+
require_relative './controller/config'
|
3
|
+
require_relative './controller/implementation'
|
4
|
+
|
1
5
|
module SimpleResourceController
|
2
6
|
module Controller
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
respond_with resource, options, &block
|
13
|
-
end
|
14
|
-
alias :show! :show
|
15
|
-
end
|
16
|
-
|
17
|
-
module New
|
18
|
-
def new(options={}, &block)
|
19
|
-
build_resource
|
20
|
-
respond_with resource, options, &block
|
21
|
-
end
|
22
|
-
alias :new! :new
|
23
|
-
end
|
24
|
-
|
25
|
-
module Create
|
26
|
-
def create(options={}, &block)
|
27
|
-
build_resource
|
28
|
-
success = save_resource_and_respond!(options, &block)
|
29
|
-
setup_flash_messages(after_create_messages) if success && !current_controller_api?
|
30
|
-
end
|
31
|
-
alias :create! :create
|
32
|
-
end
|
33
|
-
|
34
|
-
module Edit
|
35
|
-
def edit(options={}, &block)
|
36
|
-
respond_with resource, options, &block
|
37
|
-
end
|
38
|
-
alias :edit! :edit
|
39
|
-
end
|
40
|
-
|
41
|
-
module Update
|
42
|
-
def update(options={}, &block)
|
43
|
-
success = save_resource_and_respond!(options, &block)
|
44
|
-
setup_flash_messages(after_update_messages) if success && !current_controller_api?
|
45
|
-
end
|
46
|
-
alias :update! :update
|
47
|
-
end
|
48
|
-
|
49
|
-
module Destroy
|
50
|
-
def destroy(options={}, &block)
|
51
|
-
destroy_resource_and_respond!(options, &block)
|
52
|
-
ensure
|
53
|
-
setup_flash_messages(after_destroy_messages) unless current_controller_api?
|
54
|
-
end
|
55
|
-
alias :destroy! :destroy
|
56
|
-
end
|
57
|
-
|
58
|
-
module CommonMethods
|
59
|
-
private
|
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
|
-
|
75
|
-
def collection
|
76
|
-
return instance_variable_get(:"@#{collection_name}") if instance_variable_get(:"@#{collection_name}").present?
|
77
|
-
instance_variable_set(:"@#{collection_name}", apply_scopes_and_pagination(association_chain))
|
78
|
-
end
|
79
|
-
|
80
|
-
def apply_scopes_and_pagination(chain)
|
81
|
-
if respond_to?(:apply_scopes, true)
|
82
|
-
chain = apply_scopes(chain)
|
83
|
-
end
|
84
|
-
|
85
|
-
per_page = self.class.paginate_collection_config
|
86
|
-
|
87
|
-
if per_page.present?
|
88
|
-
raise 'Please install Kaminari' unless defined?(Kaminari)
|
89
|
-
|
90
|
-
chain = chain.page(params[:page]).per(params[:per_page] || per_page)
|
91
|
-
else
|
92
|
-
chain = chain.all
|
93
|
-
end
|
94
|
-
|
95
|
-
chain
|
96
|
-
end
|
97
|
-
|
98
|
-
def collection_name
|
99
|
-
resource_name.pluralize
|
100
|
-
end
|
101
|
-
|
102
|
-
def resource_class_name
|
103
|
-
self.class.resource_class_config || self.class.name.split('::').last.gsub('Controller', '').singularize
|
104
|
-
end
|
105
|
-
|
106
|
-
def resource_class
|
107
|
-
resource_class_name.constantize
|
108
|
-
end
|
109
|
-
|
110
|
-
def resource_name
|
111
|
-
resource_class_name.split('::').last.underscore
|
112
|
-
end
|
113
|
-
|
114
|
-
def resource_relation_name
|
115
|
-
resource_class_name.underscore.pluralize
|
116
|
-
end
|
117
|
-
|
118
|
-
def association_chain
|
119
|
-
self.class.resource_context_config.present? ? association_chain_by_context : resource_class
|
120
|
-
end
|
7
|
+
DEPENDENCIES_MAP = {
|
8
|
+
index: Actions::Index,
|
9
|
+
show: Actions::Show,
|
10
|
+
new: Actions::New,
|
11
|
+
create: Actions::Create,
|
12
|
+
edit: Actions::Edit,
|
13
|
+
update: Actions::Update,
|
14
|
+
destroy: Actions::Destroy
|
15
|
+
}.freeze
|
121
16
|
|
122
|
-
|
123
|
-
specified_scopes.reduce(default_context) do |context, scope|
|
124
|
-
raise "#{context.inspect} hasn't relation #{scope}" unless context.respond_to? scope
|
17
|
+
HELPER_METHODS = [:resource, :collection].freeze
|
125
18
|
|
126
|
-
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def default_context
|
131
|
-
context_method = self.class.resource_context_config.first
|
132
|
-
|
133
|
-
if self.respond_to? context_method, true
|
134
|
-
context = send(context_method)
|
135
|
-
elsif params["#{context_method}_id"].present?
|
136
|
-
class_name = context_method.to_s.classify
|
137
|
-
context_class = class_name.safe_constantize
|
138
|
-
|
139
|
-
if context_class.present?
|
140
|
-
context = context_class.find(params["#{context_method}_id"])
|
141
|
-
else
|
142
|
-
raise "Could not find model #{class_name} by param #{context_method}_id"
|
143
|
-
end
|
144
|
-
else
|
145
|
-
raise "Undefined context method #{context_method}"
|
146
|
-
end
|
19
|
+
ALL_ACTIONS_ALIAS = :crud
|
147
20
|
|
148
|
-
|
21
|
+
def self.build(controller_class, actions)
|
22
|
+
unless actions.include?(ALL_ACTIONS_ALIAS)
|
23
|
+
raise 'Unknown action name' unless (actions - DEPENDENCIES_MAP.keys).size.zero?
|
149
24
|
end
|
150
25
|
|
151
|
-
|
152
|
-
if self.class.resource_context_config.size > 1
|
153
|
-
self.class.resource_context_config[1..-1]
|
154
|
-
else
|
155
|
-
[resource_relation_name.to_sym]
|
156
|
-
end
|
157
|
-
end
|
26
|
+
controller_class.extend Config
|
158
27
|
|
159
|
-
|
160
|
-
return instance_variable_get(:"@#{resource_name}") if instance_variable_get(:"@#{resource_name}").present?
|
28
|
+
loaded_modules = [Implementation]
|
161
29
|
|
162
|
-
|
163
|
-
|
30
|
+
if actions.include?(ALL_ACTIONS_ALIAS)
|
31
|
+
loaded_modules += DEPENDENCIES_MAP.values
|
32
|
+
else
|
33
|
+
loaded_modules += actions.map { |action_name| DEPENDENCIES_MAP[action_name] }
|
164
34
|
end
|
165
35
|
|
166
|
-
|
167
|
-
|
168
|
-
instance_variable_set(:"@#{resource_name}", association_chain.find(params[:id]))
|
36
|
+
loaded_modules.uniq.each do |loaded_module|
|
37
|
+
controller_class.include loaded_module
|
169
38
|
end
|
170
39
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
unless block_given? || options[:location].present? || current_controller_api?
|
175
|
-
options[:location] = after_destroy_redirect_path
|
176
|
-
end
|
177
|
-
|
178
|
-
if activemodel_serializer?
|
179
|
-
render_resource_with_activemodel_serializer(options)
|
180
|
-
else
|
181
|
-
respond_with resource, options, &block
|
182
|
-
end
|
40
|
+
HELPER_METHODS.each do |method_name|
|
41
|
+
controller_class.helper_method method_name
|
183
42
|
end
|
184
43
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
# process not saved result because of failed callback or another ActiveRecord magic
|
189
|
-
if resource.valid? && !result.present?
|
190
|
-
resource.errors[:base] << resource_wasnt_saved_message
|
191
|
-
end
|
192
|
-
|
193
|
-
if result.present?
|
194
|
-
unless block_given? || options[:location].present? || current_controller_api?
|
195
|
-
options[:location] = after_save_redirect_path
|
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
|
202
|
-
end
|
203
|
-
|
204
|
-
if result.present? && activemodel_serializer?
|
205
|
-
render_resource_with_activemodel_serializer(options)
|
206
|
-
else
|
207
|
-
respond_with resource, options, &block
|
208
|
-
end
|
209
|
-
|
210
|
-
result
|
211
|
-
end
|
212
|
-
|
213
|
-
def resource_wasnt_saved_message
|
214
|
-
raise 'Too many Rails magic. Please check your code'
|
215
|
-
end
|
216
|
-
|
217
|
-
def after_create_messages
|
218
|
-
after_save_messages
|
219
|
-
end
|
220
|
-
|
221
|
-
def after_update_messages
|
222
|
-
after_save_messages
|
223
|
-
end
|
224
|
-
|
225
|
-
def after_destroy_messages
|
226
|
-
nil
|
227
|
-
end
|
228
|
-
|
229
|
-
def after_save_messages
|
230
|
-
nil
|
231
|
-
end
|
232
|
-
|
233
|
-
def setup_flash_messages(messages)
|
234
|
-
return unless messages.present?
|
235
|
-
|
236
|
-
raise 'Messages should be specified with Hash' unless messages.is_a?(Hash)
|
237
|
-
|
238
|
-
messages.each do |key, message|
|
239
|
-
flash[key] = message
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
# :nocov:
|
244
|
-
def after_save_redirect_path
|
245
|
-
raise 'Not Implemented'
|
246
|
-
end
|
247
|
-
# :nocov:
|
248
|
-
|
249
|
-
# :nocov:
|
250
|
-
def after_destroy_redirect_path
|
251
|
-
raise 'Not Implemented'
|
252
|
-
end
|
253
|
-
# :nocov:
|
254
|
-
|
255
|
-
# :nocov:
|
256
|
-
def permitted_params
|
257
|
-
raise 'Not Implemented'
|
258
|
-
end
|
259
|
-
# :nocov:
|
260
|
-
end
|
261
|
-
|
262
|
-
module Accessors
|
263
|
-
def resource_class_config
|
264
|
-
@resource_class_config
|
265
|
-
end
|
266
|
-
|
267
|
-
def resource_class(value)
|
268
|
-
@resource_class_config = value
|
269
|
-
end
|
270
|
-
|
271
|
-
def resource_context_config
|
272
|
-
@resource_context_config
|
273
|
-
end
|
274
|
-
|
275
|
-
def resource_context(*values)
|
276
|
-
@resource_context_config = values
|
277
|
-
end
|
278
|
-
|
279
|
-
def paginate_collection_config
|
280
|
-
@paginate_collection_config
|
281
|
-
end
|
282
|
-
|
283
|
-
def paginate_collection(value)
|
284
|
-
@paginate_collection_config = value
|
285
|
-
end
|
286
|
-
|
287
|
-
def api_config
|
288
|
-
@api_config
|
289
|
-
end
|
290
|
-
|
291
|
-
def resource_api(value)
|
292
|
-
@api_config = value
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
class ActionsBuilder
|
297
|
-
DEPENDENCIES_MAP = {
|
298
|
-
index: Index,
|
299
|
-
show: Show,
|
300
|
-
new: New,
|
301
|
-
create: Create,
|
302
|
-
edit: Edit,
|
303
|
-
update: Update,
|
304
|
-
destroy: Destroy
|
305
|
-
}.freeze
|
306
|
-
|
307
|
-
HELPER_METHODS = [:resource, :collection].freeze
|
308
|
-
|
309
|
-
ALL_ACTIONS_ALIAS = :crud
|
310
|
-
|
311
|
-
def self.build(controller_class, actions)
|
312
|
-
unless actions.include?(ALL_ACTIONS_ALIAS)
|
313
|
-
raise 'Unknown action name' unless (actions - DEPENDENCIES_MAP.keys).size.zero?
|
314
|
-
end
|
315
|
-
|
316
|
-
controller_class.extend Accessors
|
317
|
-
|
318
|
-
loaded_modules = [CommonMethods]
|
319
|
-
|
320
|
-
if actions.include?(ALL_ACTIONS_ALIAS)
|
321
|
-
loaded_modules += DEPENDENCIES_MAP.values
|
322
|
-
else
|
323
|
-
loaded_modules += actions.map { |action_name| DEPENDENCIES_MAP[action_name] }
|
324
|
-
end
|
325
|
-
|
326
|
-
loaded_modules.uniq.each do |loaded_module|
|
327
|
-
controller_class.include loaded_module
|
328
|
-
end
|
329
|
-
|
330
|
-
HELPER_METHODS.each do |method_name|
|
331
|
-
controller_class.helper_method method_name
|
332
|
-
end
|
333
|
-
|
334
|
-
controller_class.respond_to :html
|
335
|
-
end
|
44
|
+
controller_class.respond_to :html
|
45
|
+
controller_class.respond_to :json
|
336
46
|
end
|
337
47
|
end
|
338
48
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_resource_controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Zaporozhchenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -142,6 +142,11 @@ files:
|
|
142
142
|
- lib/simple_resource_controller.rb
|
143
143
|
- lib/simple_resource_controller/configurator.rb
|
144
144
|
- lib/simple_resource_controller/controller.rb
|
145
|
+
- lib/simple_resource_controller/controller/actions.rb
|
146
|
+
- lib/simple_resource_controller/controller/config.rb
|
147
|
+
- lib/simple_resource_controller/controller/implementation.rb
|
148
|
+
- lib/simple_resource_controller/controller/implementation/format/api.rb
|
149
|
+
- lib/simple_resource_controller/controller/implementation/format/html.rb
|
145
150
|
- lib/simple_resource_controller/railtie.rb
|
146
151
|
- lib/simple_resource_controller/version.rb
|
147
152
|
- simple_resource_controller.gemspec
|