railsthemes 1.2.0 → 2.0.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +15 -0
  2. data/.rvmrc +2 -4
  3. data/Gemfile +4 -6
  4. data/Rakefile +8 -5
  5. data/bin/railsthemes +20 -24
  6. data/features/railsthemes.feature +11 -0
  7. data/features/support/env.rb +1 -0
  8. data/lib/railsthemes/email_installer.rb +16 -77
  9. data/lib/railsthemes/ensurer.rb +3 -3
  10. data/lib/railsthemes/installer.rb +118 -142
  11. data/lib/railsthemes/switcher.rb +33 -0
  12. data/lib/railsthemes/tar.rb +4 -5
  13. data/lib/railsthemes/theme_installer.rb +144 -102
  14. data/lib/railsthemes/utils.rb +148 -107
  15. data/lib/railsthemes/version.rb +1 -1
  16. data/lib/railsthemes.rb +2 -9
  17. data/spec/fixtures/blank-assets/{email/app/assets/stylesheets/email.css → tier1-erb-scss/controllers/controller1.rb} +0 -0
  18. data/spec/fixtures/blank-assets/{erb-css/base/app/assets/images/bg/sprite.png → tier1-erb-scss/helpers/helper1.rb} +0 -0
  19. data/spec/fixtures/blank-assets/{erb-css/base/app/assets/images/image1.png → tier1-erb-scss/images/image1.jpg} +0 -0
  20. data/spec/fixtures/blank-assets/{erb-css/base/app/assets/javascripts/jquery.dataTables.js → tier1-erb-scss/javascripts/file1.js} +0 -0
  21. data/spec/fixtures/blank-assets/{erb-css/base/app/assets/javascripts/scripts.js.erb → tier1-erb-scss/layouts/layout1.html.haml} +0 -0
  22. data/spec/fixtures/blank-assets/{erb-css/base/app/views/layouts/_interior_sidebar.html.erb → tier1-erb-scss/mailers/mailer.rb} +0 -0
  23. data/spec/fixtures/blank-assets/{erb-css/base/app/views/layouts/application.html.erb → tier1-erb-scss/stylesheets/stylesheet1.css.scss} +0 -0
  24. data/spec/fixtures/blank-assets-archived/tier1-erb-scss.tar.gz +0 -0
  25. data/spec/lib/railsthemes/email_installer_spec.rb +10 -20
  26. data/spec/lib/railsthemes/ensurer_spec.rb +9 -9
  27. data/spec/lib/railsthemes/installer_spec.rb +90 -75
  28. data/spec/lib/railsthemes/switcher_spec.rb +82 -0
  29. data/spec/lib/railsthemes/theme_installer_spec.rb +323 -109
  30. data/spec/lib/railsthemes/utils_spec.rb +43 -3
  31. data/spec/spec_helper.rb +49 -0
  32. metadata +95 -122
  33. data/lib/railsthemes/asset_installer.rb +0 -48
  34. data/spec/fixtures/blank-assets/erb-css/base/app/assets/stylesheets/style.css.erb +0 -1
  35. data/spec/fixtures/blank-assets/erb-css/base/app/views/layouts/homepage.html.erb +0 -0
  36. data/spec/fixtures/blank-assets/erb-css/gems/formtastic/app/assets/stylesheets/formtastic.css.scss +0 -0
  37. data/spec/fixtures/blank-assets/erb-css/gems/simple_form/app/assets/stylesheets/simple_form.css.scss +0 -0
  38. data/spec/fixtures/blank-assets-archived/design-assets.tar.gz +0 -0
  39. data/spec/fixtures/blank-assets-archived/email.tar.gz +0 -0
  40. data/spec/fixtures/blank-assets-archived/erb-css.tar.gz +0 -0
  41. data/spec/lib/railsthemes/asset_installer_spec.rb +0 -10
@@ -13,9 +13,9 @@ describe Railsthemes::ThemeInstaller do
13
13
  end
14
14
 
15
15
  describe :install_from_archive do
