Skip to content
This repository
Browse code

Usage file in generators shouldn't be fetched only based on source_root

In case `source_roout` is not set, `default_source_root` is used,
which includes also `templates` directory. If there is no `templates`
directory, `default_source_root` is not available and USAGE will not
be displayed. USAGE should be also checked based on default
directory excluding `templates`.
  • Loading branch information...
commit fff3e75e1cf777f4b444d08c153065ab4264fa2e 1 parent fa1645a
Piotr Sarnacki authored April 01, 2012
21  railties/lib/rails/generators/base.rb
@@ -31,10 +31,9 @@ def self.source_root(path=nil)
31 31
       # root otherwise uses a default description.
32 32
       def self.desc(description=nil)
33 33
         return super if description
34  
-        usage = source_root && File.expand_path("../USAGE", source_root)
35 34
 
36  
-        @desc ||= if usage && File.exist?(usage)
37  
-          ERB.new(File.read(usage)).result(binding)
  35
+        @desc ||= if usage_path
  36
+          ERB.new(File.read(usage_path)).result(binding)
38 37
         else
39 38
           "Description:\n    Create #{base_name.humanize.downcase} files for #{generator_name} generator."
40 39
         end
@@ -213,7 +212,8 @@ def self.class_option(name, options={}) #:nodoc:
213 212
       # root, you should use source_root.
214 213
       def self.default_source_root
215 214
         return unless base_name && generator_name
216  
-        path = File.expand_path(File.join(base_name, generator_name, 'templates'), base_root)
  215
+        return unless default_generator_root
  216
+        path = File.join(default_generator_root, 'templates')
217 217
         path if File.exists?(path)
218 218
       end
219 219
 
@@ -373,6 +373,19 @@ def self.add_shebang_option!
373 373
           }
374 374
         end
375 375
 
  376
+        def self.usage_path
  377
+          paths = [
  378
+            source_root && File.expand_path("../USAGE", source_root),
  379
+            default_generator_root && File.join(default_generator_root, "USAGE")
  380
+          ]
  381
+          paths.compact.detect { |path| File.exists? path }
  382
+        end
  383
+
  384
+        def self.default_generator_root
  385
+          path = File.expand_path(File.join(base_name, generator_name), base_root)
  386
+          path if File.exists?(path)
  387
+        end
  388
+
376 389
     end
377 390
   end
378 391
 end
2  railties/test/generators/app_generator_test.rb
@@ -302,7 +302,7 @@ def test_usage_read_from_file
302 302
   end
303 303
 
304 304
   def test_default_usage
305  
-    File.expects(:exist?).returns(false)
  305
+    Rails::Generators::AppGenerator.expects(:usage_path).returns(nil)
306 306
     assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc)
307 307
   end
308 308
 
4  railties/test/generators/migration_generator_test.rb
@@ -152,4 +152,8 @@ def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove
152 152
       end
153 153
     end
154 154
   end
  155
+
  156
+  def test_properly_identifies_usage_file
  157
+    assert generator_class.send(:usage_path)
  158
+  end
155 159
 end

0 notes on commit fff3e75

Please sign in to comment.
Something went wrong with that request. Please try again.