Class: Maquina::Generators::RegistrationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/maquina/registration/registration_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/maquina/registration/registration_generator.rb', line 16

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

  if @prev_migration_nr && @prev_migration_nr >= current
    current = @prev_migration_nr.succ
  end

  existing = Dir.glob("#{dirname}/[0-9]*_*.rb").map { |f| File.basename(f).split("_", 2).first }
  max_existing = existing.max

  if max_existing && max_existing >= current
    current = max_existing.succ
  end

  @prev_migration_nr = current
end

Instance Method Details

#add_migrationsObject

  1. Migrations



101
102
103
104
105
106
# File 'lib/generators/maquina/registration/registration_generator.rb', line 101

def add_migrations
  migration_template "migration_create_accounts.rb.tt",
    "db/migrate/create_accounts.rb"
  migration_template "migration_add_account_fields_to_users.rb.tt",
    "db/migrate/add_account_fields_to_users.rb"
end

#add_routesObject

  1. Routes



78
79
80
81
82
83
84
85
# File 'lib/generators/maquina/registration/registration_generator.rb', line 78

def add_routes
  route_content = <<~RUBY
    # Registration routes (generated by maquina:registration)
    resources :registrations, only: [:new, :create]
  RUBY

  route route_content
end

#create_account_modelObject

  1. Account model



41
42
43
# File 'lib/generators/maquina/registration/registration_generator.rb', line 41

def 
  template "app/models/account.rb.tt", "app/models/account.rb"
end

#create_locale_filesObject

  1. Locale files



72
73
74
75
# File 'lib/generators/maquina/registration/registration_generator.rb', line 72

def create_locale_files
  copy_file "config/locales/registration.en.yml", "config/locales/registration.en.yml"
  copy_file "config/locales/registration.es.yml", "config/locales/registration.es.yml"
end

#create_registration_controllerObject

  1. Registration controller



56
57
58
59
# File 'lib/generators/maquina/registration/registration_generator.rb', line 56

def create_registration_controller
  template "app/controllers/registrations_controller.rb.tt",
    "app/controllers/registrations_controller.rb"
end

#create_viewsObject

  1. Views



62
63
64
65
66
67
68
69
# File 'lib/generators/maquina/registration/registration_generator.rb', line 62

def create_views
  return if options[:skip_views]

  template "app/views/registrations/new.html.erb.tt",
    "app/views/registrations/new.html.erb"
  template "app/views/sessions/new.html.erb.tt",
    "app/views/sessions/new.html.erb"
end

#enable_bcryptObject

  1. Enable bcrypt



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/maquina/registration/registration_generator.rb', line 88

def enable_bcrypt
  gemfile_path = File.join(destination_root, "Gemfile")
  if File.exist?(gemfile_path)
    content = File.read(gemfile_path)
    if content.include?('# gem "bcrypt"')
      gsub_file "Gemfile", '# gem "bcrypt"', 'gem "bcrypt"'
    elsif !content.include?('gem "bcrypt"')
      append_to_file "Gemfile", "\ngem \"bcrypt\"\n"
    end
  end
end

#run_rails_authenticationObject

  1. Run Rails authentication generator



34
35
36
37
38
# File 'lib/generators/maquina/registration/registration_generator.rb', line 34

def run_rails_authentication
  return unless rails_app?

  generate "authentication"
end

#show_post_installObject

  1. Post-install message



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/generators/maquina/registration/registration_generator.rb', line 109

def show_post_install
  return if options[:quiet]

  say ""
  say "Registration has been installed!", :green
  say ""
  say "Next steps:", :yellow
  say "  1. rails db:migrate                  # Run migrations"
  say "  2. rails server                      # Start the app"
  say "  3. Visit /registrations/new           # Sign-up page"
  say "  4. Visit /session/new                # Sign-in page"
  say ""
  say "Customization:", :yellow
  say "  - Edit app/models/account.rb to add account fields"
  say "  - Edit app/models/user.rb to customize roles or add fields"
  say "  - Edit config/locales/registration.*.yml to customize messages"
  say "  - Edit views to match your design"
  say ""
end

#update_current_modelObject

  1. Update Current model (replace Rails-generated)



51
52
53
# File 'lib/generators/maquina/registration/registration_generator.rb', line 51

def update_current_model
  template "app/models/current.rb.tt", "app/models/current.rb"
end

#update_user_modelObject

  1. Update User model (replace Rails-generated)



46
47
48
# File 'lib/generators/maquina/registration/registration_generator.rb', line 46

def update_user_model
  template "app/models/user.rb.tt", "app/models/user.rb"
end