16
- # does not work on Windows, NotImplementedError in tar module
16
+ # this spec does not work on Windows, NotImplementedError in tar module
17
17
  it 'should extract and then install from that extracted directory' do
18
- filename = 'spec/fixtures/blank-assets-archived/erb-css.tar.gz'
18
+ filename = 'spec/fixtures/blank-assets-archived/tier1-erb-scss.tar.gz'
19
19
  FakeFS::FileSystem.clone(filename)
20
20
  mock(@installer).install_from_file_system @tempdir
21
21
  @installer.install_from_archive filename
@@ -24,75 +24,116 @@ describe Railsthemes::ThemeInstaller do
24
24
 
25
25
  describe :install_from_file_system do
26
26
  context 'when the filepath is a directory' do
27
- it 'should copy the files from that directory into the Rails app' do
28
- FileUtils.mkdir_p('filepath/base')
29
- FileUtils.touch('filepath/base/a')
30
- FileUtils.touch('filepath/base/b')
31
- mock(@installer).post_copying_changes
27
+ context 'copying files' do
28
+ it 'should copy controllers (and subdirectories, generally)' do
29
+ create_file 'theme/controllers/controller1.rb'
30
+ create_file 'theme/controllers/railsthemes_themename/controller2.rb'
32
31
 
33
- @installer.install_from_file_system('filepath')
34
- File.should exist('a')
35
- File.should exist('b')
32
+ @installer.install_from_file_system('theme')
33
+
34
+ filesystem_should_match [
35
+ 'app/controllers/controller1.rb',
36
+ 'app/controllers/railsthemes_themename/controller2.rb',
37
+ ]
38
+ end
39
+
40
+ it 'should copy helpers' do
41
+ create_file 'theme/helpers/helper1.rb'
42
+ @installer.install_from_file_system('theme')
43
+ filesystem_should_match ['app/helpers/helper1.rb']
44
+ end
45
+
46
+ it 'should copy layouts' do
47
+ create_file 'theme/layouts/layout1.html.haml'
48
+ @installer.install_from_file_system('theme')
49
+ filesystem_should_match ['app/views/layouts/layout1.html.haml']
50
+ end
51
+
52
+ it 'should copy stylesheets' do
53
+ create_file 'theme/stylesheets/stylesheet1.css.scss'
54
+ @installer.install_from_file_system('theme')
55
+ filesystem_should_match ['app/assets/stylesheets/stylesheet1.css.scss']
56
+ end
57
+
58
+ it 'should copy javascripts' do
59
+ create_file 'theme/javascripts/file1.js'
60
+ @installer.install_from_file_system('theme')
61
+ filesystem_should_match ['app/assets/javascripts/file1.js']
62
+ end
63
+
64
+ it 'should copy docs' do
65
+ create_file 'theme/doc/some_doc.md'
66
+ @installer.install_from_file_system('theme')
67
+ filesystem_should_match ['doc/some_doc.md']
68
+ end
69
+
70
+ it 'should copy images' do
71
+ create_file 'theme/images/image1.jpg'
72
+ @installer.install_from_file_system('theme')
73
+ filesystem_should_match ['app/assets/images/image1.jpg']
74
+ end
75
+
76
+ it 'should copy mailers' do
77
+ create_file 'theme/mailers/mailer.rb'
78
+ @installer.install_from_file_system('theme')
79
+ filesystem_should_match ['app/mailers/mailer.rb']
80
+ end
81
+
82
+ it 'should copy views' do
83
+ create_file 'theme/views/railsthemes_themename/view1.html.erb'
84
+ @installer.install_from_file_system('theme')
85
+ filesystem_should_match ['app/views/railsthemes_themename/view1.html.erb']
86
+ end
87
+
88
+ it 'should copy fonts' do
89
+ create_file 'theme/fonts/railsthemes_themename/myfont.ttf'
90
+ @installer.install_from_file_system('theme')
91
+ filesystem_should_match ['app/assets/fonts/railsthemes_themename/myfont.ttf']
92
+ end
36
93
  end
