Skip to content
This repository
Browse code

Merge commit 'rails/master'

  • Loading branch information...
commit 51910106f196e4461b35661f90a835dec0c21ccf 2 parents 689b89f + c3a0a36
Emilio Tagua authored August 25, 2009
40  actionpack/lib/action_controller/metal.rb
@@ -47,7 +47,7 @@ def controller_path
47 47
     # and response object available. You might wish to control the
48 48
     # environment and response manually for performance reasons.
49 49
 
50  
-    attr_internal :status, :headers, :content_type
  50
+    attr_internal :status, :headers, :content_type, :app, :response
51 51
 
52 52
     def initialize(*)
53 53
       @_headers = {}
@@ -75,7 +75,34 @@ def call(name, env)
75 75
 
76 76
     # :api: private
77 77
     def to_a
78  
-      [status, headers, response_body]
  78
+      response ? response.to_a : [status, headers, response_body]
  79
+    end
  80
+
  81
+    class ActionEndpoint
  82
+      def initialize(controller, action)
  83
+        @controller, @action = controller, action
  84
+      end
  85
+
  86
+      def call(env)
  87
+        controller = @controller.new.call(@action, env)
  88
+      end
  89
+    end
  90
+
  91
+    class ActionMiddleware
  92
+      def initialize(controller, action)
  93
+        @controller, @action = controller, action
  94
+      end
  95
+
  96
+      def call(env)
  97
+        controller = @controller.new
  98
+        controller.app = @app
  99
+        controller.call(@action, env)
  100
+      end
  101
+
  102
+      def new(app)
  103
+        @app = app
  104
+        self
  105
+      end
79 106
     end
80 107
 
81 108
     # Return a rack endpoint for the given action. Memoize the endpoint, so
@@ -89,9 +116,12 @@ def to_a
89 116
     # Proc:: A rack application
90 117
     def self.action(name)
91 118
       @actions ||= {}
92  
-      @actions[name.to_s] ||= proc do |env|
93  
-        new.call(name, env)
94  
-      end
  119
+      @actions[name.to_s] ||= ActionEndpoint.new(self, name)
  120
+    end
  121
+
  122
+    def self.middleware(name)
  123
+      @middlewares ||= {}
  124
+      @middlewares[name.to_s] ||= ActionMiddleware.new(self, name)
95 125
     end
96 126
   end
97 127
 end
6  actionpack/lib/action_controller/metal/compatibility.rb
@@ -16,12 +16,6 @@ class ::ActionController::ActionControllerError < StandardError #:nodoc:
16 16
       cattr_accessor :allow_concurrency
17 17
       self.allow_concurrency = false
18 18
 
19  
-      cattr_accessor :param_parsers
20  
-      self.param_parsers = { Mime::MULTIPART_FORM   => :multipart_form,
21  
-                             Mime::URL_ENCODED_FORM => :url_encoded_form,
22  
-                             Mime::XML              => :xml_simple,
23  
-                             Mime::JSON             => :json }
24  
-
25 19
       cattr_accessor :relative_url_root
26 20
       self.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
27 21
 
8  actionpack/lib/action_controller/metal/rack_convenience.rb
@@ -5,7 +5,7 @@ module RackConvenience
5 5
     included do
6 6
       delegate :headers, :status=, :location=, :content_type=,
7 7
                :status, :location, :content_type, :to => "@_response"
8  
-      attr_internal :request, :response
  8
+      attr_internal :request
9 9
     end
10 10
 
11 11
     def call(name, env)
@@ -19,12 +19,6 @@ def params
19 19
       @_params ||= @_request.parameters
20 20
     end
21 21
 
22  
-    # :api: private
23  
-    def to_a
24  
-      @_response.prepare!
25  
-      @_response.to_a
26  
-    end
27  
-
28 22
     def response_body=(body)
29 23
       response.body = body if response
30 24
       super
5  actionpack/lib/action_dispatch/http/response.rb
@@ -161,13 +161,16 @@ def assign_default_content_type_and_charset!
161 161
       headers[CONTENT_TYPE] = type
162 162
     end
163 163
 
164  
-    def prepare!
  164
+    def to_a
165 165
       assign_default_content_type_and_charset!
166 166
       handle_conditional_get!
167 167
       self["Set-Cookie"] = @cookie.join("\n")
168 168
       self["ETag"]       = @etag if @etag
  169
+      super
169 170
     end
170 171
 
  172
+    alias prepare! to_a
  173
+
171 174
     def each(&callback)
