Class: ReferralBox::Generators::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_model_methodsObject



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
# File 'lib/generators/referral_box/install/install_generator.rb', line 57

def add_model_methods
  if @model_class.present?
    model_file = "app/models/#{@model_class.underscore}.rb"
    
    if File.exist?(model_file)
      content = File.read(model_file)
      
      # Check if methods already exist
      if content.include?('has_many :referral_box_transactions')
        puts "ReferralBox methods already exist in #{@model_class} model."
      else
        # Try to inject after the class definition
        if content.match(/class #{@model_class}\s+<\s+ApplicationRecord/)
          inject_into_file model_file, after: "class #{@model_class} < ApplicationRecord" do
            "\n  " + generate_model_methods(@model_class)
          end
          puts "Added ReferralBox methods to #{@model_class} model."
        elsif content.match(/class #{@model_class}\s+<\s+ActiveRecord::Base/)
          inject_into_file model_file, after: "class #{@model_class} < ActiveRecord::Base" do
            "\n  " + generate_model_methods(@model_class)
          end
          puts "Added ReferralBox methods to #{@model_class} model."
        else
          # If we can't find the class definition, show manual instructions
          show_manual_model_instructions(@model_class)
        end
      end
    else
      show_manual_model_instructions(@model_class)
    end
  end
end

#add_model_migrationObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/referral_box/install/install_generator.rb', line 42

def add_model_migration
  if @model_class.present?
    generate_migration = "AddReferralBoxTo#{@model_class.pluralize}"
    migration_content = generate_migration_content(@model_class)
    
    create_file "db/migrate/#{Time.current.strftime('%Y%m%d%H%M%S')}_#{generate_migration.underscore}.rb", migration_content
    
    puts "\nMigration generated for #{@model_class} model!"
    puts "Run 'rails db:migrate' to apply it."
  else
    puts "Warning: Could not determine model class name."
    puts "Please manually create migration for your model."
  end
end

#ask_for_model_nameObject



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
# File 'lib/generators/referral_box/install/install_generator.rb', line 14

def ask_for_model_name
  puts "\n" + "="*60
  puts "ReferralBox Model Configuration"
  puts "="*60
  
  # Detect existing models
  model_files = Dir.glob("app/models/*.rb")
  existing_models = model_files.map { |f| File.basename(f, '.rb').classify }
  
  if existing_models.any?
    puts "\nExisting models found: #{existing_models.join(', ')}"
  end
  
  @model_class = ask("What is your user model class name? (e.g., User, Customer, Account, Member):", default: "User")
  
  # Confirm the choice
  puts "\nYou selected: #{@model_class}"
  confirm = ask("Is this correct? (y/n):", default: "y")
  
  unless confirm.downcase.start_with?('y')
    puts "Please run the generator again with the correct model name."
    exit
  end
  
  # Update the initializer with the selected model
  update_initializer_with_model(@model_class)
end

#create_initializerObject



10
11
12
# File 'lib/generators/referral_box/install/install_generator.rb', line 10

def create_initializer
  template "initializer.rb", "config/initializers/referral_box.rb"
end

#show_instructionsObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/referral_box/install/install_generator.rb', line 90

def show_instructions
  puts "\n" + "="*60
  puts "ReferralBox Installation Complete!"
  puts "="*60
  puts "\nNext steps:"
  puts "1. Run 'rails db:migrate' to create the database tables"
  puts "2. Configure ReferralBox in config/initializers/referral_box.rb"
  puts "3. Visit /referral_box to access the admin dashboard"
  puts "\nFor more information, see the documentation!"
  puts "="*60
end