-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.rb
124 lines (100 loc) · 3.86 KB
/
template.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
gem "omniauth"
gem "omniauth-google-oauth2"
gem "omniauth-rails_csrf_protection"
gem "letter_opener", group: :development
after_bundle do
git add: "."
git commit: "-m 'chore: initial commit'"
generate :model, "User",
"name:string",
"email:string:index",
"authentication_provider:string:index",
"authentication_uid:string:index"
environment "config.action_mailer.delivery_method = :letter_opener", env: "development"
environment "config.action_mailer.perform_deliveries = true", env: "development"
insert_into_file "app/models/user.rb", after: "class User < ApplicationRecord\n" do <<~RUBY.indent(2)
validates :authentication_provider, presence: true
validates :authentication_uid, presence: true
validates :email, presence: true
def self.from_omniauth(auth)
where(authentication_uid: auth.uid, authentication_provider: auth.provider).first_or_initialize do |user|
user.email = auth.info.email
user.name = auth.info.name
end
end
RUBY
end
insert_into_file "app/controllers/application_controller.rb",
after: "class ApplicationController < ActionController::Base\n" do <<~RUBY.indent(2)
helper_method :current_user, :user_signed_in?
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
def user_signed_in?
!!current_user
end
RUBY
end
generate :controller, "Sessions", "--skip-routes", "--skip-helper"
insert_into_file "app/controllers/sessions_controller.rb", after: "SessionsController < ApplicationController\n" do <<~RUBY.indent(2)
def create
user = User.from_omniauth(request.env["omniauth.auth"])
if user.save
session[:user_id] = user.id
redirect_to root_path, notice: "Signed in successfully"
else
redirect_to root_path, alert: "Failed to sign in"
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, notice: "Signed out successfully"
end
RUBY
end
generate :controller, "Home"
insert_into_file "app/controllers/home_controller.rb", after: "class HomeController < ApplicationController\n" do <<~RUBY.indent(2)
def index
end
RUBY
end
create_file "app/views/home/index.html.erb", <<~HTML.indent(4)
<h1>Welcome to the home page</h1>
HTML
route <<~RUBY.indent(2)
get "auth/:provider/callback", to: "sessions#create"
get "auth/failure", to: redirect("/")
post "signout", to: "sessions#destroy", as: "signout"
root "home#index"
RUBY
initializer "omniauth.rb", <<~RUBY
Rails.application.config.middleware.use OmniAuth::Builder do
if Rails.env.production?
provider :google_oauth2, Rails.application.credentials.google.oauth2.client_id!, Rails.application.credentials.google.oauth2.client_secret!
else
# Change this to your Google OAuth client ID and secret as above or using environment variables
provider :developer
end
end
OmniAuth.config.allowed_request_methods = [ :post ]
OmniAuth.config.full_host = Rails.env.production? ? "https://example.com" : "http://127.0.0.1:3000"
RUBY
insert_into_file "app/views/layouts/application.html.erb", after: "<body>\n" do <<~HTML.indent(4)
<header>
<% if user_signed_in? %>
<%= button_to "Sign out", signout_path, method: :post, data: { turbo: false } %>
<% else %>
<% if Rails.env.development? %>
<%= button_to "Sign in", '/auth/developer', method: :post, data: { turbo: false } %>
<% else %>
<%= button_to "Sign in", '/auth/google_oauth2', method: :post, data: { turbo: false } %>
<% end %>
<% end %>
</header>
HTML
end
rails_command "db:drop db:create db:migrate"
run "bundle exec rubocop -a"
git add: "."
git commit: "-m 'feat: scaffold application'"
end