172 175
       if @body.respond_to?(:call)
173 176
         @writer = lambda { |x| callback.call(x) }
12  actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -2,11 +2,13 @@
2 2
 
3 3
 module ActionDispatch
4 4
   class ParamsParser
5  
-    ActionController::Base.param_parsers[Mime::XML] = :xml_simple
6  
-    ActionController::Base.param_parsers[Mime::JSON] = :json
  5
+    DEFAULT_PARSERS = {
  6
+      Mime::XML => :xml_simple,
  7
+      Mime::JSON => :json
  8
+    }
7 9
 
8  
-    def initialize(app)
9  
-      @app = app
  10
+    def initialize(app, parsers = {})
  11
+      @app, @parsers = app, DEFAULT_PARSERS.merge(parsers)
10 12
     end
11 13
 
12 14
     def call(env)
@@ -24,7 +26,7 @@ def parse_formatted_parameters(env)
24 26
         return false if request.content_length.zero?
25 27
 
26 28
         mime_type = content_type_from_legacy_post_data_format_header(env) || request.content_type
27  
-        strategy = ActionController::Base.param_parsers[mime_type]
  29
+        strategy = @parsers[mime_type]
28 30
 
29 31
         return false unless strategy
30 32
 
129  actionpack/test/controller/webservice_test.rb
@@ -24,11 +24,6 @@ def rescue_action(e) raise end
24 24
 
25 25
   def setup
26 26
     @controller = TestController.new
27  
-    @default_param_parsers = ActionController::Base.param_parsers.dup
28  
-  end
29  
-
30  
-  def teardown
31  
-    ActionController::Base.param_parsers = @default_param_parsers
32 27
   end
33 28
 
34 29
   def test_check_parameters
@@ -110,58 +105,61 @@ def test_post_xml_using_a_root_node_named_type
110 105
 
111 106
   def test_post_xml_using_an_attributted_node_named_type
112 107
     with_test_route_set do
113  
-      ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access }
114  
-      post "/", '<request><type type="string">Arial,12</type><z>3</z></request>',
115  
-        {'CONTENT_TYPE' => 'application/xml'}
116  
-
117  
-      assert_equal 'type, z', @controller.response.body
118  
-      assert @controller.params.has_key?(:type)
119  
-      assert_equal 'Arial,12', @controller.params['type'], @controller.params.inspect
120  
-      assert_equal '3', @controller.params['z'], @controller.params.inspect
  108
+      with_params_parsers Mime::XML => Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access } do
  109
+        post "/", '<request><type type="string">Arial,12</type><z>3</z></request>',
  110
+          {'CONTENT_TYPE' => 'application/xml'}
  111
+
  112
+        assert_equal 'type, z', @controller.response.body
  113
+        assert @controller.params.has_key?(:type)
  114
+        assert_equal 'Arial,12', @controller.params['type'], @controller.params.inspect
  115
+        assert_equal '3', @controller.params['z'], @controller.params.inspect
  116
+      end
121 117
     end
122 118
   end
123 119
 
124 120
   def test_register_and_use_yaml
125 121
     with_test_route_set do
126  
-      ActionController::Base.param_parsers[Mime::YAML] = Proc.new { |d| YAML.load(d) }
127  
-      post "/", {"entry" => "loaded from yaml"}.to_yaml,
128  
-        {'CONTENT_TYPE' => 'application/x-yaml'}
  122
+      with_params_parsers Mime::YAML => Proc.new { |d| YAML.load(d) } do
  123
+        post "/", {"entry" => "loaded from yaml"}.to_yaml,
  124
+          {'CONTENT_TYPE' => 'application/x-yaml'}
129 125
 
130  
-      assert_equal 'entry', @controller.response.body
131  
-      assert @controller.params.has_key?(:entry)
132  
-      assert_equal 'loaded from yaml', @controller.params["entry"]
  126
+        assert_equal 'entry', @controller.response.body
  127
+        assert @controller.params.has_key?(:entry)
  128
+        assert_equal 'loaded from yaml', @controller.params["entry"]
  129
+      end
133 130
     end
134 131
   end
135 132
 
136 133
   def test_register_and_use_yaml_as_symbol
137 134
     with_test_route_set do
138  
-      ActionController::Base.param_parsers[Mime::YAML] = :yaml
139  
-      post "/", {"entry" => "loaded from yaml"}.to_yaml,
140  
-        {'CONTENT_TYPE' => 'application/x-yaml'}
  135
+      with_params_parsers Mime::YAML => :yaml do
  136
