Class: Authentication::Generators::EmailGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_gemsObject



71
72
73
74
# File 'lib/generators/authentication/email/email_generator.rb', line 71

def add_gems
  gem 'warden', '~> 1.2.0'
  gem 'bcrypt-ruby'
end

#add_helper_methodsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/generators/authentication/email/email_generator.rb', line 39

def add_helper_methods
  insert_into_file 'app/controllers/application_controller.rb', after: /:exception/ do
    <<-EOS


  helper_method :current_#{resource_name}, :#{resource_name}_signed_in?, :warden_message

protected
  def current_#{resource_name}
    warden.user(scope: :#{resource_name})
  end

  def #{resource_name}_signed_in?
    warden.authenticate?(scope: :#{resource_name})
  end

  def authenticate!
    redirect_to root_path, notice: t('.not_logged') unless #{resource_name}_signed_in?
  end

  def warden_message
    warden.message
  end

  def warden
    request.env['warden']
  end
    EOS
  end

end

#add_routesObject



23
24
25
26
27
28
29
30
# File 'lib/generators/authentication/email/email_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 "delete 'log_out' => 'sessions#destroy', as: :log_out"

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

#add_translationsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/generators/authentication/email/email_generator.rb', line 76

def add_translations
  insert_into_file "config/locales/en.yml", after: 'en:' do
  <<-EOS

  sessions:
new:
  log_in: 'Log in'
create:
  invalid_credentials: 'Your credentials are invalid'
  logged_in: 'Welcome back!'
destroy:
  logged_out: 'See you later!'
  #{resource_pluralize}:
new:
  create: 'Create #{resource_name}'
create:
  sign_up: 'Welcome to your new account!'
  EOS
  end
end

#copy_controller_filesObject



8
9
10
11
# File 'lib/generators/authentication/email/email_generator.rb', line 8

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

#copy_view_filesObject



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

def copy_view_files
  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

#copy_warden_fileObject



97
98
99
# File 'lib/generators/authentication/email/email_generator.rb', line 97

def copy_warden_file
  template 'warden.rb', File.join('config', 'initializers', 'warden.rb')
end

#copy_warden_strategiesObject



101
102
103
# File 'lib/generators/authentication/email/email_generator.rb', line 101

def copy_warden_strategies
  template 'database_authentication.rb', File.join('lib', 'strategies', 'database_authentication.rb')
end

#generate_userObject



32
33
34
35
36
37
# File 'lib/generators/authentication/email/email_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

#instructionsObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/generators/authentication/email/email_generator.rb', line 105

def instructions
  message = "There are a few manual steps that you need to take care of\n\n"
  message << "1. Run bundle command to install new gems.\n"
  message << "2. Be sure that to have definition for root in your routes.\n"
  message << "3. Run rake db:migrate to add your #{resource_pluralize} table.\n"
  message << "4. Inspect warden initializer at config/initializers/warden.rb\n"
  message << "   and update the failure_app if need it.\n"
  message << "5. Inspect generated files and learn how authentication was implemented.\n\n"

  puts message
end