37
94
 
38
95
  it 'should handle directories that have spaces' do
39
- FileUtils.mkdir_p('file path/base')
40
- FileUtils.touch('file path/base/a')
41
- FileUtils.touch('file path/base/b')
42
- mock(@installer).post_copying_changes
43
-
44
- @installer.install_from_file_system('file path')
45
- File.should exist('a')
46
- File.should exist('b')
96
+ create_file 'theme/images/image with spaces.png'
97
+ @installer.install_from_file_system('theme')
98
+ filesystem_should_match ['app/assets/images/image with spaces.png']
47
99
  end
48
100
 
49
101
  it 'should handle windows style paths' do
50
- FileUtils.mkdir_p('fp1/fp2/base')
51
- FileUtils.touch('fp1/fp2/base/a')
52
- FileUtils.touch('fp1/fp2/base/b')
53
- FileUtils.mkdir_p('fp1/fp2/gems')
54
- mock(@installer).post_copying_changes
55
-
56
- @installer.install_from_file_system('fp1\fp2')
57
- File.should exist('a')
58
- File.should exist('b')
102
+ create_file 'subdir/theme/images/image.png'
103
+ @installer.install_from_file_system('subdir\theme')
104
+ filesystem_should_match ['app/assets/images/image.png']
59
105
  end
60
106
 
61
107
  it 'should not copy system files' do
62
- FileUtils.mkdir_p('filepath/base')
63
- FileUtils.touch('filepath/base/.DS_Store')
64
- mock(@installer).post_copying_changes
108
+ create_file 'theme/controllers/.DS_Store'
109
+ @installer.install_from_file_system('theme')
110
+ File.should_not exist('app/controllers/.DS_Store')
111
+ end
65
112
 
66
- @installer.install_from_file_system('filepath')
67
- File.should_not exist('.DS_Store')
113
+ it 'should do the post copying changes needed' do
114
+ create_file 'theme/theme_name', :content => 'themename'
115
+ mock(@installer).post_copying_changes('themename')
116
+ @installer.install_from_file_system('theme')
68
117
  end
69
118
  end
70
119
 
71
120
  describe 'override file behavior' do
72
121
  before do
73
- FileUtils.mkdir_p('filepath/gems')
74
- FileUtils.mkdir_p('filepath/base/app/assets/stylesheets')
75
- FileUtils.mkdir_p("app/assets/stylesheets")
122
+ create_file 'theme/stylesheets/overrides.css.scss', :content => 'the override'
76
123
  @filename = 'app/assets/stylesheets/overrides.css.scss'
77
- FileUtils.touch("filepath/base/#{@filename}")
78
- mock(@installer).post_copying_changes
79
- stub(@installer).popup_documentation
80
124
  end
81
125
 
82
126
  it 'should not overwrite override files when they already exist' do
83
- File.open(@filename, 'w') do |f|
84
- f.write "existing override"
85
- end
86
-
87
- @installer.install_from_file_system('filepath')
127
+ create_file @filename, :content => 'do not replace'
128
+ @installer.install_from_file_system('theme')
88
129
  File.should exist(@filename)
89
- File.read(@filename).should =~ /existing override/
130
+ File.read(@filename).should =~ /do not replace/
90
131
  end
91
132
 
92
133
  it 'should create override files when they do not already exist' do
93
- @installer.install_from_file_system('filepath')
134
+ @installer.install_from_file_system('theme')
94
135
  File.should exist(@filename)
95
- File.read(@filename).should == ''
136
+ File.read(@filename).should == 'the override'
96
137
  end
97
138
  end
98
139
 
@@ -113,93 +154,266 @@ describe Railsthemes::ThemeInstaller do
113
154
  end
114
155
  end
115
156
 
116
- describe '#create_railsthemes_demo_pages' do
157
+ # this should arguably be an integration test, but I'm not sure how
158
+ # fakefs + running arbitrary binaries will work out
159
+ describe 'end to end behavior' do
117
160
  before do