+        post "/", {"entry" => "loaded from yaml"}.to_yaml,
  137
+          {'CONTENT_TYPE' => 'application/x-yaml'}
141 138
 
142  
-      assert_equal 'entry', @controller.response.body
143  
-      assert @controller.params.has_key?(:entry)
144  
-      assert_equal 'loaded from yaml', @controller.params["entry"]
  139
+        assert_equal 'entry', @controller.response.body
  140
+        assert @controller.params.has_key?(:entry)
  141
+        assert_equal 'loaded from yaml', @controller.params["entry"]
  142
+      end
145 143
     end
146 144
   end
147 145
 
148 146
   def test_register_and_use_xml_simple
149 147
     with_test_route_set do
150  
-      ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access }
151  
-      post "/", '<request><summary>content...</summary><title>SimpleXml</title></request>',
152  
-        {'CONTENT_TYPE' => 'application/xml'}
153  
-
154  
-      assert_equal 'summary, title', @controller.response.body
155  
-      assert @controller.params.has_key?(:summary)
156  
-      assert @controller.params.has_key?(:title)
157  
-      assert_equal 'content...', @controller.params["summary"]
158  
-      assert_equal 'SimpleXml', @controller.params["title"]
  148
+      with_params_parsers Mime::XML => Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access } do
  149
+        post "/", '<request><summary>content...</summary><title>SimpleXml</title></request>',
  150
+          {'CONTENT_TYPE' => 'application/xml'}
  151
+
  152
+        assert_equal 'summary, title', @controller.response.body
  153
+        assert @controller.params.has_key?(:summary)
  154
+        assert @controller.params.has_key?(:title)
  155
+        assert_equal 'content...', @controller.params["summary"]
  156
+        assert_equal 'SimpleXml', @controller.params["title"]
  157
+      end
159 158
     end
160 159
   end
161 160
 
162 161
   def test_use_xml_ximple_with_empty_request
163 162
     with_test_route_set do
164  
-      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
165 163
       assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
166 164
       assert @controller.response.body.blank?
167 165
     end
@@ -169,7 +167,6 @@ def test_use_xml_ximple_with_empty_request
169 167
 
170 168
   def test_dasherized_keys_as_xml
171 169
     with_test_route_set do
172  
-      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
173 170
       post "/?full=1", "<first-key>\n<sub-key>...</sub-key>\n</first-key>",
174 171
         {'CONTENT_TYPE' => 'application/xml'}
175 172
       assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body
@@ -179,7 +176,6 @@ def test_dasherized_keys_as_xml
179 176
 
180 177
   def test_typecast_as_xml
181 178
     with_test_route_set do
182  
-      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
183 179
       xml = <<-XML
184 180
         <data>
185 181
           <a type="integer">15</a>
@@ -208,7 +204,6 @@ def test_typecast_as_xml
208 204
 
209 205
   def test_entities_unescaped_as_xml_simple
210 206
     with_test_route_set do
211  
-      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
212 207
       xml = <<-XML
213 208
         <data>&lt;foo &quot;bar&apos;s&quot; &amp; friends&gt;</data>
214 209
       XML
@@ -219,34 +214,44 @@ def test_entities_unescaped_as_xml_simple
219 214
 
220 215
   def test_typecast_as_yaml
221 216
     with_test_route_set do
222  
-      ActionController::Base.param_parsers[Mime::YAML] = :yaml
223  
-      yaml = <<-YAML
224  
-        ---
225  
-        data:
226  
-          a: 15
227  
-          b: false
228  
-          c: true
229  
-          d: 2005-03-17
230  
-          e: 2005-03-17T21:41:07Z
231  
-          f: unparsed
232  
-          g:
233  
-            - 1
234  
-            - hello
235  
-            - 1974-07-25
236  
-      YAML
237  
-      post "/", yaml, {'CONTENT_TYPE' => 'application/x-yaml'}
238  
-      params = @controller.params
239  
-      assert_equal 15, params[:data][:a]
240  
-      assert_equal false, params[:data][:b]
241  
-      assert_equal true, params[:data][:c]
242  
-      assert_equal Date.new(2005,3,17), params[:data][:d]
243  
-      assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
244  
-      assert_equal "unparsed", params[:data][:f]
245  
-      assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
  217
+      with_params_parsers Mime::YAML => :yaml do
  218
+        yaml = <<-YAML
  219
+          ---
  220
+          data:
  221
+            a: 15
  222
+            b: false
  223
