Class: RailsI18nOnair::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_route_mountingObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/rails_i18n_onair/install_generator.rb', line 41

def add_route_mounting
  say "\n━━━ Adding Routes ━━━", :bold

  route_code = 'mount RailsI18nOnair::Engine, at: "/i18n"'

  if File.read("config/routes.rb").include?(route_code)
    say "Routes already mounted", :yellow
  else
    route route_code
    say "✓ Mounted RailsI18nOnair::Engine at /i18n", :green
  end
end

#copy_initializerObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/rails_i18n_onair/install_generator.rb', line 13

def copy_initializer
  @storage_mode = options[:storage_mode]
  if @storage_mode == "database"
    say "Storage mode set to 'database'. Translation migration will be installed.", :green
    template "rails_i18n_onair_database.rb", "config/initializers/rails_i18n_onair.rb"
  else      
    say "Storage mode set to 'file'. Translation migration will be skipped.", :green
    template "rails_i18n_onair_file.rb", "config/initializers/rails_i18n_onair.rb"
  end
end

#create_initial_translatorObject



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
# File 'lib/generators/rails_i18n_onair/install_generator.rb', line 64

def create_initial_translator
  return if options[:skip_translator]

  say "\n━━━ Creating Initial Translator Account ━━━", :bold
  say "You need a translator account to access the i18n dashboard.\n", :cyan

  username = ask("Enter username:", :green, default: "admin")
  password = ask("Enter password (min 6 characters):", :green, echo: false)

  while password.to_s.length < 6
    say "\nPassword must be at least 6 characters!", :red
    password = ask("Enter password:", :green, echo: false)
  end

  password_confirmation = ask("\nConfirm password:", :green, echo: false)

  while password != password_confirmation
    say "\nPasswords don't match!", :red
    password = ask("Enter password:", :green, echo: false)
    password_confirmation = ask("Confirm password:", :green, echo: false)
  end

  create_file "db/seeds/rails_i18n_onair.rb", <<~RUBY
    # Create initial translator for RailsI18nOnair
    unless RailsI18nOnair::Translator.exists?(username: "#{username}")
      RailsI18nOnair::Translator.create!(
        username: "#{username}",
        password: "#{password}"
      )
      puts "✓ Created translator: #{username}"
    end
  RUBY

  append_to_file "db/seeds.rb", "\n# RailsI18nOnair seeds\nload Rails.root.join('db/seeds/rails_i18n_onair.rb')\n"

  say "\n"
  if yes?("Create translator account now? (y/n)", :green)
    rails_command "runner 'RailsI18nOnair::Translator.create!(username: \"#{username}\", password: \"#{password}\")'"
    say "✓ Translator account created successfully!", :green
  else
    say "Run 'rails db:seed' to create the translator account later.", :yellow
  end
end

#install_migrationsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/rails_i18n_onair/install_generator.rb', line 24

def install_migrations
  say "\n━━━ Installing Migrations ━━━", :bold

  # Always install translator migration (required for authentication)
  say "Installing translator migration (required)...", :green
  rake "rails_i18n_onair:install:migrations:translator"

  # Only install translation migration if using database mode
  if @storage_mode == "database"
    sleep 1 # Ensure unique timestamps
    say "\nInstalling translation migration (database mode)...", :green
    rake "rails_i18n_onair:install:migrations:translation"
  else
    say "\nSkipping translation migration (file mode selected)", :yellow
  end
end

#run_migrationsObject



54
55
56
57
58
59
60
61
62
# File 'lib/generators/rails_i18n_onair/install_generator.rb', line 54

def run_migrations
  say "\n━━━ Running Migrations ━━━", :bold

  if yes?("Run migrations now? (y/n)", :green)
    rails_command "db:migrate"
  else
    say "Remember to run 'rails db:migrate' later!", :yellow
  end
end

#show_readmeObject



108
109
110
# File 'lib/generators/rails_i18n_onair/install_generator.rb', line 108

def show_readme
  readme "README" if behavior == :invoke
end