118
- FileUtils.mkdir('config')
119
- File.open(File.join('config', 'routes.rb'), 'w') do |f|
120
- f.write <<-EOS
161
+ stub(@installer).post_copying_changes
162
+ FakeFS::FileSystem.clone('spec/fixtures')
163
+ end
164
+
165
+ def verify_end_to_end_operation
166
+ [
167
+ 'app/controllers/controller1.rb',
168
+ 'doc/some_doc.md',
169
+ 'app/helpers/helper1.rb',
170
+ 'app/assets/images/image1.jpg',
171
+ 'app/assets/javascripts/file1.js',
172
+ 'app/views/layouts/layout1.html.haml',
173
+ 'app/mailers/mailer.rb',
174
+ 'app/assets/stylesheets/stylesheet1.css.scss',
175
+ ].each do |filename|
176
+ File.should exist(filename), "#{filename} was expected but not present"
177
+ end
178
+ end
179
+
180
+ it 'should extract correctly from directory' do
181
+ filename = 'spec/fixtures/blank-assets/tier1-erb-scss'
182
+ @installer.install_from_file_system filename
183
+ verify_end_to_end_operation
184
+ end
185
+
186
+ # this spec does not work on Windows, NotImplementedError in tar module
187
+ it 'should extract correctly from archive' do
188
+ filename = 'spec/fixtures/blank-assets-archived/tier1-erb-scss'
189
+ @installer.install_from_file_system filename
190
+ verify_end_to_end_operation
191
+ end
192
+ end
193
+
194
+ describe '#post_copying_changes' do
195
+ it 'should call the right submethods' do
196
+ mock(@installer).remove_unwanted_public_files
197
+ mock(@installer).create_railsthemes_demo_routes
198
+ mock(@installer).add_needed_gems
199
+ mock(Railsthemes::Utils).set_layout_in_application_controller 'theme_name'
200
+ mock(@installer).add_to_asset_precompilation_list 'theme_name'
201
+ mock(@installer).comment_out_formtastic_if_user_does_not_use_formtastic 'theme_name'
202
+ @installer.post_copying_changes 'theme_name'
203
+ end
204
+ end
205
+
206
+ describe '#remove_unwanted_public_files' do
207
+ it 'should remove files we do not want hanging around' do
208
+ files = [
209
+ 'public/index.html',
210
+ 'public/404.html',
211
+ 'public/422.html',
212
+ 'public/500.html',
213
+ ]
214
+ files.each do |filename|
215
+ create_file filename
216
+ end
217
+
218
+ @installer.remove_unwanted_public_files
219
+
220
+ files.each do |filename|
221
+ File.should_not exist(filename), "#{filename} was expected to be gone, but it is still here"
222
+ end
223
+ end
224
+ end
225
+
226
+ describe '#add_needed_gems' do
227
+ describe 'general gems' do
228
+ context 'are not present' do
229
+ it 'should require them' do
230
+ create_file 'Gemfile'
231
+ @installer.add_needed_gems
232
+ lines = File.read('Gemfile').split("\n")
233
+ lines.grep(/^gem 'sass'/).count.should == 1
234
+ lines.grep(/^gem 'jquery-rails'/).count.should == 1
235
+ lines.grep(/^gem 'jquery-ui-rails'/).count.should == 1
236
+ end
237
+ end
238
+
239
+ context 'are present' do
240
+ it 'should not readd them' do
241
+ write_gemfiles_using_gems 'sass', 'jquery-rails', 'jquery-ui-rails'
242
+ @installer.add_needed_gems
243
+ lines = File.read('Gemfile').split("\n")
244
+ lines.grep(/^gem 'sass'/).count.should == 1
245
+ lines.grep(/^gem 'jquery-rails'/).count.should == 1
246
+ lines.grep(/^gem 'jquery-ui-rails'/).count.should == 1
247
+ end
248
+ end
249
+ end
250
+
251
+ describe 'asset gems' do
252
+ context 'gems are not present' do
253
+ it 'should add them' do
254
+ create_file 'Gemfile'
255
+ @installer.add_needed_gems
256
+ lines = File.read('Gemfile').split("\n")
257
+ lines.grep(/gem 'compass-rails'/).count.should == 1
258
+ lines.grep(/gem 'zurb-foundation'/).count.should == 1
259
+ end
260
+ end
261
+
262
+ context 'gems are present' do
263
+ it 'should not add them' do
264
+ write_gemfiles_using_gems :assets => ['compass-rails', 'zurb-foundation']
265
+ @installer.add_needed_gems
266
+ lines = File.read('Gemfile').split("\n")
267
+ lines.grep(/gem 'compass-rails'/).count.should == 1
268
+ lines.grep(/gem 'zurb-foundation'/).count.should == 1
269
+ end
270
+ end
271
+
272
+ context 'only one is present' do
273
+ it 'should add compass-rails if not present' do
274
+ write_gemfiles_using_gems :assets => ['zurb-foundation']
275
+ @installer.add_needed_gems
276
+ lines = File.read('Gemfile').split("\n")
277
+ lines.grep(/gem 'compass-rails'/).count.should == 1
278
+ lines.grep(/gem 'zurb-foundation'/).count.should == 1
279
+ end
280
+
281
+ it 'should add zurb-foundation if not present' do
282
+ write_gemfiles_using_gems :assets => ['compass-rails']
283
+ @installer.add_needed_gems
284
+ lines = File.read('Gemfile').split("\n")
285
+ lines.grep(/gem 'compass-rails'/).count.should == 1
286
+ lines.grep(/gem 'zurb-foundation'/).count.should == 1
287
+ end
288
+ end
289
+
290
+ it 'should specify the right version of zurb-foundation' do
291
+ write_gemfiles_using_gems :assets => ['compass-rails']
292
+ @installer.add_needed_gems
293
+ lines = File.read('Gemfile').split("\n")
294
+ matches = lines.grep(/gem 'zurb-foundation'/)
295
+ matches.first.should =~ /'~> 4\.0'/
296
+ end
297
+ end
298
+ end
299
+
300
+ describe '#create_railsthemes_demo_routes' do
301
+ before do
302
+ contents = <<-EOS
121
303
  RailsApp::Application.routes.draw do