+            c: true
  224
+            d: 2005-03-17
  225
+            e: 2005-03-17T21:41:07Z
  226
+            f: unparsed
  227
+            g:
  228
+              - 1
  229
+              - hello
  230
+              - 1974-07-25
  231
+        YAML
  232
+        post "/", yaml, {'CONTENT_TYPE' => 'application/x-yaml'}
  233
+        params = @controller.params
  234
+        assert_equal 15, params[:data][:a]
  235
+        assert_equal false, params[:data][:b]
  236
+        assert_equal true, params[:data][:c]
  237
+        assert_equal Date.new(2005,3,17), params[:data][:d]
  238
+        assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
  239
+        assert_equal "unparsed", params[:data][:f]
  240
+        assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
  241
+      end
246 242
     end
247 243
   end
248 244
 
249 245
   private
  246
+    def with_params_parsers(parsers = {})
  247
+      old_session = @integration_session
  248
+      app = ActionDispatch::ParamsParser.new(ActionController::Routing::Routes, parsers)
  249
+      @integration_session = open_session(app)
  250
+      yield
  251
+    ensure
  252
+      @integration_session = old_session
  253
+    end
  254
+
250 255
     def with_test_route_set
251 256
       with_routing do |set|
252 257
         set.draw do |map|
45  actionpack/test/new_base/metal_test.rb
... ...
@@ -0,0 +1,45 @@
  1
+require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
  2
+
  3
+module MetalTest
  4
+  class MetalMiddleware < ActionController::Metal
  5
+    def index
  6
+      if env["PATH_INFO"] =~ /authed/
  7
+        self.response = app.call(env)
  8
+      else
  9
+        self.response_body = "Not authed!"
  10
+        self.status = 401
  11
+      end
  12
+    end
  13
+  end
  14
+
  15
+  class Endpoint
  16
+    def call(env)
  17
+      [200, {}, "Hello World"]
  18
+    end
  19
+  end
  20
+
  21
+  class TestMiddleware < ActiveSupport::TestCase
  22
+    def setup
  23
+      @app = Rack::Builder.new do
  24
+        use MetalMiddleware.middleware(:index)
  25
+        run Endpoint.new
  26
+      end.to_app
  27
+    end
  28
+
  29
+    test "it can call the next app by using @app" do
  30
+      env = Rack::MockRequest.env_for("/authed")
  31
+      response = @app.call(env)
  32
+
  33
+      assert_equal "Hello World", response[2]
  34
+    end
  35
+
  36
+    test "it can return a response using the normal AC::Metal techniques" do
  37
+      env = Rack::MockRequest.env_for("/")
  38
+      response = @app.call(env)
  39
+
  40
+      assert_equal "Not authed!", response[2]
  41
+      assert_equal 401, response[0]
  42
+    end
  43
+  end
  44
+end
  45
+
6  activerecord/examples/performance.rb
@@ -75,13 +75,13 @@ def self.feel(exhibits) exhibits.each { |e| e.feel } end
75 75
   puts 'Inserting 10,000 users and exhibits...'
76 76
   10_000.times do
77 77
     user = User.create(
78  
-      :created_on => today,
  78
+      :created_at => today,
79 79
       :name       => Faker::Name.name,
80 80
       :email      => Faker::Internet.email
81 81
     )
82 82
 
83 83
     Exhibit.create(
84  
-      :created_on => today,
  84
+      :created_at => today,
85 85
       :name       => Faker::Company.name,
86 86
       :user       => user,
87 87
       :notes      => notes
@@ -130,7 +130,7 @@ def self.feel(exhibits) exhibits.each { |e| e.feel } end
130 130
   exhibit = {
131 131
     :name       => Faker::Company.name,
132 132
     :notes      => Faker::Lorem.paragraphs.join($/),
133  
-    :created_on => Date.today
  133
+    :created_at => Date.today
134 134
   }
135 135
 
136 136
   report 'Model.create' do
3  activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -172,7 +172,8 @@ def next_week(day = :monday)
172 172
 
173 173
   # Returns a new Time representing the start of the day (0:00)
174 174
   def beginning_of_day
175  
-    (self - seconds_since_midnight).change(:usec => 0)
  175
+    #(self - seconds_since_midnight).change(:usec => 0)
  176
+    change(:hour => 0, :min => 0, :sec => 0, :usec => 0)
176 177
   end
177 178
   alias :midnight :beginning_of_day
178 179
   alias :at_midnight :beginning_of_day

0 notes on commit 5191010

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