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



69
70
71
72
# File 'lib/generators/authentication/email/email_generator.rb', line 69

def add_gems
  gem "warden", "~> 1.2.0"
  gem "bcrypt"
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
# File 'lib/generators/authentication/email/email_generator.rb', line 39

def add_helper_methods
  insert_into_file "app/controllers/application_controller.rb", after: /::Base/ do
    "\n        helper_method :current_\#{resource_name}, :\#{resource_name}_signed_in?, :warden_message\n\n      protected\n        def current_\#{resource_name}\n          warden.user(scope: :\#{resource_name})\n        end\n\n        def \#{resource_name}_signed_in?\n          warden.authenticate?(scope: :\#{resource_name})\n        end\n\n        def authenticate!\n          redirect_to root_path, notice: t('.not_logged') unless \#{resource_name}_signed_in?\n        end\n\n        def warden_message\n          warden.message\n        end\n\n        def warden\n          request.env['warden']\n        end\n    EOS\n  end\nend\n"

#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', to: '#{resource_pluralize}#new', as: :sign_up"
  route "get 'log_in', to: 'sessions#new', as: :log_in"
  route "delete 'log_out', to: 'sessions#destroy', as: :log_out"

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

#add_translationsObject



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

def add_translations
  insert_into_file "config/locales/en.yml", after: "en:" do
    "\n  sessions:\n    new:\nlog_in: 'Log in'\n    create:\ninvalid_credentials: 'Your credentials are invalid'\nlogged_in: 'Welcome back!'\n    destroy:\nlogged_out: 'See you later!'\n  \#{resource_pluralize}:\n    new:\ncreate: 'Create \#{resource_name}'\n    create:\nsign_up: 'Welcome to your new account!'\n    EOS\n  end\nend\n"

#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



95
96
97
# File 'lib/generators/authentication/email/email_generator.rb', line 95

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

#copy_warden_strategiesObject



99
100
101
# File 'lib/generators/authentication/email/email_generator.rb', line 99

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



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

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