122
304
  # This is a legacy wild controller route that's not recommended for RESTful applications.
123
305
  # Note: This route will make all actions in every controller accessible via GET requests.
124
306
  # match ':controller(/:action(/:id(.:format)))'
307
+ # root :to => 'home/index'
125
308
  end
126
- EOS
127
- end
309
+ EOS
310
+ create_file 'config/routes.rb', :content => contents
311
+ end
312
+
313
+ it 'should add routing if it has not been generated yet' do
314
+ @installer.create_railsthemes_demo_routes
315
+ File.read('config/routes.rb').split("\n").grep(
316
+ /get 'railsthemes', controller: :railsthemes, action: :index/
317
+ ).count.should == 1
128
318
  end
129
319
 
130
- it 'should create a RailsThemes controller' do
131
- @installer.create_railsthemes_demo_pages
132
- controller = File.join('app', 'controllers', 'railsthemes_controller.rb')
133
- lines = File.read(controller).split("\n")
134
- lines.count.should == 11
135
- lines.first.should match /class RailsthemesController < ApplicationController/
320
+ it 'should not readd routing' do
321
+ @installer.create_railsthemes_demo_routes
322
+ @installer.create_railsthemes_demo_routes
323
+ File.read('config/routes.rb').split("\n").grep(
324
+ /get 'railsthemes', controller: :railsthemes, action: :index/
325
+ ).count.should == 1
136
326
  end
137
327
 
