-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.rb
executable file
·163 lines (135 loc) · 6.65 KB
/
backend.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
#!/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'
# add auth to default app controller
run 'cd app/controllers/; wget -N https://raw.github.com/pct/rails3-template/master/replace/application_controller.rb'
# 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 -N 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}
# 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 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'
# 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'"