Class: Registration::RegistrationGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_routesObject



20
21
22
23
24
# File 'lib/generators/registration/registration_generator.rb', line 20

def add_routes
  route "resource :registration, only: [:new, :create]"
  route "get \"sign_in\", to: \"sessions#new\", as: :sign_in"
  route "delete \"sign_out\", to: \"sessions#destroy\", as: :sign_out"
end

#create_controllerObject



16
17
18
# File 'lib/generators/registration/registration_generator.rb', line 16

def create_controller
  template "registrations_controller.rb", "app/controllers/registrations_controller.rb"
end

#create_helperObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/registration/registration_generator.rb', line 26

def create_helper
  create_file "app/helpers/authentication_helper.rb", <<~RUBY
    module AuthenticationHelper
      def link_to_sign_in_or_out
        if authenticated?
          button_to "Sign Out", sign_out_path, method: :delete
        else
          link_to "Sign In", sign_in_path
        end
      end

      def show_username_if_signed_in
        if authenticated?
          "Signed in as \#{Current.user.email_address}"
        end
      end
    end
  RUBY

  # Inject include statement at the correct place
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController", <<~RUBY
    include AuthenticationHelper
  RUBY
end

#create_sign_inObject



12
13
14
# File 'lib/generators/registration/registration_generator.rb', line 12

def 
  template "sign_in_form.html.erb", "app/views/sessions/new.html.erb", force: true
end

#create_viewObject



8
9
10
# File 'lib/generators/registration/registration_generator.rb', line 8

def create_view
  template "registration_form.html.erb", "app/views/registrations/new.html.erb"
end