-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymorphic_blog_post.rb
328 lines (272 loc) · 8.96 KB
/
polymorphic_blog_post.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
def source_paths
Array(super)
[File.expand_path(File.dirname(__FILE__))]
end
#Here we are removing the Gemfile and starting over
#You may want to tap and existing gemset or go this method to make it easier for
#others to check it out.
#Plus, you dont have to remove the comments in the Gemfile
remove_file "Gemfile"
run "touch Gemfile"
#be sure to add source at the top of the file
add_source 'https://rubygems.org'
gem 'rails'
gem 'sqlite3'
gem 'uglifier'
gem 'coffee-rails'
gem 'jquery-rails'
gem 'turbolinks'
gem 'devise'
gem 'bootstrap-sass'
gem 'sass-rails'
gem 'font-awesome-sass'
gem 'spring'
gem 'sdoc', group: :doc
gem_group :development, :test do
gem 'quiet_assets'
gem 'byebug'
gem 'awesome_print'
gem 'better_errors'
gem 'binding_of_caller'
end
generate 'devise:install'
generate 'devise User'
generate 'devise:views'
rake 'db:create'
rake 'db:migrate'
#Add bootstrap to App
create_file 'app/assets/stylesheets/customizations.css.scss' do <<-TEXT
@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome-sprockets";
@import "font-awesome";
.center {
text-align: center;
}
/*for Devise Rememver me box*/
#user_remember_me {
margin-left: 0px;
}
/*flash*/
.alert-error {
background-color: #f2dede;
border-color: #eed3d7;
color: #b94a48;
text-align: left;
}
.alert-alert {
background-color: #f2dede;
border-color: #eed3d7;
color: #b94a48;
text-align: left;
}
.alert-success {
background-color: #dff0d8;
border-color: #d6e9c6;
color: #468847;
text-align: left;
}
.alert-notice {
background-color: #dff0d8;
border-color: #d6e9c6;
color: #468847;
text-align: left;
}
TEXT
end
insert_into_file('app/assets/javascripts/application.js', "//= require bootstrap-sprockets\n",
:before => /\/\/= require_tree ./)
#Creating a home page
generate(:controller, "pages home")
remove_file 'app/views/pages/home.html.erb'
create_file 'app/views/pages/home.html.erb' do <<-TEXT
<div class="jumbotron center">
<h1>Polymorphic Users with Devise</h1>
<p><a href="http://codebycodes.com/blog/2016/05/30/creating-polymorphic-users-with-devise-and-rails/">Back To Post</a></p>
<h4><%= link_to 'Sign Up', new_user_registration_path %></h4>
</div>
TEXT
end
#Add a nav header for bootstrap
create_file 'app/views/layouts/_header.html.erb' do <<-TEXT
<nav class="navbar navbar-static-top navbar-default" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%= link_to "APP NAME", root_path, class: "navbar-brand" %>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<% if user_signed_in? %>
<li><%= link_to current_user.email, edit_user_registration_path %>
<li><%= link_to "Log out", destroy_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to "Sign in", user_session_path %></li>
<% end %>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container -->
</nav>
TEXT
end
# Add meta tags and html5 shim
insert_into_file 'app/views/layouts/application.html.erb', :after => /<%= csrf_meta_tags %>/ do <<-TEXT
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src='https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js'></script>
<script src='https://oss.maxcdn.com/respond/1.4.2/respond.min.js'></script>
<![endif]-->
TEXT
end
create_file 'app/views/layouts/_messages.html.erb' do <<-HTML
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<a href="#" data-dismiss="alert" class="close">×</a>
<ul>
<li>
<%= value %>
</li>
</ul>
</div>
<% end %>
HTML
end
remove_file 'app/views/devise/registrations/edit.html.erb'
remove_file 'app/views/devise/registrations/new.html.erb'
create_file 'app/views/devise/registrations/edit.html.erb' do <<-TEXT
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h1>Edit <%= resource_name.to_s.humanize %></h1>
</div>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: "form-control", :autofocus => true %>
</div>
<div class="form-group">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
<%= f.password_field :password, class: "form-control", :autocomplete => "off" %>
</div>
<div class="form-group">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i>
<%= f.password_field :current_password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Update", class: "btn btn-primary" %>
</div>
<% end %>
</div>
<div class="panel-footer">
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete, class: "btn btn-warning" %></p>
<%= link_to "Back", :back %>
</div>
</div>
TEXT
end
create_file 'app/views/devise/registrations/new.html.erb' do <<-TEXT
<div class="panel panel-default">
<div class="panel-heading">
<h1>Sign up</h1>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
</div>
<div class="panel-footer">
<%= render "devise/shared/links" %>
</div>
</div>
TEXT
end
remove_file 'app/views/devise/sessions/new.html.erb'
create_file 'app/views/devise/sessions/new.html.erb' do <<-TEXT
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h1>Sign in</h1>
</div>
</div>
<div class="panel-body">
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: "form-control", :autofocus => true %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<div class="checkbox">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
</div>
<div class="form-group">
<%= f.submit "Sign in", class: "btn btn-primary" %>
</div>
<% end %>
</div>
<div class="panel-footer">
<%= render "devise/shared/links" %>
</div>
</div>
TEXT
end
insert_into_file 'app/views/layouts/application.html.erb',
"\n<%= render 'layouts/header' %>\n <%= render 'layouts/messages'%>", :after => /<body>/
route "root 'pages#home'"
create_file 'app/helpers/devise_helper.rb' do <<-TEXT
module DeviseHelper
def devise_error_messages!
return '' if resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
html = <<-HTML
<div class="alert alert-error alert-danger"> <button type="button"
class="close" data-dismiss="alert">x</button>
{messages}
</div>
HTML
html.html_safe
end
end
TEXT
end
#Janky "temp" solution
insert_into_file('app/assets/javascripts/application.js', "//= require bootstrap-sprockets\n",
:before => /\/\/= require_tree ./)
insert_into_file('app/helpers/devise_helper.rb', '#', :before => /{messages}/)
generate 'simple_form:install --bootstrap'
git :init
git add: '.'
git commit: "-a -m 'Initial commit'"