138
- it 'should insert lines into the routes file' do
139
- @installer.create_railsthemes_demo_pages
140
- routes_file = File.join('config', 'routes.rb')
141
- lines = File.read(routes_file).split("\n")
142
- lines.grep(/match 'railsthemes\/landing' => 'railsthemes#landing'/).count.should == 1
143
- lines.grep(/match 'railsthemes\/inner' => 'railsthemes#inner'/).count.should == 1
144
- lines.grep(/match 'railsthemes\/jquery_ui' => 'railsthemes#jquery_ui'/).count.should == 1
328
+ context 'when no root route exists' do
329
+ it 'should add root route' do
330
+ @installer.create_railsthemes_demo_routes
331
+ File.read('config/routes.rb').split("\n").grep(
332
+ ' root :to => "railsthemes#index"'
333
+ ).count.should == 1
334
+ end
145
335
  end
146
336
 
147
- it 'should not insert lines into the routes file when run more than once' do
148
- @installer.create_railsthemes_demo_pages
149
- @installer.create_railsthemes_demo_pages
150
- routes_file = File.join('config', 'routes.rb')
151
- lines = File.read(routes_file).split("\n")
152
- lines.grep(/match 'railsthemes\/landing' => 'railsthemes#landing'/).count.should == 1
153
- lines.grep(/match 'railsthemes\/inner' => 'railsthemes#inner'/).count.should == 1
154
- lines.grep(/match 'railsthemes\/jquery_ui' => 'railsthemes#jquery_ui'/).count.should == 1
337
+ context 'when root route exists' do
338
+ it 'should not add another root route' do
339
+ @installer.create_railsthemes_demo_routes
340
+ @installer.create_railsthemes_demo_routes
341
+ File.read('config/routes.rb').split("\n").grep(
342
+ ' root :to => "railsthemes#index"'
343
+ ).count.should == 1
344
+ end
155
345
  end
156
346
  end
157
347
 
158
- describe :install_gems_from do
159
- it 'should install the gems that we specify that match' do
160
- FakeFS::FileSystem.clone('spec/fixtures/blank-assets')
161
- # we only know about formtastic and simple_form in the gems directory
162
- @installer.install_gems_from("spec/fixtures/blank-assets/erb-css", ['formtastic', 'kaminari'])
163
- File.should exist('app/assets/stylesheets/formtastic.css.scss')
164
- File.should_not exist('app/assets/stylesheets/kaminari.css.scss')
165
- File.should_not exist('app/assets/stylesheets/simple_form.css.scss')
348
+ describe '#add_to_asset_precompilation_list' do
349
+ it 'should add it to the list if the line is not there yet' do
350
+ create_file 'config/environments/production.rb', :content => <<-EOS
351
+ BaseApp::Application.configure do
352
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
353
+ # config.assets.precompile += %w( search.js )
354
+ end
355
+ EOS
356
+ @installer.add_to_asset_precompilation_list 'magenta'
357
+ count = File.read('config/environments/production.rb').split("\n").grep(
358
+ /^\s*config.assets.precompile \+= %w\( railsthemes_magenta\.js railsthemes_magenta\.css \)$/).count
359
+ count.should == 1
166
360
  end
167
- end
168
361
 
169
- describe 'end to end operation' do
170
- def verify_end_to_end_operation
171
- ['app/assets/images/image1.png',
172
- 'app/assets/images/bg/sprite.png',
173
- 'app/assets/javascripts/jquery.dataTables.js',
174
- 'app/assets/javascripts/scripts.js.erb',
175
- 'app/assets/stylesheets/style.css.erb',
176
- 'app/views/layouts/_interior_sidebar.html.erb',
177
- 'app/views/layouts/application.html.erb',
178
- 'app/views/layouts/homepage.html.erb'].each do |filename|
179
- File.should exist(filename), "#{filename} was not present"
180
- end
181
- File.open('app/assets/stylesheets/style.css.erb').each do |line|
182
- line.should match /style.css.erb/
183
- end
362
+ it 'should not add it again if the line is there already' do
363
+ create_file 'config/environments/production.rb', :content => <<-EOS
364
+ BaseApp::Application.configure do
365
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
366
+ config.assets.precompile += %w( railsthemes_magenta.js railsthemes_magenta.css )
367
+ # config.assets.precompile += %w( search.js )
368
+ end
369
+ EOS
370
+ @installer.add_to_asset_precompilation_list 'magenta'
371
+ count = File.read('config/environments/production.rb').split("\n").grep(
372
+ /^\s*config.assets.precompile \+= %w\( railsthemes_magenta\.js railsthemes_magenta\.css \)$/).count
373
+ count.should == 1
374
+ end
375
+
376
+ it 'should add it to the list if there is a different theme already installed' do
377
+ create_file 'config/environments/production.rb', :content => <<-EOS
378
+ BaseApp::Application.configure do
379
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
380
+ config.assets.precompile += %w( railsthemes_orange.js railsthemes_orange.css )
381
+ # config.assets.precompile += %w( search.js )
382
+ end
383
+ EOS
384
+ @installer.add_to_asset_precompilation_list 'magenta'
385
+ count = File.read('config/environments/production.rb').split("\n").grep(
386
+ /^\s*config.assets.precompile \+= %w\( railsthemes_magenta\.js railsthemes_magenta\.css \)$/).count
387
+ count.should == 1
184
388
  end
