-
Notifications
You must be signed in to change notification settings - Fork 0
/
subject.rb
171 lines (163 loc) · 5.37 KB
/
subject.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module RestingPug
# Defines everything related to subject basic operations(creation, updating etc)
# @used_in {Base.included}
module Subject
protected
# Gets a subject model from a controller class name
# @note Override this method if you want to use another model.
# @used_in {#subject_model_sym}
# @used_in {#subject_model_sym_plural}
# @used_in {#create_subject}
# @used_in {#fetch_subject}
# @used_in {#fetch_subjects}
# @used_in {Params#permitted_fields}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a subject_model to use Novel as a model
# def subject_model
# Novel
# end
# end
def subject_model
controller_name.classify.constantize
end
# Returns a symbol out of the subject model name
# @note Override this method to get another root for params and JSON responses.
# @use {#subject_model}
# @used_in {Params#params_for_create}
# @used_in {Params#params_for_update}
# @used_in {Render#render_subject}
# @used_in {Render#render_param_missing}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a subject_model to use :novel as a symbol used in params and rendering
# def subject_model_sym
# :novel
# end
# end
def subject_model_sym
subject_model.to_s.underscore.to_sym
end
# Returns a symbol out of the subject model name(plural)
# @note Override this method to get another root JSON response.
# @use {#subject_model}
# @used_in {Render#render_subjects}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a subject_model_sym_plural to use :novels as a symbol used in index action
# def subject_model_sym_plural
# :novels
# end
# end
def subject_model_sym_plural
subject_model.to_s.pluralize.underscore.to_sym
end
# Creates a subject
# @note Override this method to add some custom logic to creating process.
# @used_in {Chains#create_chain}
# @use {#subject_model}
# @use {Params#params_for_create}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a create_subject to create only if title is not 'None'
# def create_subject
# @subject = subject_model.create(params_for_create) unless params_for_create[:title] == 'None'
# end
# end
def create_subject
@subject = subject_model.create(params_for_create)
end
# Updates a subject
# @note Override this method to add some custom logic to updating process.
# @used_in {Chains#update_chain}
# @use {Params#params_for_update}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a update_subject to update only if title is not 'None'
# def update_subject
# @subject.update(params_for_update) unless params_for_update[:title] == 'None'
# end
# end
def update_subject
@subject.update(params_for_update)
end
# Destroys a subject and render nothing or errors
# @note Override this method to add some custom logic to destroying process.
# @used_in {Chains#destroy_chain}
# @use {Render#render_nothing}
# @use {Render#render_errors}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a destroy_subject to destroy only if title is not 'None'
# def destroy_subject
# if @subject.title != 'None' && @subject.destroy
# render_nothing
# else
# render_errors
# end
# end
# end
def destroy_subject
if @subject.destroy
render_nothing
else
render_errors
end
end
# Finds a subject by id
# @note Override this method to add some custom logic to fetching process.
# @used_in {Chains#create_chain}
# @used_in {Chains#update_chain}
# @used_in {Chains#destroy_chain}
# @used_in {Chains#show_chain}
# @use {#subject_model}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a fetch_subject to return a default subject if requested is not found
# def fetch_subject
# @subject = subject_model.find_by_id(params[:id]) || default_subject
# end
# end
def fetch_subject
@subject = subject_model.find(params[:id])
end
# Returns all subjects accessable for API
# @note Override this method to add some custom logic to fetching process.
# @used_in {Chains#index_chain}
# @use {#subject_model}
# @example
# class BooksController < ApplicationController
# include RestingPug::Base
#
# private
# # Override a fetch_subjects to return subject where title is not 'None'
# def fetch_subjects
# @subjects = subject_model.where.not(title: 'None')
# end
# end
def fetch_subjects
@subjects = subject_model.all
end
end
end