railspp 0.3.0 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37e6f7c34e88a8709e1cf932d8bbe1ef6f994601349b91ecc49dc76e7161f1d7
4
- data.tar.gz: f6843abd128722e9480ab99580b29a34adcad06cca7cd18a63fe1294d433726b
3
+ metadata.gz: 65047ba2296e1a10247976a0652895892dbc4cfafbc52ce3eec4e368b3a95275
4
+ data.tar.gz: 6bf4edfeed47661342c1b71b3a60e4b5afebd66c839b4593440956fbe9c17cb5
5
5
  SHA512:
6
- metadata.gz: 8cd644a00d15a557889b70aa8cd8e247e178312f8b5f1ecbaaeb97f3f1a39b2df3cbc48573664fce0b3bd8e67de9a3e83a9825f5b0e0e299e8ebfd39fd7770a4
7
- data.tar.gz: 4ee799382461b7c864fc2d458f477ebce87d11206b7a7a52d15e7b9ec12f85ae45a0b029f7933e119605c692785ead92a04364d904a09fc964075de3c9107d35
6
+ metadata.gz: c65e5067ff212106566bcd1588489e9338f64e555788caf8a9360fce4064d1f9da62f5cbef8871ab1906c10af6821e84ce5260e0e97c1652bc16ceef19346a29
7
+ data.tar.gz: 3332b64f99cabbd733085ea13a171c9d5e7735fc9e9f443b53ae06fe7f6a4b9ad852721a2638d878e24d69a1c04c0ca2ba13ecabca2f7fb7dd12399f14e43eba
@@ -10,6 +10,9 @@ class UpdateVersionCommand < MoreUtils
10
10
  gc_template = get_file_str("#{this_dir}/../templates/global_controller.txt")
11
11
  write_file("#{root}/app/controllers/global_controller.rb", gc_template)
12
12
 
13
+ gc_template = get_file_str("#{this_dir}/../templates/response.txt")
14
+ write_file("#{root}/app/controllers/concerns/response.rb", gc_template)
15
+
13
16
  # dc_template = get_file_str("#{this_dir}/../templates/documentation_controller.txt")
14
17
  # write_file("#{root}/app/controllers/documentation_controller.rb", dc_template)
15
18
 
@@ -1,199 +1,211 @@
1
1
  class GlobalController < ApplicationController
2
-
3
2
  include ExceptionHandler
4
3
  include Response
5
4
 
6
5
  def default_per_page
7
- 25
6
+ 25
8
7
  end
9
8
 
10
9
  def model
11
- self.class.to_s.split('::').select { |e| /Controller/.match(e) }.first.chomp('Controller').singularize.constantize
10
+ self.class.to_s.split("::").select {|e| /Controller/.match(e) }.first.chomp("Controller").singularize.constantize
12
11
  end
13
12
 
14
13
  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 : resp
20
- data = handle_includes(data) if params[:include] && !past_pages
21
- render_json(data, status)
14
+ resp = index_response
15
+ past_pages = resp[:last_page] < resp[:current_page]
16
+ error = {error: "Past the amount of pages available"}
17
+ status = past_pages ? 400 : 200
18
+ data = past_pages ? error : resp
19
+ data = handle_includes(data) if params[:include] && !past_pages
20
+ render_json(data, status)
22
21
  end
23
22
 
24
23
  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)
24
+ data = model.find(params[:id] || 0)
25
+ data = handle_includes(data) if params[:include]
26
+ render_json(data, 200)
29
27
  end
30
28
 
31
29
  def create
32
- data = model.create(create_params)
33
- render_json(data, 201)
30
+ data = model.create(create_params)
31
+ render_json(data, 201)
34
32
  end
35
33
 
36
34
  def update
37
- data = model.where(id: params[:id] || 0).update(update_params).first
38
- render_json(data, 200)
35
+ data = model.where(id: params[:id] || 0).update(update_params).first
36
+ render_json(data, 200)
39
37
  end
40
38
 
41
39
  def destroy
42
- model.destroy(params[:id] || 0)
43
- render_json(nil, 204)
40
+ model.destroy(params[:id] || 0)
41
+ render_json(nil, 204)
44
42
  end
45
43
 
46
44
  def bulk_csv_create
47
- data = model.create(csv_to_json)
48
- render_json(data, 201)
45
+ data = model.create(csv_to_json)
46
+ render_json(data, 201)
49
47
  end
50
48
 
51
49
  def bulk_create
52
- data = model.create(bulk_create_params)
53
- render_json(data, 201)
50
+ data = model.create(bulk_create_params[:bulk])
51
+ render_json(data, 201)
54
52
  end
55
53
 
56
54
  def bulk_update
57
- data = model.where(id: bulk_update_params[:id]).update(bulk_update_params)
58
- render_json(data, 201)
55
+ data = model.where(id: bulk_update_params[:bulk].map {|e| e[:id] }).update(bulk_update_params[:bulk])
56
+ render_json(data, 201)
59
57
  end
60
58
 
61
59
  def bulk_destory
62
- model.where(id: bulk_destroy_params)
63
- render_json(nil, 204)
60
+ model.where(id: bulk_destroy_params[:bulk])
61
+ render_json(nil, 204)
64
62
  end
65
63
 
66
64
  private
67
65
 
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
66
+ def csv_to_json param_key=:file, separated_by=","
67
+ lines = params[param_key].split("\n")
68
+ keys = lines[0].split(separated_by)
69
+ rows = lines.slice(1).map {|e| e.split(separated_by) }
70
+ rows.map {|e| e.each_with_index.inject({}) {|acc, (e, i)| acc.merge(:"#{keys[i]}" => e) } }
71
+ end
72
+
73
+ def create_params
74
+ bl = array_to_hash(black_list_create)
75
+ params.permit(*get_model_key.reject {|e| bl[e.to_sym] })
76
+ end
77
+
78
+ def update_params
79
+ bl = array_to_hash(black_list_update)
80
+ params.permit(*get_model_key.reject {|e| bl[e.to_sym] })
81
+ end
82
+
83
+ def bulk_create_params
84
+ params.permit(bulk: get_model_key)
85
+ end
86
+
87
+ def bulk_update_params
88
+ params.permit(bulk: get_model_key)
89
+ end
90
+
91
+ def bulk_destroy_params
92
+ params.permit(bulk: [:id])
93
+ end
94
+
95
+ def black_list_create
96
+ %i[id created_at updated_at]
97
+ end
98
+
99
+ def black_list_update
100
+ %i[id created_at updated_at]
101
+ end
102
+
103
+ def array_to_hash array
104
+ array.each_with_object({}) do |e, acc|
105
+ acc[e] = true
106
+ acc
107
+ end
108
+ end
109
+
110
+ def handle_pagination
111
+ limit = (params[:limit] || default_per_page).to_i
112
+ offset = (params[:offset] || 0).to_i
113
+ page = (params[:page] || 1).to_i
114
+ order = params[:order] ? get_order : "id ASC"
115
+
116
+ data = model.limit(limit).offset(offset).offset((page - 1) * limit)
117
+ data = data.where(get_where) if params[:where]
118
+ data = data.includes(*get_includes) if params[:include]
119
+ data = data.order(order)
120
+ data = data.all
121
+ data
122
+ end
123
+
124
+ def handle_pagination_count
125
+ data = model
126
+ data = data.where(get_where) if params[:where]
127
+ data.count
128
+ end
129
+
130
+ def handle_includes data
131
+ data.to_json(include: get_includes)
132
+ end
133
+
134
+ def index_response
135
+ page = (params[:page] || 1).to_i
136
+ per_page = (params[:limit] || default_per_page).to_i
137
+ total = handle_pagination_count
138
+ path = request.original_url
139
+ path_path = add_pg_qs(path)
140
+ last = (total / per_page.to_f).ceil
141
+ nxt = (page + 1) > last ? nil : (page + 1)
142
+ prev = (page - 1) == 0 ? nil : (page - 1)
143
+ last = last == 0 ? 1 : last
144
+ data = handle_pagination
145
+ data_first = data.first
146
+ data_last = data.last
147
+
148
+ {
149
+ data: data,
150
+ from: data_first ? data_first.id : nil,
151
+ to: data_last ? data_last.id : nil,
152
+ total: total,
153
+ path: path,
154
+ current_page: page,
155
+ per_page: per_page,
156
+ last_page: last,
157
+ first_page_url: path_path + "1",
158
+ last_page_url: path_path + last.to_s,
159
+ next_page_url: nxt ? (path_path + nxt.to_s) : nil,
160
+ prev_page_url: prev ? (path_path + prev.to_s) : nil
161
+ }
162
+ end
163
+
164
+ def add_pg_qs str
165
+ has_qs = /\?/.match(str)
166
+ return "#{str}?page=" unless has_qs
167
+
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 do |e|
176
+ semi = e.split(":")
177
+ semi.length > 1 ? array_depth(semi) : semi.first.to_sym
178
+ end
179
+ end
180
+
181
+ def array_depth(array, acc={})
182
+ if array.length > 2
183
+ key = array.shift
184
+ acc[key.to_sym] = array_depth(array, {})
185
+ else
186
+ key, val = array
187
+ acc[key.to_sym] = val.to_sym
188
+ end
189
+ acc
190
+ end
191
+
192
+ def get_order
193
+ params[:order].split(",").flat_map {|e| e.split(":") }.join(" ")
194
+ end
198
195
 
196
+ def get_where
197
+ arrays = params[:where].split(",").map {|e| e.split(":") }
198
+ arrays.each_with_object({}) do |item, acc|
199
+ key, val = item
200
+ acc[key] = val
201
+ end
202
+ end
203
+
204
+ def comma_separated key
205
+ params[key].split(",")
206
+ end
207
+
208
+ def get_model_key
209
+ model.columns.map {|e| e.name.to_sym }
210
+ end
199
211
  end
@@ -1,7 +1,30 @@
1
1
  module Response
2
+ def render_json data, status
3
+ data = JSON.parse(data) if data.is_a?(String)
4
+ data[:data] = data[:data].map {|e| object_delete(e) } if data && data[:data] && !data[:data].empty?
5
+ data = object_delete(data) if data && !data[:data]
6
+ render json: data, status: status
7
+ end
2
8
 
3
- def render_json data, status
4
- render json: data, status: status
5
- end
9
+ private
10
+
11
+ def black_list
12
+ [
13
+ :password_digest
14
+ ]
15
+ end
6
16
 
17
+ def object_delete(hash)
18
+ json = JSON.parse(hash.to_json).symbolize_keys
19
+ black_list.each do |e|
20
+ if json[e].is_a?(Hash)
21
+ json[e] = object_delete(json[e])
22
+ elsif json[e].is_a?(Array)
23
+ json[e] = json[e].map {|i| object_delete(i) }
24
+ elsif json[e]
25
+ json.delete(e)
26
+ end
27
+ end
28
+ json
29
+ end
7
30
  end
data/lib/utils/strings.rb CHANGED
@@ -21,7 +21,7 @@ class MoreUtils
21
21
  class << self
22
22
 
23
23
  def gem_version
24
- "0.3.0"
24
+ "0.3.1"
25
25
  end
26
26
 
27
27
  def get_file_str path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railspp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Layne Faler