-
Notifications
You must be signed in to change notification settings - Fork 0
/
full.rb
executable file
·198 lines (164 loc) · 8.19 KB
/
full.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
#!/usr/bin/env ruby
# coding: utf-8
# clean file
run "rm README.rdoc"
run "rm -f public/index.html"
run "rm -f public/images/rails.png"
run "cp config/database.yml config/database.yml.example"
# add to Gemfile
append_file 'Gemfile', <<-CODE
gem "devise"
gem "kaminari"
gem "therubyracer"
gem "less-rails"
gem "twitter-bootstrap-rails"
gem "ransack"
CODE
# bundle install
run 'bundle install'
run 'rails g devise:install'
run 'rails g devise Admin'
run 'rails g devise:views'
run 'rails g bootstrap:install'
run 'rails g bootstrap:layout application fixed -f'
run 'rails g kaminari:config'
run 'rake db:migrate'
# add default admin account
append_file 'db/seeds.rb', <<-CODE
admins = Admin.create([{email: '[email protected]', password: '[email protected]', password_confirmation: '[email protected]'}])
CODE
run 'rake db:seed'
# default controller
run 'rails g controller welcome index'
# default application controller
run 'cd app/controllers; wget -N https://raw.github.com/pct/rails3-template/master/replace/application_controller.rb'
file_name = 'app/controllers/application_controller.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/before_filter :authenticate_admin!/, "\b")
File.open(file_name, 'w') {|file| file.puts ret}
# default backend controllers
run 'rails g controller backend/application'
run 'rails g controller backend/welcome index'
# add auth to backend app controller
run 'cd app/controllers/backend; wget -N https://raw.github.com/pct/rails3-template/master/replace/application_controller.rb'
file_name = 'app/controllers/backend/application_controller.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/class ApplicationController/, "class Backend::ApplicationController")
ret = ret.gsub(/"application"/, '"backend"')
File.open(file_name, 'w') {|file| file.puts ret}
# fix default backend controller
file_name = 'app/controllers/backend/welcome_controller.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/ApplicationController/, "Backend::ApplicationController")
ret = ret.gsub(/"application"/, '"backend"')
File.open(file_name, 'w') {|file| file.puts ret}
# use different layout for devise
run 'cd app/views/layouts/; wget -N https://raw.github.com/pct/rails3-template/master/replace/devise_layout.html.erb'
run 'cd app/views/layouts/; wget -O backend.html.erb https://raw.github.com/pct/rails3-template/master/replace/application.html.erb'
# 改 route.rb 啟用 welcome/index
file_name = 'config/routes.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/# root :to => 'welcome#index'/, "root :to => 'welcome#index'")
File.open(file_name, 'w') {|file| file.puts ret}
# 改 route.rb 啟用 backend welcome#index
file_name = 'config/routes.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/get "welcome\/index"/, "namespace :backend do root to: 'welcome#index' end\n get 'welcome/index'")
File.open(file_name, 'w') {|file| file.puts ret}
# kaminari per page 10
file_name = 'config/initializers/kaminari_config.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/# config.default_per_page = 25/, "config.default_per_page = 10")
File.open(file_name, 'w') {|file| file.puts ret}
# devise use :get to sign out
file_name = 'config/initializers/devise.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/config.sign_out_via = :delete/, "config.sign_out_via = :get")
File.open(file_name, 'w') {|file| file.puts ret}
# cancel devise admin registration
file_name = 'app/models/admin.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/devise :database_authenticatable, :registerable,/, "devise :database_authenticatable, #:registerable,")
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'config/routes.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/devise_for :admins/, "devise_for :admins, :skip => [:registration]")
File.open(file_name, 'w') {|file| file.puts ret}
run ':> app/views/welcome/index.html.erb'
append_file 'app/views/welcome/index.html.erb', <<-CODE
<div class="hero-unit">
<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
</div>
CODE
# devise layout
file_name = 'app/views/devise/sessions/new.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Sign in"/, 'f.submit "Sign in", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'app/views/devise/passwords/new.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Send me reset password instructions"/, 'f.submit "Send me reset password instructions", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'app/views/devise/passwords/edit.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Change my password"/, 'f.submit "Change my password", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'app/views/devise/confirmations/new.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Resend confirmation instructions"/, 'f.submit "Resend confirmation instructions", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'app/views/devise/registrations/edit.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Update"/, 'f.submit "Update", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'app/views/devise/registrations/new.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Sign up"/, 'f.submit "Sign up", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
file_name = 'app/views/devise/unlocks/new.html.erb'
tmp = File.read(file_name)
ret = tmp.gsub(/f.submit "Resend unlock instructions"/, 'f.submit "Resend unlock instructions", :class => "btn"')
File.open(file_name, 'w') {|file| file.puts ret}
# scaffold without scaffold.css
file_name = 'config/application.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/config.assets.version = '1.0'/, "config.assets.version = '1.0'\n config.generators do |g|\n g.stylesheets false\n end")
File.open(file_name, 'w') {|file| file.puts ret}
# apply css
append_file 'app/assets/stylesheets/application.css', <<-CODE
input, textarea { width: auto; }
body { padding-top: 60px; }
CODE
# fetch scaffold template
run 'mkdir -p lib/templates/erb/scaffold'
run 'cd lib/templates/erb/scaffold; wget -N https://raw.github.com/pct/rails3-template/master/lib/templates/erb/scaffold/_form.html.erb'
run 'cd lib/templates/erb/scaffold; wget -N https://raw.github.com/pct/rails3-template/master/lib/templates/erb/scaffold/edit.html.erb'
run 'cd lib/templates/erb/scaffold; wget -N https://raw.github.com/pct/rails3-template/master/lib/templates/erb/scaffold/new.html.erb'
run 'cd lib/templates/erb/scaffold; wget -N https://raw.github.com/pct/rails3-template/master/lib/templates/erb/scaffold/index.html.erb'
run 'cd lib/templates/erb/scaffold; wget -N https://raw.github.com/pct/rails3-template/master/lib/templates/erb/scaffold/show.html.erb'
# fetch and modify scaffold controller with kaminari
run 'mkdir -p lib/templates/rails/scaffold_controller'
run 'cd lib/templates/rails/scaffold_controller; wget -N https://raw.github.com/pct/rails3-template/master/lib/templates/rails/scaffold_controller/controller.rb'
file_name = 'lib/templates/rails/scaffold_controller/controller.rb'
tmp = File.read(file_name)
ret = tmp.gsub(/Controller < ApplicationController/, "Controller < Backend::ApplicationController")
File.open(file_name, 'w') {|file| file.puts ret}
# fetch kaminari views
run 'mkdir -p app/views/kaminari'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_first_page.html.erb'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_gap.html.erb'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_last_page.html.erb'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_next_page.html.erb'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_page.html.erb'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_paginator.html.erb'
run 'cd app/views/kaminari; wget -N https://raw.github.com/pct/rails3-template/master/replace/kaminari/_prev_page.html.erb'
# git ignore
append_file '.gitignore', <<-CODE
*~
*.swp
CODE
# git init
git :init
git :add => '.'
git :commit => "-a -m 'init'"