389
+ end
185
390
 
391
+ describe '#comment_out_formtastic_if_user_does_not_use_formtastic' do
186
392
  before do
187
- stub(@installer).post_copying_changes
188
- FakeFS::FileSystem.clone('spec/fixtures')
189
- # should add some gems to the gemfile here and test gem installation
393
+ @filename = 'app/assets/stylesheets/railsthemes_themename.css'
394
+ create_file @filename, :content => <<-EOS
395
+ /*
396
+ *= require formtastic
397
+ */
398
+ EOS
190
399
  end
191
400
 
192
- it 'should extract correctly from directory' do
193
- filename = 'spec/fixtures/blank-assets/erb-css'
194
- @installer.install_from_file_system filename
195
- verify_end_to_end_operation
401
+ context 'user is using formtastic' do
402
+ before do
403
+ end
404
+
405
+ it 'should not comment out the line' do
406
+ write_gemfiles_using_gems 'formtastic'
407
+ @installer.comment_out_formtastic_if_user_does_not_use_formtastic 'themename'
408
+ File.read(@filename).split("\n").grep(/\*= require formtastic/).count.should == 1
409
+ end
196
410
  end
197
411
 
198
- # does not work on Windows, NotImplementedError in tar module
199
- it 'should extract correctly from archive' do
200
- filename = 'spec/fixtures/blank-assets-archived/erb-css'
201
- @installer.install_from_file_system filename
202
- verify_end_to_end_operation
412
+ context 'user is not using formtastic' do
413
+ it 'should comment out the line' do
414
+ @installer.comment_out_formtastic_if_user_does_not_use_formtastic 'themename'
415
+ File.read(@filename).split("\n").grep(/\* require formtastic/).count.should == 1
416
+ end
203
417
  end
204
418
  end
205
419
 
@@ -53,11 +53,11 @@ describe Railsthemes::Utils do
53
53
  describe 'add_gem_to_gemfile' do
54
54
  it 'should add the gem to the Gemfile' do
55
55
  Railsthemes::Utils.add_gem_to_gemfile 'test'
56
- Railsthemes::Utils.add_gem_to_gemfile 'test'
56
+ Railsthemes::Utils.add_gem_to_gemfile 'test2'
57
57
  lines = File.open('Gemfile').readlines.map(&:strip)
58
58
  lines.count.should == 2
59
- lines[0].should == "gem 'test'"
60
- lines[1].should == "gem 'test'"
59
+ lines[0].should =~ /^gem 'test'/
60
+ lines[1].should =~ /^gem 'test2'/
61
61
  end
62
62
  end
63
63
 
@@ -70,4 +70,44 @@ describe Railsthemes::Utils do
70
70
  Railsthemes::Utils.download(:url => 'http://example.com/something', :save_to => 'whatever')
71
71
  end
72
72
  end
