Skip to content
This repository
Browse code

Create generators http_only! setup and hide some common namespaces fo…

…r now

[Carlos Antonio da Silva & Santiago Pastorino]
  • Loading branch information...
commit c825e9a5eaa8060a3745c5275b6829c37c6545a7 1 parent 124838b
Carlos Antonio da Silva authored March 10, 2012 spastorino committed March 14, 2012
14  railties/lib/rails/configuration.rb
@@ -44,14 +44,14 @@ module Configuration
44 44
     # Cookies, Session and Flash, BestStandardsSupport, and MethodOverride. You
45 45
     # can always add any of them later manually if you want.
46 46
     class MiddlewareStackProxy
  47
+      attr_reader :http_only
  48
+      alias       :http_only? :http_only
  49
+
47 50
       def initialize
48 51
         @operations = []
49 52
         @http_only  = false
50 53
       end
51 54
 
52  
-      attr_reader :http_only
53  
-      alias       :http_only? :http_only
54  
-
55 55
       def http_only!
56 56
         @http_only = true
57 57
       end
@@ -90,6 +90,9 @@ class Generators #:nodoc:
90 90
       attr_accessor :aliases, :options, :templates, :fallbacks, :colorize_logging
91 91
       attr_reader :hidden_namespaces
92 92
 
  93
+      attr_reader :http_only
  94
+      alias       :http_only? :http_only
  95
+
93 96
       def initialize
94 97
         @aliases = Hash.new { |h,k| h[k] = {} }
95 98
         @options = Hash.new { |h,k| h[k] = {} }
@@ -97,6 +100,7 @@ def initialize
97 100
         @templates = []
98 101
         @colorize_logging = true
99 102
         @hidden_namespaces = []
  103
+        @http_only = false
100 104
       end
101 105
 
102 106
       def initialize_copy(source)
@@ -110,6 +114,10 @@ def hide_namespace(namespace)
110 114
         @hidden_namespaces << namespace
111 115
       end
112 116
 
  117
+      def http_only!
  118
+        @http_only = true
  119
+      end
  120
+
113 121
       def method_missing(method, *args)
114 122
         method = method.to_s.sub(/=$/, '').to_sym
115 123
 
8  railties/lib/rails/generators.rb
@@ -68,6 +68,7 @@ def self.configure!(config) #:nodoc:
68 68
       templates_path.concat config.templates
69 69
       templates_path.uniq!
70 70
       hide_namespaces(*config.hidden_namespaces)
  71
+      http_only! if config.http_only?
71 72
     end
72 73
 
73 74
     def self.templates_path
@@ -104,6 +105,11 @@ def self.no_color!
104 105
       Thor::Base.shell = Thor::Shell::Basic
105 106
     end
106 107
 
  108
+    # Configure generators for http only
  109
+    def self.http_only!
  110
+      hide_namespaces "assets", "css", "js", "session_migration"
  111
+    end
  112
+
107 113
     # Track all generators subclasses.
108 114
     def self.subclasses
109 115
       @subclasses ||= []
@@ -235,7 +241,7 @@ def self.help(command = 'generate')
235 241
       rails.delete("plugin_new")
236 242
       print_list("rails", rails)
237 243
 
238  
-      hidden_namespaces.each {|n| groups.delete(n.to_s) }
  244
+      hidden_namespaces.each { |n| groups.delete(n.to_s) }
239 245
 
240 246
       groups.sort.each { |b, n| print_list(b, n) }
241 247
     end
24  railties/test/generators_test.rb
@@ -213,4 +213,28 @@ def test_hide_namespace
213 213
     Rails::Generators.hide_namespace("special:namespace")
214 214
     assert Rails::Generators.hidden_namespaces.include?("special:namespace")
215 215
   end
  216
+
  217
+  def test_http_only_hides_generators
  218
+    generators = %w(assets js css session_migration)
  219
+
  220
+    generators.each do |generator|
  221
+      assert !Rails::Generators.hidden_namespaces.include?(generator)
  222
+    end
  223
+
  224
+    with_http_only! do
  225
+      generators.each do |generator|
  226
+        assert Rails::Generators.hidden_namespaces.include?(generator),
  227
+          "http only should hide #{generator} generator"
  228
+      end
  229
+    end
  230
+  end
  231
+
  232
+  private
  233
+
  234
+  def with_http_only!
  235
+    Rails::Generators.http_only!
  236
+    yield
  237
+  ensure
  238
+    Rails::Generators.instance_variable_set(:@http_only, false)
  239
+  end
216 240
 end

0 notes on commit c825e9a

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