Class: Railg::LoginGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/railg/login/login_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_routeObject



19
20
21
22
# File 'lib/generators/railg/login/login_generator.rb', line 19

def add_route
  route 'resource  :session, only: %i[new create destroy]'
  route "resources :#{plural_route_name}, only: %i[new create]"
end

#after_bundle_callbackObject



228
229
230
# File 'lib/generators/railg/login/login_generator.rb', line 228

def after_bundle_callback
  rake 'db:migrate'
end

#create_account_modelObject



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

def 
  create_file "app/models/#{file_path}.rb", <<-EOT
# frozen_string_literal: true

class #{class_name} < ApplicationRecord
  has_secure_password

  validates :#{id_name}, presence: true, uniqueness: true
end
  EOT

  create_file "db/migrate/20181105044958_create_#{plural_table_name}.rb", <<-EOT
class Create#{plural_table_name.capitalize} < ActiveRecord::Migration[5.2]
  def change
create_table :#{plural_table_name} do |t|
  t.string :#{id_name}, null: false
  t.string :password_digest, null: false
  t.index :#{id_name}, unique: true

  t.timestamps
end
  end
end
EOT
end

#create_controller_filesObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/generators/railg/login/login_generator.rb', line 105

def create_controller_files
  create_file "app/controllers/#{plural_name}_controller.rb", <<-EOT
# frozen_string_literal: true

class #{plural_name.capitalize}Controller < ApplicationController
  def new
#{redirect_resource_name} = #{class_name}.new
  end

  def create
#{redirect_resource_name} = #{class_name}.new(#{singular_name}_params)

if #{redirect_resource_name}.save
  redirect_to new_session_path
else
  render :new
end
  end

  private

  def #{singular_name}_params
params.require(:#{singular_name}).permit(:#{id_name}, :password, :password_confirmation)
  end
end
  EOT

  create_file 'app/controllers/auth_controller.rb', <<-EOT
# frozen_string_literal: true

class AuthController < ApplicationController
  rescue_from 'SessionManager::NoSigninError', with: -> { redirect_to new_session_path }

  before_action :current_#{singular_name} # require signin by raising SessionManager::NoSigninError
end
  EOT

  create_file 'app/controllers/sessions_controller.rb', <<-EOT
# frozen_string_literal: true

class SessionsController < ApplicationController
  def new
  end

  def create
#{singular_name} = #{class_name}.find_by(#{id_name}: params[:#{id_name}])

if #{singular_name} && #{singular_name}.authenticate(params[:password])
  retain_session(#{singular_name}, remember: params[:remember])
  redirect_to root_path
else
  render :new
end
  end

  def destroy
free_session
  end
end
  EOT

  create_file 'app/controllers/session_manager.rb', <<-EOT
# frozen_string_literal: true

module SessionManager
  extend ActiveSupport::Concern

  SESSION_KEY = :#{singular_name}_id
  NoSigninError = Class.new(StandardError)

  included do
helper_method :signed_in?
  end

  private

  def retain_session(#{singular_name}, remember: false)
session[SESSION_KEY] = #{singular_name}.id

if remember
  # TODO: impl
end
  end

  def free_session
session[SESSION_KEY] = nil
  end

  # TODO: refact
  def current_#{singular_name}
if (id = session[SESSION_KEY])
  #{class_name}.find(id)
elsif (id = cookies.signed[SESSION_KEY])
  #{singular_name} = #{class_name}.find(id)
  raise NoSigninError unless #{singular_name}.authenticated?(cookies[:remember_token])

  sign_in(#{singular_name}) # sessionにも保存
  #{singular_name}
else
  raise NoSigninError
end
  end

  def signed_in?
current_#{singular_name}
true
  rescue NoSigninError
false
  end
end
  EOT

  insert_into_file 'app/controllers/application_controller.rb', <<-EOT, after: "class ApplicationController < ActionController::Base\n"
  include SessionManager
  EOT
end

#create_view_filesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/generators/railg/login/login_generator.rb', line 50

def create_view_files
  create_file 'app/views/application/_header.html.slim', <<-EOT
nav.navbar
  .container
.navbar-menu
  .navbar-end
    .navbar-item
      = form_with url: session_url, method: :delete do |f|
        .buttons
          - unless signed_in?
            = link_to 'sign in', new_session_path, class: 'button is-text'
            = link_to 'sign up', #{new_helper}, class: 'button is-text'
          - else
            = f.submit 'sign out', class: 'button is-text'
  EOT

  create_file "app/views/#{plural_file_name}/new.html.slim", <<-EOT
section.section
  .container
= form_with model: #{redirect_resource_name} do |f|
  .field
    .control
      = f.text_field :#{id_name}, class: 'input'
  .field
    .control
      = f.password_field :password, class: 'input'
  .field
    .control
      = f.password_field :password_confirmation, class: 'input'
  .field
    .control
      = f.submit class: 'button'
  EOT

  create_file 'app/views/sessions/new.html.slim', <<-EOT
section.section
  .container
= form_with url: session_url do |f|
  .field
    .control
      = f.text_field :#{id_name}, class: 'input'
  .field
    .control
      = f.password_field :password, class: 'input'
  .field
    .control
      label.checkbox
        = f.check_box :remember, {}, true, false
        | Remember me?
  .field
    .control
      = f.submit class: 'button'
  EOT
end

#gem_bcryptObject



9
10
11
# File 'lib/generators/railg/login/login_generator.rb', line 9

def gem_bcrypt
  gem 'bcrypt'
end

#insert_headerObject



13
14
15
16
17
# File 'lib/generators/railg/login/login_generator.rb', line 13

def insert_header
  insert_into_file 'app/views/layouts/application.html.erb', <<-TAG, after: "  <body>\n"
<%= render 'header' %>
  TAG
end