Class: Jimothy::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/jimothy/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#install_jimothyObject



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" # use cyan color for puts
  # only run if User model doesn't exist yet
  if !Dir[Rails.root + 'app/models/*.rb'].map { |path| path.split('/')[-1] }.include? 'user.rb'
    puts "#{cyan}\nšŸ¦„ Scaffolding user model now."
    # scaffold users
    %x(rails g scaffold user name email image)
    puts "šŸ¦„ User model/views scaffolding built."
    %x(rails db:migrate)
    puts "šŸ¦„ Database migrated."
  
    # add image tag
    gsub_file 'app/views/users/_user.html.erb',/<%= user.image %>/,'<%= image_tag user.image %>'
    puts "#{cyan}šŸ¦„ _user view partial updated."
  
    # empty seed file
    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
  
    # add jimothy call
    seeds_file = File.read('db/seeds.rb')
    if seeds_file.empty? # only paste in this Jimothy::seed_users if the previous empty_seed_file was successful (ie, don't add it again if it's already there)
      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
  
    # seed users
    puts "šŸ¦„ Seeding users now."
    %x(rails db:seed)

    # add gem manifest to app manifest
    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" # restore puts color to normal
end