73
+
74
+ describe '.set_layout_in_application_controller' do
75
+ before do
76
+ @base = <<-EOS
77
+ class ApplicationController < ActionController::Base
78
+ protect_from_forgery
79
+ EOS
80
+ end
81
+
82
+ it 'should add the layout line if it does not exist' do
83
+ create_file 'app/controllers/application_controller.rb', :content => "#{@base}\nend"
84
+ Railsthemes::Utils.set_layout_in_application_controller('magenta')
85
+ lines = File.read('app/controllers/application_controller.rb').split("\n")
86
+ lines.grep(/layout 'railsthemes_magenta'/).count.should == 1
87
+ end
88
+
89
+ it 'should modify the layout line if it exists but different' do
90
+ create_file 'app/controllers/application_controller.rb', :content => <<-EOS
91
+ #{@base}
92
+ layout 'railsthemes_orange'
93
+ end
94
+ EOS
95
+ Railsthemes::Utils.set_layout_in_application_controller('magenta')
96
+ lines = File.read('app/controllers/application_controller.rb').split("\n")
97
+ lines.grep(/layout 'railsthemes_orange'/).count.should == 0
98
+ lines.grep(/layout 'railsthemes_magenta'/).count.should == 1
99
+ end
100
+
101
+ it 'should not modify the layout line if it exists and same' do
102
+ create_file 'app/controllers/application_controller.rb', :content => <<-EOS
103
+ #{@base}
104
+ layout 'railsthemes_orange'
105
+ end
106
+ EOS
107
+ Railsthemes::Utils.set_layout_in_application_controller('orange')
108
+ lines = File.read('app/controllers/application_controller.rb').split("\n")
109
+ lines.grep(/layout 'railsthemes_orange'/).count.should == 1
110
+ end
111
+ end
112
+
73
113
  end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'bundler/setup'
3
3
  require 'logger'
4
4
  require 'fakefs/spec_helpers'
5
5
  require 'fakeweb'
6
+ require 'rr'
6
7
 
7
8
  LOGFILE_NAME = 'railsthemes.log'
8
9
 
@@ -27,6 +28,40 @@ end
27
28
 
28
29
  FakeWeb.allow_net_connect = false
29
30
 
31
+ def write_gemfiles_using_gems *gems
32
+ File.open('Gemfile', 'a') do |f|
33
+ f.puts "source :rubygems"
34
+ gems.each do |gem|
35
+ if gem.is_a? Hash
36
+ gem.each do |group, inner_gems|
37
+ f.puts "group :#{group.to_s} do"
38
+ inner_gems.each do |gemname|
39
+ f.puts " gem '#{gemname}'"
40
+ end
41
+ f.puts "end\n"
42
+ end
43
+ else
44
+ f.puts "gem '#{gem.to_s}'"
45
+ end
46
+ end
47
+ end
48
+
49
+ gem_names = *gems.map do |gem|
50
+ if gem.is_a? Hash
51
+ gems = []
52
+ gem.each do |group, inner_gems|
53
+ gems += inner_gems
54
+ end
55
+ gems
56
+ else
57
+ gem
58
+ end
59
+ end.flatten
60
+ File.open('Gemfile.lock', 'w') do |f|
61
+ f.write using_gems(*gem_names)
62
+ end
63
+ end
64
+
30
65
  def using_gems *gems
31
66
  "GEM\nremote: https://rubygems.org/\nspecs:\n" +
32
67
  gems.map{|gem| " #{gem}"}.join("\n") +
@@ -69,3 +104,17 @@ def with_installer_version version, &block
69
104
  Railsthemes.send(:remove_const, 'VERSION')
70
105
  Railsthemes.const_set('VERSION', old_version)
71
106
  end
107
+
108
+ def create_file filename, opts = {}
109
+ FileUtils.mkdir_p(File.dirname(filename))
110
+ FileUtils.touch(filename)
111
+ File.open(filename, 'w') { |f| f.write opts[:content] } if opts[:content]
112
+ end
113
+
114
+ def filesystem
115
+ Dir["**/*"]
116
+ end
117
+
118
+ def filesystem_should_match files_to_match
119
+ (filesystem & files_to_match).should =~ files_to_match
120
+ end