railg 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77dea236a4eb31e138aa38b9d2305a270253917296aa65791e720e19c5b386a8
4
- data.tar.gz: d1c91f99acf1a742c5c36a65d50858f0fd87bf4da201dc46729a51357e059186
3
+ metadata.gz: a90c5a8b0483f94e157d30f0e29ea813385d30a91bcac615ba91763f8afcd3dc
4
+ data.tar.gz: 6ab4090e2732dca0cc8b39b1293d4fe68b5378c4deee51de5d162bfa11182a8d
5
5
  SHA512:
6
- metadata.gz: 4707998ddc09d7f45018ef938faedfaf3f71d40f67ce479cc8fe56fcf47b5b64e86a3a000530ba440bf9d9041312bcb069a55328c0ceea889621edb4c335dde9
7
- data.tar.gz: 22326710ace63c26835e8545740a56f92457e1e0b84005c9609e8fde00ce5303215bd53278082055707288db0bb446368b829c3f594e3dd03a81a9ea54da3cc3
6
+ metadata.gz: 90d5b2ce19c9eb5ab879e2118e8f930372c017de5b14b9b0236be7247cb7fa20d71f1b031bb1277ded322acb41328a3e5df1c630b0c5835bb1df71304f663c65
7
+ data.tar.gz: 56db56a6e2c1edf3dea2be49451e2fa16dd6120cfb26f932e49367c16a0cc0175c8d8d64ae14000f5bd7f5aad7c6ede5d549f01b8698d84730b4d8263f4f8acc
@@ -1,34 +1,44 @@
1
1
  module Railg
2
- class LoginGenerator < ::Rails::Generators::Base
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 'resources :accounts, only: %i[new create]'
21
+ route "resources :#{plural_route_name}, only: %i[new create]"
12
22
  end
13
23
 
14
24
  def create_account_model
15
- create_file 'app/models/account.rb', <<-EOT
25
+ create_file "app/models/#{file_path}.rb", <<-EOT
16
26
  # frozen_string_literal: true
17
27
 
18
- class Account < ApplicationRecord
28
+ class #{class_name} < ApplicationRecord
19
29
  has_secure_password
20
30
 
21
- validates :identifier, presence: true, uniqueness: true
31
+ validates :#{id_name}, presence: true, uniqueness: true
22
32
  end
23
33
  EOT
24
34
 
25
- create_file 'db/migrate/20181105044958_create_accounts.rb', <<-EOT
26
- class CreateAccounts < ActiveRecord::Migration[5.2]
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 :accounts do |t|
29
- t.string :identifier, null: false
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 :identifier, unique: true
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', new_account_path, class: 'button is-text'
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 'app/views/accounts/new.html.slim', <<-EOT
66
+ create_file "app/views/#{plural_file_name}/new.html.slim", <<-EOT
57
67
  section.section
58
68
  .container
59
- = form_with model: @account do |f|
69
+ = form_with model: #{redirect_resource_name} do |f|
60
70
  .field
61
71
  .control
62
- = f.text_field :identifier, class: 'input'
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 :identifier, class: 'input'
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 'app/controllers/accounts_controller.rb', <<-EOT
106
+ create_file "app/controllers/#{plural_name}_controller.rb", <<-EOT
97
107
  # frozen_string_literal: true
98
108
 
99
- class AccountsController < ApplicationController
109
+ class #{plural_name.capitalize}Controller < ApplicationController
100
110
  def new
101
- @account = Account.new
111
+ #{redirect_resource_name} = #{class_name}.new
102
112
  end
103
113
 
104
114
  def create
105
- @account = Account.new(account_params)
115
+ #{redirect_resource_name} = #{class_name}.new(#{singular_name}_params)
106
116
 
107
- if @account.save
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 account_params
117
- params.require(:account).permit(:identifier, :password, :password_confirmation)
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 :current_account # require signin by raising SessionManager::NoSigninError
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
- account = Account.find_by(identifier: params[:identifier])
150
+ #{singular_name} = #{class_name}.find_by(#{id_name}: params[:#{id_name}])
141
151
 
142
- if account && account.authenticate(params[:password])
143
- retain_session(account, remember: params[:remember])
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 = :account_id
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(account, remember: false)
172
- session[SESSION_KEY] = account.id
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 current_account
194
+ def current_#{singular_name}
185
195
  if (id = session[SESSION_KEY])
186
- Account.find(id)
196
+ #{class_name}.find(id)
187
197
  elsif (id = cookies.signed[SESSION_KEY])
188
- account = Account.find(id)
189
- raise NoSigninError unless account.authenticated?(cookies[:remember_token])
198
+ #{singular_name} = #{class_name}.find(id)
199
+ raise NoSigninError unless #{singular_name}.authenticated?(cookies[:remember_token])
190
200
 
191
- sign_in(account) # sessionにも保存
192
- account
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
- current_account
209
+ current_#{singular_name}
200
210
  true
201
211
  rescue NoSigninError
202
212
  false
data/lib/railg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Railg
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.1
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-17 00:00:00.000000000 Z
11
+ date: 2018-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler