railspp 0.2.5 → 0.2.7
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/lib/templates/global_controller.txt +195 -187
- data/lib/utils/strings.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: 517e702540a5b51072c0990ce8db30bfd7b05cf95c7201763bad4bcf295303ca
|
4
|
+
data.tar.gz: 5b3ee44ec775458339eab499682d61e043ba4f064f25184cc9463bcc24893ed7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0ee0cd730181a438321c4709454a92847b1945e7b90d448db2c6f06c207a09a5ff72b5896d9286e8fe906b8e3cab7860596c73e06db31d86f27ffc2c435084b
|
7
|
+
data.tar.gz: ec76eb3b0a04bb8b932254f3469aa27610f749526811e7c28eb041b646d11e4ce7db836716ee17cf08ff9ea1192ad05ed4b1a255ee7fa044c14e82462d10cc35
|
@@ -1,191 +1,199 @@
|
|
1
1
|
class GlobalController < ApplicationController
|
2
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
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
3
|
+
include ExceptionHandler
|
4
|
+
include Response
|
5
|
+
|
6
|
+
def default_per_page
|
7
|
+
25
|
8
|
+
end
|
9
|
+
|
10
|
+
def model
|
11
|
+
self.class.to_s.split('::').select { |e| /Controller/.match(e) }.first.chomp('Controller').singularize.constantize
|
12
|
+
end
|
13
|
+
|
14
|
+
def index
|
15
|
+
resp = index_response
|
16
|
+
past_pages = resp[:last_page] < resp[:current_page]
|
17
|
+
error = { error: 'Past the amount of pages available' }
|
18
|
+
status = past_pages ? 400 : 200
|
19
|
+
data = past_pages ? error : index_response
|
20
|
+
data = handle_includes(data) if params[:include]
|
21
|
+
render_json(data, status)
|
22
|
+
end
|
23
|
+
|
24
|
+
def show
|
25
|
+
data = model.find(params[:id] || 0)
|
26
|
+
data = data.includes(*get_includes) if params[:include]
|
27
|
+
data = handle_includes(data) if params[:include]
|
28
|
+
render_json(data, 200)
|
29
|
+
end
|
30
|
+
|
31
|
+
def create
|
32
|
+
data = model.create(create_params)
|
33
|
+
render_json(data, 201)
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
data = model.where(id: params[:id] || 0).update(update_params).first
|
38
|
+
render_json(data, 200)
|
39
|
+
end
|
40
|
+
|
41
|
+
def destroy
|
42
|
+
model.destroy(params[:id] || 0)
|
43
|
+
render_json(nil, 204)
|
44
|
+
end
|
45
|
+
|
46
|
+
def bulk_csv_create
|
47
|
+
data = model.create(csv_to_json)
|
48
|
+
render_json(data, 201)
|
49
|
+
end
|
50
|
+
|
51
|
+
def bulk_create
|
52
|
+
data = model.create(bulk_create_params)
|
53
|
+
render_json(data, 201)
|
54
|
+
end
|
55
|
+
|
56
|
+
def bulk_update
|
57
|
+
data = model.where(id: bulk_update_params[:id]).update(bulk_update_params)
|
58
|
+
render_json(data, 201)
|
59
|
+
end
|
60
|
+
|
61
|
+
def bulk_destory
|
62
|
+
model.where(id: bulk_destroy_params)
|
63
|
+
render_json(nil, 204)
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def csv_to_json param_key = :file, separated_by = ','
|
69
|
+
lines = params[param_key].split("\n")
|
70
|
+
keys = lines[0].split(separated_by)
|
71
|
+
rows = lines.slice(1).map { |e| e.split(separated_by) }
|
72
|
+
rows.map { |e| e.each_with_index.inject({}) { |acc, (e, i)| acc.merge("#{keys[i]}": e) } }
|
73
|
+
end
|
74
|
+
|
75
|
+
def create_params
|
76
|
+
bl = array_to_hash(black_list_create)
|
77
|
+
params.permit(*get_model_key.reject { |e| bl[e.to_sym] })
|
78
|
+
end
|
79
|
+
|
80
|
+
def update_params
|
81
|
+
bl = array_to_hash(black_list_update)
|
82
|
+
params.permit(*get_model_key.reject { |e| bl[e.to_sym] })
|
83
|
+
end
|
84
|
+
|
85
|
+
def bulk_create_params
|
86
|
+
params.permit(bulk: get_model_key)
|
87
|
+
end
|
88
|
+
|
89
|
+
def bulk_update_params
|
90
|
+
params.permit(bulk: get_model_key)
|
91
|
+
end
|
92
|
+
|
93
|
+
def bulk_destroy_params
|
94
|
+
params.permit(bulk: [:id])
|
95
|
+
end
|
96
|
+
|
97
|
+
def black_list_create
|
98
|
+
[:id, :created_at, :updated_at]
|
99
|
+
end
|
100
|
+
|
101
|
+
def black_list_update
|
102
|
+
[:id, :created_at, :updated_at]
|
103
|
+
end
|
104
|
+
|
105
|
+
def array_to_hash array
|
106
|
+
array.each_with_object({}) do |e, acc|
|
107
|
+
acc[e] = true
|
108
|
+
acc
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def handle_pagination
|
113
|
+
limit = (params[:limit] || default_per_page).to_i
|
114
|
+
offset = (params[:offset] || 0).to_i
|
115
|
+
page = (params[:page] || 1).to_i
|
116
|
+
|
117
|
+
data = model.limit(limit).offset(offset).offset((page - 1) * limit)
|
118
|
+
data = data.where(get_where) if params[:where]
|
119
|
+
data = data.includes(*get_includes) if params[:include]
|
120
|
+
data = data.order(get_order) if params[:order]
|
121
|
+
data = data.all
|
122
|
+
data
|
123
|
+
end
|
124
|
+
|
125
|
+
def handle_pagination_count
|
126
|
+
data = model
|
127
|
+
data = data.where(get_where) if params[:where]
|
128
|
+
data.count
|
129
|
+
end
|
130
|
+
|
131
|
+
def handle_includes data
|
132
|
+
data.to_json({ include: get_includes })
|
133
|
+
end
|
134
|
+
|
135
|
+
def index_response
|
136
|
+
page = (params[:page] || 1).to_i
|
137
|
+
per_page = (params[:limit] || default_per_page).to_i
|
138
|
+
total = handle_pagination_count
|
139
|
+
path = request.original_url
|
140
|
+
path_path = add_pg_qs(path)
|
141
|
+
last = (total / per_page.to_f).ceil
|
142
|
+
nxt = (page + 1) > last ? nil : (page + 1)
|
143
|
+
prev = (page - 1) == 0 ? nil : (page - 1)
|
144
|
+
last = last == 0 ? 1 : last
|
145
|
+
data = handle_pagination
|
146
|
+
data_first = data.first
|
147
|
+
data_last = data.last
|
148
|
+
|
149
|
+
{
|
150
|
+
data: data,
|
151
|
+
from: data_first ? data_first.id : nil,
|
152
|
+
to: data_last ? data_last.id : nil,
|
153
|
+
total: total,
|
154
|
+
path: path,
|
155
|
+
current_page: page,
|
156
|
+
per_page: per_page,
|
157
|
+
last_page: last,
|
158
|
+
first_page_url: path_path + '1',
|
159
|
+
last_page_url: path_path + last.to_s,
|
160
|
+
next_page_url: nxt ? (path_path + nxt.to_s) : nil,
|
161
|
+
prev_page_url: prev ? (path_path + prev.to_s) : nil,
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
def add_pg_qs str
|
166
|
+
has_qs = /\?/.match(str)
|
167
|
+
return "#{str}?page=" unless has_qs
|
168
|
+
before_qs, after = str.split('?')
|
169
|
+
after_qs = after.split('&').reject { |e| /page\=/.match(e) }
|
170
|
+
after_qs.push('page=')
|
171
|
+
"#{before_qs}?#{after_qs.join('&')}"
|
172
|
+
end
|
173
|
+
|
174
|
+
def get_includes
|
175
|
+
comma_separated(:include).map(&:to_sym)
|
176
|
+
end
|
177
|
+
|
178
|
+
def get_order
|
179
|
+
params[:order].split(',').flat_map { |e| e.split(':') }.join(' ')
|
180
|
+
end
|
181
|
+
|
182
|
+
def get_where
|
183
|
+
arrays = params[:where].split(',').map { |e| e.split(':') }
|
184
|
+
arrays.inject({}) do |acc, item|
|
185
|
+
key, val = item
|
186
|
+
acc[key] = val
|
187
|
+
acc
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def comma_separated key
|
192
|
+
params[key].split(',')
|
193
|
+
end
|
194
|
+
|
195
|
+
def get_model_key
|
196
|
+
model.columns.map { |e| e.name.to_sym }
|
197
|
+
end
|
190
198
|
|
191
199
|
end
|
data/lib/utils/strings.rb
CHANGED