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
|
# File 'lib/generators/jimothy/install_generator.rb', line 7
def install_jimothy
cyan = "\e[36m"
if !Dir[Rails.root + 'app/models/*.rb'].map { |path| path.split('/')[-1] }.include? 'user.rb'
puts "#{cyan}\nš¦ Scaffolding user model now."
%x(rails g scaffold user name email image)
puts "š¦ User model/views scaffolding built."
%x(rails db:migrate)
puts "š¦ Database migrated."
gsub_file 'app/views/users/_user.html.erb',/<%= user.image %>/,'<%= image_tag user.image %>'
puts "#{cyan}š¦ _user view partial updated."
if File.exists?('db/seeds.rb')
puts "š¦ Seed file exists - emptying the file now."
gsub_file 'db/seeds.rb',/# This file should contain all the record creation needed to seed the database with its default values.\n# The data can then be loaded with the bin\/rails db:seed command \(or created alongside the database with db:setup\).\n#\n# Examples:\n#\n# movies = Movie.create\(\[\{ name: \"Star Wars\" \}, \{ name: \"Lord of the Rings\" \}\]\)\n# Character.create\(name: \"Luke\", movie: movies.first\)\n/,''
else
puts "š¦ Seed file does not exist. Creating now."
File.open("db/seeds.rb") {|f| f.write(".") }
end
seeds_file = File.read('db/seeds.rb')
if seeds_file.empty?
puts "#{cyan}š¦ Seed file is empty - adding Jimothy::seed_users call now."
File.write('db/seeds.rb', "require \"jimothy\"\nJimothy::seed_users", mode: 'a+')
else
puts "š¤ Seed file is not empty - skipping adding Jimothy::seed_users call."
end
puts "š¦ Seeding users now."
%x(rails db:seed)
puts "š¦ Adding jimothy gem manifest to app manifest now.\n"
File.write('app/assets/config/manifest.js', '//= link jimothy_manifest.js', mode: 'a+')
else
puts "#{cyan}\nš¤ Error: user model already exists. Skipping jimothy install."
end
puts "\e[0m"
end
|