railg 0.1.0 → 0.2.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: dce7fc9988558ac74aa8a2d7b35ec827964ffa179a48caa2bf79fcaf3ff08ccd
4
- data.tar.gz: 10b3d8140d88effc5c306a31357c5ea2de1ee17cfbed1a70f68c2bdafa0c06c7
3
+ metadata.gz: f1c10daee459881fc58275367eca4336dd9016942adbcdc845feca9ebab8e0f5
4
+ data.tar.gz: 1c8a2ede1197a95a208ae7d23d0e4a75a1c0ac9c2702e0eecb6780e3bc3da406
5
5
  SHA512:
6
- metadata.gz: 328944b84b0aeed780c85cb08f24e31ffd6162a5683abea04788460df2c4eb2c5fae9dd383d0e9710a3210ab744cde18105270821c05d4aca4d41ce9382259e6
7
- data.tar.gz: 5a53b83387a98ae6b82d17273ea37db8eab243f7702b31f24df3d44b8a5af1ffa2cf5ea293dcf4557ea8a224d67f1c76369a74512f709fcf8bac09c888de35da
6
+ metadata.gz: afdda81daf225e32bda9c711bc60b30ed3692df420f5404b5f774666b965eb9e9e1010adafcb91eab91a6e2681c0adce123e96f9f134e069e07a4746d3fe0be6
7
+ data.tar.gz: e44db8aa4708fae85e31ed85663d591c7d3610a7b2d23b03ece5afd8ace1cc70a6df1da635718df7e3e6b9e49d60dd13d4d8b562b52f8b2f984de21c349f5b99
@@ -0,0 +1,15 @@
1
+ module Railg
2
+ class BulmaGenerator < ::Rails::Generators::Base
3
+ def insert_bulma_link_tag
4
+ insert_into_file 'app/views/layouts/application.html.erb', <<-TAG, before: " </head>\n"
5
+ <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css'>
6
+ TAG
7
+ end
8
+
9
+ def insert_fontawesome_script_tag
10
+ insert_into_file 'app/views/layouts/application.html.erb', <<-TAG, before: " </head>\n"
11
+ <script defer src='https://use.fontawesome.com/releases/v5.3.1/js/all.js'></script>
12
+ TAG
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module Railg
2
+ class DefaultGenerator < ::Rails::Generators::Base
3
+ def configure_generator
4
+ environment <<-EOT, env: 'development'
5
+ config.generators do |g|
6
+ g.assets false
7
+ g.helper false
8
+ g.jbuilder false
9
+ g.test_framework false
10
+ end
11
+ EOT
12
+ end
13
+
14
+ def insert_meta_tag
15
+ insert_into_file 'app/views/layouts/application.html.erb', <<-EOT, after: " <head>\n"
16
+ <meta charset='UTF-8'>
17
+ <meta name='viewport' content='width=device-width,initial-scale=1'>
18
+ EOT
19
+ end
20
+
21
+ def create_db
22
+ rake 'db:create'
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,226 @@
1
+ module Railg
2
+ class LoginGenerator < ::Rails::Generators::Base
3
+ source_root File.expand_path('templates', __dir__)
4
+
5
+ def gem_bcrypt
6
+ gem 'bcrypt'
7
+ end
8
+
9
+ def add_route
10
+ route 'resource :session, only: %i[new create destroy]'
11
+ route 'resources :accounts, only: %i[new create]'
12
+ end
13
+
14
+ def create_account_model
15
+ create_file 'app/models/account.rb', <<-EOT
16
+ # frozen_string_literal: true
17
+
18
+ class Account < ApplicationRecord
19
+ has_secure_password
20
+
21
+ validates :identifier, presence: true, uniqueness: true
22
+ end
23
+ EOT
24
+
25
+ create_file 'db/migrate/20181105044958_create_accounts.rb', <<-EOT
26
+ class CreateAccounts < ActiveRecord::Migration[5.2]
27
+ def change
28
+ create_table :accounts do |t|
29
+ t.string :identifier, null: false
30
+ t.string :password_digest, null: false
31
+ t.index :identifier, unique: true
32
+
33
+ t.timestamps
34
+ end
35
+ end
36
+ end
37
+ EOT
38
+ end
39
+
40
+ def create_view_files
41
+ create_file 'app/views/application/_header.html.slim', <<-EOT
42
+ nav.navbar
43
+ .container
44
+ .navbar-menu
45
+ .navbar-end
46
+ .navbar-item
47
+ = form_with url: session_url, method: :delete do |f|
48
+ .buttons
49
+ - unless signed_in?
50
+ = link_to 'sign in', new_session_path, class: 'button is-text'
51
+ = link_to 'sign up', new_account_path, class: 'button is-text'
52
+ - else
53
+ = f.submit 'sign out', class: 'button is-text'
54
+ EOT
55
+
56
+ create_file 'app/views/accounts/new.html.slim', <<-EOT
57
+ section.section
58
+ .container
59
+ = form_with model: @account do |f|
60
+ .field
61
+ .control
62
+ = f.text_field :identifier, class: 'input'
63
+ .field
64
+ .control
65
+ = f.password_field :password, class: 'input'
66
+ .field
67
+ .control
68
+ = f.password_field :password_confirmation, class: 'input'
69
+ .field
70
+ .control
71
+ = f.submit class: 'button'
72
+ EOT
73
+
74
+ create_file 'app/views/sessions/new.html.slim', <<-EOT
75
+ section.section
76
+ .container
77
+ = form_with url: session_url do |f|
78
+ .field
79
+ .control
80
+ = f.text_field :identifier, class: 'input'
81
+ .field
82
+ .control
83
+ = f.password_field :password, class: 'input'
84
+ .field
85
+ .control
86
+ label.checkbox
87
+ = f.check_box :remember, {}, true, false
88
+ | Remember me?
89
+ .field
90
+ .control
91
+ = f.submit class: 'button'
92
+ = form_with url: session_url, method: :delete do |f|
93
+ .field
94
+ .control
95
+ = f.submit
96
+ EOT
97
+ end
98
+
99
+ def create_controller_files
100
+ create_file 'app/controllers/accounts_controller.rb', <<-EOT
101
+ # frozen_string_literal: true
102
+
103
+ class AccountsController < ApplicationController
104
+ def new
105
+ @account = Account.new
106
+ end
107
+
108
+ def create
109
+ @account = Account.new(account_params)
110
+
111
+ if @account.save
112
+ redirect_to new_session_path
113
+ else
114
+ render :new
115
+ end
116
+ end
117
+
118
+ private
119
+
120
+ def account_params
121
+ params.require(:account).permit(:identifier, :password, :password_confirmation)
122
+ end
123
+ end
124
+ EOT
125
+
126
+ create_file 'app/controllers/auth_controller.rb', <<-EOT
127
+ # frozen_string_literal: true
128
+
129
+ class AuthController < ApplicationController
130
+ rescue_from 'SessionManager::NoSigninError', with: -> { redirect_to new_session_path }
131
+
132
+ before_action :current_account # require signin by raising SessionManager::NoSigninError
133
+ end
134
+ EOT
135
+
136
+ create_file 'app/controllers/sessions_controller.rb', <<-EOT
137
+ # frozen_string_literal: true
138
+
139
+ class SessionsController < ApplicationController
140
+ def new
141
+ end
142
+
143
+ def create
144
+ account = Account.find_by(identifier: params[:identifier])
145
+
146
+ if account && account.authenticate(params[:password])
147
+ retain_session(account, remember: params[:remember])
148
+ redirect_to root_path
149
+ else
150
+ render :new
151
+ end
152
+ end
153
+
154
+ def destroy
155
+ free_session
156
+ end
157
+ end
158
+ EOT
159
+
160
+ create_file 'app/controllers/session_manager.rb', <<-EOT
161
+ # frozen_string_literal: true
162
+
163
+ module SessionManager
164
+ extend ActiveSupport::Concern
165
+
166
+ SESSION_KEY = :account_id
167
+ NoSigninError = Class.new(StandardError)
168
+
169
+ included do
170
+ helper_method :signed_in?
171
+ end
172
+
173
+ private
174
+
175
+ def retain_session(account, remember: false)
176
+ session[SESSION_KEY] = account.id
177
+
178
+ if remember
179
+ # TODO: impl
180
+ end
181
+ end
182
+
183
+ def free_session
184
+ session[SESSION_KEY] = nil
185
+ end
186
+
187
+ # TODO: refact
188
+ def current_account
189
+ if (id = session[SESSION_KEY])
190
+ Account.find(id)
191
+ elsif (id = cookies.signed[SESSION_KEY])
192
+ account = Account.find(id)
193
+ raise NoSigninError unless account.authenticated?(cookies[:remember_token])
194
+
195
+ sign_in(account) # sessionにも保存
196
+ account
197
+ else
198
+ raise NoSigninError
199
+ end
200
+ end
201
+
202
+ def signed_in?
203
+ current_account
204
+ true
205
+ rescue NoSigninError
206
+ false
207
+ end
208
+ end
209
+ EOT
210
+
211
+ insert_into_file 'app/controllers/application_controller.rb', <<-EOT, after: "class ApplicationController < ActionController::Base\n"
212
+ include SessionManager
213
+ EOT
214
+ end
215
+
216
+ def insert_header
217
+ insert_into_file 'app/views/layouts/application.html.erb', <<-EOT, after: " <body>\n"
218
+ <%= render 'header' %>
219
+ EOT
220
+ end
221
+
222
+ def after_bundle_callback
223
+ rake 'db:migrate'
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,8 @@
1
+ module Railg
2
+ class PryGenerator < ::Rails::Generators::Base
3
+ def add_gem
4
+ gem 'pry-rails', group: :development
5
+ gem 'pry-byebug', group: :development
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module Railg
2
+ class SlimGenerator < ::Rails::Generators::Base
3
+ def add_gem
4
+ gem 'slim-rails'
5
+ gem 'html2slim', group: :development
6
+ end
7
+
8
+ def after_bundle_callback
9
+ run 'erb2slim -d app/views/layouts/application.html.erb'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ gem_group :development, :test do
2
+ gem 'rspec-rails'
3
+ gem 'factory_bot_rails'
4
+ end
5
+
6
+ after_bundle do
7
+ generate(:'rspec:install')
8
+ file 'spec/support/factory_bot.rb', <<~CODE
9
+ # frozen_string_literal: true
10
+ RSpec.configure do |config|
11
+ config.include FactoryBot::Syntax::Methods
12
+ end
13
+ CODE
14
+ insert_into_file 'spec/rails_helper.rb', <<~EOT, after: "# Add additional requires below this line. Rails is not loaded until this point!\n"
15
+ require_relative 'support/factory_bot.rb'
16
+ EOT
17
+ end
data/lib/railg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Railg
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nishisuke
@@ -69,6 +69,12 @@ files:
69
69
  - Rakefile
70
70
  - bin/console
71
71
  - bin/setup
72
+ - lib/generators/railg/bulma/bulma_generator.rb
73
+ - lib/generators/railg/default/default_generator.rb
74
+ - lib/generators/railg/login/login_generator.rb
75
+ - lib/generators/railg/pry/pry_generator.rb
76
+ - lib/generators/railg/slim/slim_generator.rb
77
+ - lib/generators/railg/test_gen.txt
72
78
  - lib/railg.rb
73
79
  - lib/railg/version.rb
74
80
  - railg.gemspec