-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivestorage.sh
executable file
·331 lines (232 loc) · 7.85 KB
/
activestorage.sh
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env bash
echo ~----------~----------Startingd $HOSTNAME, pwd: `pwd`, dlr0: "$0", bashsource0: "${BASH_SOURCE[0]}", $(date +"%Y-%m-%d_%H.%M.%S")
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Summary:
# This depends on jq autocomplete
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# read settings..
read appn sfil sfil2 mpwd parm0</tmp/"_brvar1202_${USER}".txt
echo $appn $sfil $sfil2 $mpwd $parm0
# timeout1=5 ; read -t "${timeout1}" -p "Press ENTER or wait $timeout1 seconds..." || true ; echo ;
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# If not installed, install imagemagick
# sudo apt -y install imagemagick
bin/rails active_storage:install
rails db:migrate RAILS_ENV=development
sleep 1
git add -A # Add all files and commit them
git commit -m "activestorage1"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo "# " >> Gemfile
echo "gem 'mini_magick'">> Gemfile
bundle update
sleep 1
git add -A # Add all files and commit them
git commit -m "activestorage gemfile"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# model..
### ~~
# add new lines of text after patrn...
#
filetarg='app/models/country_of_origin.rb'
cat $filetarg
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
patrn='class'
repl2 = %Q{
has_one_attached :avatar
has_many_attached :documents
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /#{Regexp.escape(patrn)}/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# _form
# 7. Add input fields to app/views/country_of_origins/_form.html.erb:
# class="actions"
### ~~~~
# add new lines of text n lines after patrn...
#
filetarg='app/views/country_of_origins/_form.html.erb'
cat $filetarg
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
patrn='f.number_field :sort_order'
repl2 = %Q{
<hr>
<div class="field">
<%= f.label :avatar %>
<p> (A new choice will replace previous one.)</p>
<%= f.file_field :avatar %>
</div>
<br>
<div class="field">
<%= f.label :documents %>
<p> (Choosing files will add to previous ones..)</p>
<%= f.file_field :documents, multiple: true %>
</div>
<hr>
}
i=-2
ARGF.each_with_index do |line, ndx|
puts line
if line =~ /#{Regexp.escape(patrn)}/
i = ndx + 2
end
if ndx == i
puts repl2
end
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# controllers
# this one isn't needed
#
# add new lines of text after patrn...
#
# filetarg='app/controllers/country_of_origins_controller.rb'
# cat $filetarg
# r1tmp="/tmp/_temprubyrunner_${USER}.rb"
# cat << 'HEREDOC' > $r1tmp
# patrn='if @country_of_origin.save'
# repl2 = %Q{
# avatar = params[:country_of_origin][:avatar]
# documents = params[:country_of_origin][:documents]
# if avatar
# @country_of_origin.avatar.attach(avatar)
# end
# if documents
# @country_of_origin.documents.attach(documents)
# end
# }
# ARGF.each do |line|
# puts line
# puts repl2 if line =~ /#{Regexp.escape(patrn)}/
# end
# HEREDOC
# ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
# edit whitelist..
# original.. params.require(:country_of_origin).permit(:name, :pdate, :active_status, :sort)
filetarg='app/controllers/country_of_origins_controller.rb'
sed -i '/params.require/s/)$/, :avatar)/' $filetarg
sed -i '/params.require/s/)$/, documents: [])/' $filetarg
# add new lines of text before patrn...
#
filetarg='app/controllers/country_of_origins_controller.rb'
cat $filetarg
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat << 'HEREDOC' > $r1tmp
patrn='private'
repl2 = %Q{
def delete_document_attachment
@document = ActiveStorage::Attachment.find(params[:id])
@document.purge
# redirect_to @current_page
redirect_back(fallback_location: root_path)
end
}
ARGF.each do |line|
puts repl2 if line =~ /#{Regexp.escape(patrn)}/
puts line
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# edit routes
# if `resources :country_of_origins do` found, then - add - to the stanza.
# if `resources :country_of_origins` found, then - replace - the stanza.
pattern1='resources :country_of_origins do'
filetarg='config/routes.rb'
if ! grep -q "${pattern1}" $filetarg ; then
echo 'nogrep ~216. routes stanza not found check script interation between jq and activestorage. '
sleep 9 ;
exit 9 ;
fi
# make the edit to add
#
# add new lines of text after patrn...
#
filetarg='config/routes.rb'
cat $filetarg
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat <<'HEREDOC' > $r1tmp
patrn=':country_of_origins do'
repl2 = %Q{
member do
delete :delete_document_attachment
end
}
ARGF.each do |line|
puts line
puts repl2 if line =~ /#{Regexp.escape(patrn)}/
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sleep 1
git add -A # Add all files and commit them
git commit -m "activestorage app edits"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Add line to config..
filetarg='config/environments/development.rb'
sed -i '/Rails.application.configure/a \ \ config.active_storage.service = :local' $filetarg
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# edit show...
filetarg='app/views/country_of_origins/show.html.erb'
cat $filetarg
r1tmp="/tmp/_temprubyrunner_${USER}.rb"
cat <<'HEREDOC' > $r1tmp
patrn='@country_of_origin.sort'
repl2 = %Q{
<hr>
<p>
<strong>Avatar:</strong> <br><br>
Syntax type 1:
<%= if @country_of_origin.avatar.attached? then
link_to @country_of_origin.avatar.blob.filename, url_for(@country_of_origin.avatar)
end %> <br>
<%= if @country_of_origin.avatar.attached? then
if @country_of_origin.avatar.image? then ( image_tag @country_of_origin.avatar.variant(resize: "180x180") ) end
end %> <br>
Syntax type 2:
<%= if @country_of_origin.avatar.attached? then link_to @country_of_origin.avatar.filename, rails_blob_path(@country_of_origin.avatar, disposition: 'preview') end %>
</p>
<br>
<p>
<strong>Documents:</strong>
<ul>
<% @country_of_origin.documents.each do |document| %>
<li><%= link_to document.blob.filename, url_for(document) %></li>
<%# show thumbnail if it is an image.. %>
<%= if document.image? then ( image_tag document.variant(resize: "180x180") ) end %>
<%= link_to 'Remove', delete_document_attachment_country_of_origin_url(document),
method: :delete,
data: { confirm: 'Are you sure you want to delete?' } %>
<% end %>
</ul>
</p>
<hr>
}
i=-2
ARGF.each_with_index do |line, ndx|
puts line
if line =~ /#{Regexp.escape(patrn)}/
i = ndx + 1
end
if ndx == i
puts repl2
end
end
HEREDOC
ruby $r1tmp $filetarg > $filetarg.tmp ; cat $filetarg.tmp ; cp $filetarg.tmp $filetarg; rm $filetarg.tmp
git add -A # Add all files and commit them
git commit -m "activestorage app edits2"
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~