Class: AuthenticationGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



55
56
57
# File 'lib/generators/authentication/authentication_generator.rb', line 55

def add_gems
  gem 'bcrypt-ruby', require: 'bcrypt'
end

#add_helper_methodsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/authentication/authentication_generator.rb', line 39

def add_helper_methods
  insert_into_file 'app/controllers/application_controller.rb', after: /:exception/ do
"\n\nhelper_method :current_\#{resource_name}\n\nprivate\ndef current_\#{resource_name}\n  @current_\#{resource_name} ||= \#{resource_name.classify}.find(session[:\#{resource_name}_id]) if session[:\#{resource_name}_id]\nend\n"
  end

end

#add_routesObject



23
24
25
26
27
28
29
30
# File 'lib/generators/authentication/authentication_generator.rb', line 23

def add_routes
  route "get 'sign_up' => '#{resource_pluralize}#new', as: :sign_up"
  route "get 'log_in' => 'sessions#new', as: :log_in"
  route "get 'log_out' => 'sessions#destroy', as: :log_out"

  route "resource :#{resource_name}, only: [:create, :new]"
  route "resource :sessions, only: [:create, :new]"
end

#add_translationsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/authentication/authentication_generator.rb', line 59

def add_translations
  insert_into_file "config/locales/#{I18n.default_locale.to_s}.yml", after: 'en:' do
"\nsessions:\n  new:\n    log_in: 'Log in'\n  create:\n    invalid_credentials: 'Your credentials are invalid'\n    logged_in: 'Welcome back!'\n  destroy:\n    logged_out: 'See you later!'\n\#{resource_pluralize}:\n  new:\n    create: 'Create \#{resource_name}'\n  create:\n    sign_up: 'Welcome to your new account!'\n"
  end
end

#copy_controller_filesObject



7
8
9
10
# File 'lib/generators/authentication/authentication_generator.rb', line 7

def copy_controller_files
  template 'identities_controller.rb', "app/controllers/#{resource_pluralize}_controller.rb"
  template 'sessions_controller.rb', 'app/controllers/sessions_controller.rb'
end

#copy_view_filesObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/authentication/authentication_generator.rb', line 12

def copy_view_files
  puts ">>>#{options}"
  if options[:haml]
    template 'haml/identity_new.html.haml', "app/views/#{resource_pluralize}/new.html.haml"
    template 'haml/session_new.html.haml', "app/views/sessions/new.html.haml"
  else
    template 'erb/identity_new.html.erb', "app/views/#{resource_pluralize}/new.html.erb"
    template 'erb/session_new.html.erb', "app/views/sessions/new.html.erb"
  end
end

#generate_userObject



32
33
34
35
36
37
# File 'lib/generators/authentication/authentication_generator.rb', line 32

def generate_user
  if Dir["db/migrate/*create_#{resource_pluralize}.rb"].empty?
    template 'create_identities.rb', "db/migrate/#{migration_name}"
  end
  template 'identity.rb', "app/models/#{resource_name}.rb"
end