Class: ActiveRegistration::InstallGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



6
7
8
# File 'lib/generators/active_registration/install_generator.rb', line 6

def self.next_migration_number(dirname)
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

Instance Method Details

#add_routesObject



22
23
24
25
26
27
28
# File 'lib/generators/active_registration/install_generator.rb', line 22

def add_routes
  route "    resource :registration, only: [ :new, :create ] do\n      get :confirm, on: :collection\n    end\n  ROUTE\nend\n"

#copy_migrationObject



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

def copy_migration
  migration_template "add_active_registration_fields_to_users.rb", "db/migrate/add_active_registration_fields_to_users.rb"
end

#generate_controllerObject



14
15
16
# File 'lib/generators/active_registration/install_generator.rb', line 14

def generate_controller
  copy_file "registrations_controller.rb", "app/controllers/registrations_controller.rb"
end

#generate_mailerObject



30
31
32
33
# File 'lib/generators/active_registration/install_generator.rb', line 30

def generate_mailer
  copy_file "confirmation_mailer.rb", "app/mailers/confirmation_mailer.rb"
  directory "views/confirmation_mailer", "app/views/confirmation_mailer"
end

#generate_viewsObject



18
19
20
# File 'lib/generators/active_registration/install_generator.rb', line 18

def generate_views
  directory "views/registrations", "app/views/registrations"
end

#inject_user_methodsObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/active_registration/install_generator.rb', line 35

def inject_user_methods
  user_model_path = "app/models/user.rb"
  return unless File.exist?(user_model_path)

  content = File.read(user_model_path)

  if content.include?("validates :email_address, presence: true, uniqueness: true") &&
     content.include?("def confirm!") &&
     content.include?("def generate_confirmation_token")
    say "User model already contains active_registration methods, skipping injection.", :yellow
    return
  end

  inject_validations_and_hooks(user_model_path)
  inject_all_methods(user_model_path)
rescue Errno::ENOENT
  say "User model not found. Please add the registration methods to your User model manually.", :yellow
end