-
Notifications
You must be signed in to change notification settings - Fork 47
/
template.rb
345 lines (275 loc) · 8.45 KB
/
template.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
require "active_support/all"
def install_standalone
set_ruby_version
gem "godmin", "1.5.0"
after_bundle do
create_database
generate_models
generate("godmin:install")
generate("godmin:resource", "article")
generate("godmin:resource", "author")
modify_rakefile
modify_routes
modify_locales
modify_models
modify_author_service
modify_article_controller
modify_article_service
modify_readme
migrate_and_seed
end
end
def install_engine
set_ruby_version
run_ruby_script("bin/rails plugin new admin --mountable")
gsub_file "admin/admin.gemspec", "TODO: ", ""
gsub_file "admin/admin.gemspec", "TODO", ""
inject_into_file "admin/admin.gemspec", before: /^end/ do
<<-END.strip_heredoc.indent(2)
s.add_dependency "godmin", "~> 1.5.0"
END
end
gem "admin", path: "admin"
after_bundle do
create_database
generate_models
run_ruby_script("admin/bin/rails g godmin:install")
run_ruby_script("admin/bin/rails g godmin:resource article")
run_ruby_script("admin/bin/rails g godmin:resource author")
inject_into_file "config/routes.rb", before: /^end/ do
<<-END.strip_heredoc.indent(2)
mount Admin::Engine, at: "admin"
END
end
modify_rakefile
modify_routes("admin")
modify_locales
modify_models
modify_author_service("admin")
modify_article_controller("admin")
modify_article_service("admin")
modify_readme
migrate_and_seed
end
end
def set_ruby_version
prepend_to_file "Gemfile" do
"ruby '2.3.5'\n"
end
end
def create_database
rake("db:drop")
rake("db:create")
end
def generate_models
generate(:model, "author name:string")
generate(:model, "article title:string body:text author:references published:boolean published_at:datetime")
gsub_file Dir.glob("db/migrate/*_create_articles.rb").first, "t.boolean :published", "t.boolean :published, default: false"
append_to_file "db/seeds.rb" do
<<-END.strip_heredoc
def title
5.times.map { lorem.sample }.join(" ").capitalize
end
def lorem
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".gsub(/[.,]/, "").downcase.split(" ")
end
def author
Author.all.sample
end
def published
[true, true, false].sample
end
def published_at
Time.now - [0, 1, 2, 3, 4, 5].sample.days
end
["Lorem Ipsum", "Magna Aliqua", "Commodo Consequat"].each do |name|
Author.create name: name
end
35.times do |i|
Article.create! title: title, author: author, published: published, published_at: published_at
end
END
end
end
def modify_rakefile
append_to_file "RakeFile" do
<<-END.strip_heredoc
namespace :sandbox do
desc "Reseed the database"
task reseed: :environment do
Rake::Task["sandbox:reset"].invoke
Rake::Task["db:environment:set"].invoke
Rake::Task["db:schema:load"].invoke
Rake::Task["db:seed"].invoke
end
desc "Reset the database"
task reset: :environment do
ActiveRecord::Base.connection.tables.each do |table|
if table != "schema_migrations"
query = "DROP TABLE IF EXISTS \#{table} CASCADE;"
ActiveRecord::Base.connection.execute(query)
end
end
end
end
END
end
end
def modify_locales
append_to_file "config/locales/en.yml" do
<<-END.strip_heredoc.indent(2)
activerecord:
models:
article:
one: Article
other: Articles
author:
one: Author
other: Authors
END
end
end
def modify_routes(namespace = nil)
routes_file =
if namespace
"admin/config/routes.rb"
else
"config/routes.rb"
end
gsub_file routes_file, "application#welcome", "articles#index"
end
def modify_models
inject_into_file "app/models/article.rb", before: "end" do
<<-END.strip_heredoc.indent(2)
def to_s
title
end
END
end
inject_into_file "app/models/author.rb", before: "end" do
<<-END.strip_heredoc.indent(2)
def to_s
name
end
END
end
end
def modify_article_controller(namespace = nil)
articles_controller =
if namespace
"admin/app/controllers/admin/articles_controller.rb"
else
"app/controllers/articles_controller.rb"
end
inject_into_file articles_controller, after: "Godmin::Resources::ResourceController\n" do
<<-END.strip_heredoc.indent(namespace ? 4 : 2)
private
def redirect_after_batch_action_unpublish
articles_path(scope: :unpublished)
end
def redirect_after_batch_action_publish
articles_path(scope: :published)
end
END
end
end
def modify_article_service(namespace = nil)
article_service =
if namespace
"admin/app/services/admin/article_service.rb"
else
"app/services/article_service.rb"
end
gsub_file article_service, "attrs_for_index", "attrs_for_index :title, :author, :published_at"
gsub_file article_service, "attrs_for_show", "attrs_for_show :title, :body, :author, :published, :published_at"
gsub_file article_service, "attrs_for_form", "attrs_for_form :title, :body, :author, :published, :published_at"
inject_into_file article_service, after: "attrs_for_form :title, :body, :author, :published, :published_at \n" do
<<-END.strip_heredoc.indent(namespace ? 4 : 2)
attrs_for_export :id, :title, :author, :published, :published_at
scope :unpublished
scope :published
def scope_unpublished(articles)
articles.where(published: false)
end
def scope_published(articles)
articles.where(published: true)
end
filter :title
filter :author, as: :select, collection: -> { Author.all }, option_text: "name"
def filter_title(articles, value)
articles.where("title LIKE ?", "%\#{value}%")
end
def filter_author(articles, value)
articles.where(author: value)
end
batch_action :unpublish, except: [:unpublished]
batch_action :publish, except: [:published]
batch_action :destroy, confirm: true
def batch_action_unpublish(articles)
articles.update_all(published: false)
end
def batch_action_publish(articles)
articles.update_all(published: true)
end
def batch_action_destroy(articles)
articles.destroy_all
end
def per_page
15
end
END
end
end
def modify_author_service(namespace = nil)
author_service =
if namespace
"admin/app/services/admin/author_service.rb"
else
"app/services/author_service.rb"
end
gsub_file author_service, "attrs_for_index", "attrs_for_index :name"
gsub_file author_service, "attrs_for_show", "attrs_for_show :name"
gsub_file author_service, "attrs_for_form", "attrs_for_form :name"
inject_into_file author_service, after: "attrs_for_form :name \n" do
<<-END.strip_heredoc.indent(namespace ? 4 : 2)
attrs_for_export :id, :name
filter :name
def filter_name(authors, value)
authors.where("name LIKE ?", "%\#{value}%")
end
batch_action :destroy, confirm: true
def batch_action_destroy(authors)
authors.each { |a| a.destroy }
end
END
end
end
def modify_readme
readme_file = "README.md"
run "rm #{readme_file}"
readme_text = <<~README
# README
This is the source code for a demo application of the [Godmin](https://github.com/varvet/godmin) admin framework for Rails.
It is generated by running `rake sandbox:deploy` inside the Godmin repo.
README
File.open(readme_file, 'w') { |file| file.write(readme_text) }
end
def migrate_and_seed
rake("db:migrate")
rake("db:seed")
end
with_engine = "--with-engine"
without_engine = "--without-engine"
if ARGV.count > (ARGV - [with_engine, without_engine]).count
if ARGV.include? with_engine
install_engine
elsif ARGV.include? without_engine
install_standalone
end
else
if yes?("Place godmin in admin engine?")
install_engine
else
install_standalone
end
end