railg 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/generators/railg/login/login_generator.rb +46 -36
- data/lib/railg/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a90c5a8b0483f94e157d30f0e29ea813385d30a91bcac615ba91763f8afcd3dc
|
4
|
+
data.tar.gz: 6ab4090e2732dca0cc8b39b1293d4fe68b5378c4deee51de5d162bfa11182a8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90d5b2ce19c9eb5ab879e2118e8f930372c017de5b14b9b0236be7247cb7fa20d71f1b031bb1277ded322acb41328a3e5df1c630b0c5835bb1df71304f663c65
|
7
|
+
data.tar.gz: 56db56a6e2c1edf3dea2be49451e2fa16dd6120cfb26f932e49367c16a0cc0175c8d8d64ae14000f5bd7f5aad7c6ede5d549f01b8698d84730b4d8263f4f8acc
|
@@ -1,34 +1,44 @@
|
|
1
1
|
module Railg
|
2
|
-
class LoginGenerator < ::Rails::Generators::
|
2
|
+
class LoginGenerator < ::Rails::Generators::NamedBase
|
3
3
|
source_root File.expand_path('templates', __dir__)
|
4
4
|
|
5
|
+
argument :id_name, type: :string, default: '#{id_name}', banner: 'column name like email'
|
6
|
+
|
7
|
+
check_class_collision
|
8
|
+
|
5
9
|
def gem_bcrypt
|
6
10
|
gem 'bcrypt'
|
7
11
|
end
|
8
12
|
|
13
|
+
def insert_header
|
14
|
+
insert_into_file 'app/views/layouts/application.html.erb', <<-TAG, after: " <body>\n"
|
15
|
+
<%= render 'header' %>
|
16
|
+
TAG
|
17
|
+
end
|
18
|
+
|
9
19
|
def add_route
|
10
20
|
route 'resource :session, only: %i[new create destroy]'
|
11
|
-
route
|
21
|
+
route "resources :#{plural_route_name}, only: %i[new create]"
|
12
22
|
end
|
13
23
|
|
14
24
|
def create_account_model
|
15
|
-
create_file
|
25
|
+
create_file "app/models/#{file_path}.rb", <<-EOT
|
16
26
|
# frozen_string_literal: true
|
17
27
|
|
18
|
-
class
|
28
|
+
class #{class_name} < ApplicationRecord
|
19
29
|
has_secure_password
|
20
30
|
|
21
|
-
validates
|
31
|
+
validates :#{id_name}, presence: true, uniqueness: true
|
22
32
|
end
|
23
33
|
EOT
|
24
34
|
|
25
|
-
create_file
|
26
|
-
class
|
35
|
+
create_file "db/migrate/20181105044958_create_#{plural_table_name}.rb", <<-EOT
|
36
|
+
class Create#{plural_table_name.capitalize} < ActiveRecord::Migration[5.2]
|
27
37
|
def change
|
28
|
-
create_table
|
29
|
-
t.string
|
38
|
+
create_table :#{plural_table_name} do |t|
|
39
|
+
t.string :#{id_name}, null: false
|
30
40
|
t.string :password_digest, null: false
|
31
|
-
t.index
|
41
|
+
t.index :#{id_name}, unique: true
|
32
42
|
|
33
43
|
t.timestamps
|
34
44
|
end
|
@@ -48,18 +58,18 @@ nav.navbar
|
|
48
58
|
.buttons
|
49
59
|
- unless signed_in?
|
50
60
|
= link_to 'sign in', new_session_path, class: 'button is-text'
|
51
|
-
= link_to 'sign up',
|
61
|
+
= link_to 'sign up', #{new_helper}, class: 'button is-text'
|
52
62
|
- else
|
53
63
|
= f.submit 'sign out', class: 'button is-text'
|
54
64
|
EOT
|
55
65
|
|
56
|
-
create_file
|
66
|
+
create_file "app/views/#{plural_file_name}/new.html.slim", <<-EOT
|
57
67
|
section.section
|
58
68
|
.container
|
59
|
-
= form_with model:
|
69
|
+
= form_with model: #{redirect_resource_name} do |f|
|
60
70
|
.field
|
61
71
|
.control
|
62
|
-
= f.text_field
|
72
|
+
= f.text_field :#{id_name}, class: 'input'
|
63
73
|
.field
|
64
74
|
.control
|
65
75
|
= f.password_field :password, class: 'input'
|
@@ -77,7 +87,7 @@ section.section
|
|
77
87
|
= form_with url: session_url do |f|
|
78
88
|
.field
|
79
89
|
.control
|
80
|
-
= f.text_field
|
90
|
+
= f.text_field :#{id_name}, class: 'input'
|
81
91
|
.field
|
82
92
|
.control
|
83
93
|
= f.password_field :password, class: 'input'
|
@@ -93,18 +103,18 @@ section.section
|
|
93
103
|
end
|
94
104
|
|
95
105
|
def create_controller_files
|
96
|
-
create_file
|
106
|
+
create_file "app/controllers/#{plural_name}_controller.rb", <<-EOT
|
97
107
|
# frozen_string_literal: true
|
98
108
|
|
99
|
-
class
|
109
|
+
class #{plural_name.capitalize}Controller < ApplicationController
|
100
110
|
def new
|
101
|
-
|
111
|
+
#{redirect_resource_name} = #{class_name}.new
|
102
112
|
end
|
103
113
|
|
104
114
|
def create
|
105
|
-
|
115
|
+
#{redirect_resource_name} = #{class_name}.new(#{singular_name}_params)
|
106
116
|
|
107
|
-
if
|
117
|
+
if #{redirect_resource_name}.save
|
108
118
|
redirect_to new_session_path
|
109
119
|
else
|
110
120
|
render :new
|
@@ -113,8 +123,8 @@ class AccountsController < ApplicationController
|
|
113
123
|
|
114
124
|
private
|
115
125
|
|
116
|
-
def
|
117
|
-
params.require(
|
126
|
+
def #{singular_name}_params
|
127
|
+
params.require(:#{singular_name}).permit(:#{id_name}, :password, :password_confirmation)
|
118
128
|
end
|
119
129
|
end
|
120
130
|
EOT
|
@@ -125,7 +135,7 @@ end
|
|
125
135
|
class AuthController < ApplicationController
|
126
136
|
rescue_from 'SessionManager::NoSigninError', with: -> { redirect_to new_session_path }
|
127
137
|
|
128
|
-
before_action :
|
138
|
+
before_action :current_#{singular_name} # require signin by raising SessionManager::NoSigninError
|
129
139
|
end
|
130
140
|
EOT
|
131
141
|
|
@@ -137,10 +147,10 @@ class SessionsController < ApplicationController
|
|
137
147
|
end
|
138
148
|
|
139
149
|
def create
|
140
|
-
|
150
|
+
#{singular_name} = #{class_name}.find_by(#{id_name}: params[:#{id_name}])
|
141
151
|
|
142
|
-
if
|
143
|
-
retain_session(
|
152
|
+
if #{singular_name} && #{singular_name}.authenticate(params[:password])
|
153
|
+
retain_session(#{singular_name}, remember: params[:remember])
|
144
154
|
redirect_to root_path
|
145
155
|
else
|
146
156
|
render :new
|
@@ -159,7 +169,7 @@ end
|
|
159
169
|
module SessionManager
|
160
170
|
extend ActiveSupport::Concern
|
161
171
|
|
162
|
-
SESSION_KEY =
|
172
|
+
SESSION_KEY = :#{singular_name}_id
|
163
173
|
NoSigninError = Class.new(StandardError)
|
164
174
|
|
165
175
|
included do
|
@@ -168,8 +178,8 @@ module SessionManager
|
|
168
178
|
|
169
179
|
private
|
170
180
|
|
171
|
-
def retain_session(
|
172
|
-
session[SESSION_KEY] =
|
181
|
+
def retain_session(#{singular_name}, remember: false)
|
182
|
+
session[SESSION_KEY] = #{singular_name}.id
|
173
183
|
|
174
184
|
if remember
|
175
185
|
# TODO: impl
|
@@ -181,22 +191,22 @@ module SessionManager
|
|
181
191
|
end
|
182
192
|
|
183
193
|
# TODO: refact
|
184
|
-
def
|
194
|
+
def current_#{singular_name}
|
185
195
|
if (id = session[SESSION_KEY])
|
186
|
-
|
196
|
+
#{class_name}.find(id)
|
187
197
|
elsif (id = cookies.signed[SESSION_KEY])
|
188
|
-
|
189
|
-
raise NoSigninError unless
|
198
|
+
#{singular_name} = #{class_name}.find(id)
|
199
|
+
raise NoSigninError unless #{singular_name}.authenticated?(cookies[:remember_token])
|
190
200
|
|
191
|
-
sign_in(
|
192
|
-
|
201
|
+
sign_in(#{singular_name}) # sessionにも保存
|
202
|
+
#{singular_name}
|
193
203
|
else
|
194
204
|
raise NoSigninError
|
195
205
|
end
|
196
206
|
end
|
197
207
|
|
198
208
|
def signed_in?
|
199
|
-
|
209
|
+
current_#{singular_name}
|
200
210
|
true
|
201
211
|
rescue NoSigninError
|
202
212
|
false
|
data/lib/railg/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: railg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nishisuke
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|