aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-06-02 15:53:10 +0200
committerwycats <wycats@gmail.com>2010-06-02 15:53:10 +0200
commit26c5680bd01bd0e525eccc5d47a2e7126d9383f7 (patch)
treec655c56511ef89ba55c554a0aa7d82b1cd062f78 /actionpack
parentb870daba5ff71973b237616fb95f90bb321ae7fb (diff)
parent4b91daff13be43ed913a97ffc8ad1b3f77fd9690 (diff)
downloadrails-26c5680bd01bd0e525eccc5d47a2e7126d9383f7.tar.gz
rails-26c5680bd01bd0e525eccc5d47a2e7126d9383f7.tar.bz2
rails-26c5680bd01bd0e525eccc5d47a2e7126d9383f7.zip
Merge branch 'master' of github.com:rails/rails
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/actionpack.gemspec5
-rw-r--r--actionpack/lib/action_controller/metal.rb48
-rw-r--r--actionpack/lib/action_dispatch/middleware/stack.rb8
-rw-r--r--actionpack/lib/action_view/base.rb4
-rw-r--r--actionpack/lib/action_view/helpers/active_model_helper.rb7
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb32
-rw-r--r--actionpack/test/controller/new_base/middleware_test.rb33
7 files changed, 92 insertions, 45 deletions
diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec
index a5abe9be10..0f45cb5a4a 100644
--- a/actionpack/actionpack.gemspec
+++ b/actionpack/actionpack.gemspec
@@ -21,8 +21,11 @@ Gem::Specification.new do |s|
s.add_dependency('activesupport', version)
s.add_dependency('activemodel', version)
+ s.add_dependency('builder', '~> 2.1.2')
+ s.add_dependency('i18n', '~> 0.4.0')
s.add_dependency('rack', '~> 1.1.0')
- s.add_dependency('rack-test', '~> 0.5.0')
+ s.add_dependency('rack-test', '~> 0.5.4')
s.add_dependency('rack-mount', '~> 0.6.3')
+ s.add_dependency('tzinfo', '~> 0.3.16')
s.add_dependency('erubis', '~> 2.6.5')
end
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb
index 30aa34d956..775a5002e2 100644
--- a/actionpack/lib/action_controller/metal.rb
+++ b/actionpack/lib/action_controller/metal.rb
@@ -1,6 +1,48 @@
require 'active_support/core_ext/class/attribute'
+require 'active_support/core_ext/object/blank'
+require 'action_dispatch/middleware/stack'
module ActionController
+ # Extend ActionDispatch middleware stack to make it aware of options
+ # allowing the following syntax in controllers:
+ #
+ # class PostsController < ApplicationController
+ # use AuthenticationMiddleware, :except => [:index, :show]
+ # end
+ #
+ class MiddlewareStack < ActionDispatch::MiddlewareStack #:nodoc:
+ class Middleware < ActionDispatch::MiddlewareStack::Middleware #:nodoc:
+ def initialize(klass, *args)
+ options = args.extract_options!
+ @only = Array(options.delete(:only)).map(&:to_s)
+ @except = Array(options.delete(:except)).map(&:to_s)
+ args << options unless options.empty?
+ super
+ end
+
+ def valid?(action)
+ if @only.present?
+ @only.include?(action)
+ elsif @except.present?
+ !@except.include?(action)
+ else
+ true
+ end
+ end
+ end
+
+ def build(action, app=nil, &block)
+ app ||= block
+ action = action.to_s
+ raise "MiddlewareStack#build requires an app" unless app
+
+ reverse.inject(app) do |a, middleware|
+ middleware.valid?(action) ?
+ middleware.build(a) : a
+ end
+ end
+ end
+
# ActionController::Metal provides a way to get a valid Rack application from a controller.
#
# In AbstractController, dispatching is triggered directly by calling #process on a new controller.
@@ -91,10 +133,10 @@ module ActionController
end
class_attribute :middleware_stack
- self.middleware_stack = ActionDispatch::MiddlewareStack.new
+ self.middleware_stack = ActionController::MiddlewareStack.new
def self.inherited(base)
- self.middleware_stack = base.middleware_stack.dup
+ base.middleware_stack = self.middleware_stack.dup
super
end
@@ -120,7 +162,7 @@ module ActionController
# ==== Returns
# Proc:: A rack application
def self.action(name, klass = ActionDispatch::Request)
- middleware_stack.build do |env|
+ middleware_stack.build(name.to_s) do |env|
new.dispatch(name, klass.new(env))
end
end
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index 0e5ab507df..e3180dba77 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -55,7 +55,7 @@ module ActionDispatch
def insert(index, *args, &block)
index = self.index(index) unless index.is_a?(Integer)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
super(index, middleware)
end
@@ -73,7 +73,7 @@ module ActionDispatch
end
def use(*args, &block)
- middleware = Middleware.new(*args, &block)
+ middleware = self.class::Middleware.new(*args, &block)
push(middleware)
end
@@ -82,8 +82,8 @@ module ActionDispatch
"was removed from the middleware stack", caller
end
- def build(app = nil, &blk)
- app ||= blk
+ def build(app = nil, &block)
+ app ||= block
raise "MiddlewareStack#build requires an app" unless app
reverse.inject(app) { |a, e| e.build(a) }
end
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 735398d972..f4af763afe 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -165,6 +165,10 @@ module ActionView #:nodoc:
cattr_accessor :debug_rjs
@@debug_rjs = false
+ # Specify the proc used to decorate input tags that refer to attributes with errors.
+ cattr_accessor :field_error_proc
+ @@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
+
class_attribute :helpers
remove_method :helpers
attr_reader :helpers
diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb
index 0cddd09eb0..8054de0af6 100644
--- a/actionpack/lib/action_view/helpers/active_model_helper.rb
+++ b/actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -4,13 +4,6 @@ require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/object/blank'
module ActionView
- ActiveSupport.on_load(:action_view) do
- class ActionView::Base
- @@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
- cattr_accessor :field_error_proc
- end
- end
-
module Helpers
module ActiveModelHelper
%w(input form error_messages_for error_message_on).each do |method|
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 41423d4e2e..bfad9f8d31 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/string/filters'
require 'action_view/helpers/tag_helper'
module ActionView
@@ -42,28 +43,25 @@ module ActionView
# ==== Examples
#
# truncate("Once upon a time in a world far far away")
- # # => Once upon a time in a world...
+ # # => "Once upon a time in a world..."
#
- # truncate("Once upon a time in a world far far away", :separator => ' ')
- # # => Once upon a time in a world...
+ # truncate("Once upon a time in a world far far away", :length => 17)
+ # # => "Once upon a ti..."
#
- # truncate("Once upon a time in a world far far away", :length => 14)
- # # => Once upon a...
+ # truncate("Once upon a time in a world far far away", :lenght => 17, :separator => ' ')
+ # # => "Once upon a..."
#
- # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
- # # => And they found t(clipped)
- #
- # truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 25)
- # # => And they f... (continued)
+ # truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
+ # # => "And they f... (continued)"
#
# You can still use <tt>truncate</tt> with the old API that accepts the
# +length+ as its optional second and the +ellipsis+ as its
# optional third parameter:
# truncate("Once upon a time in a world far far away", 14)
- # # => Once upon a...
+ # # => "Once upon a..."
#
# truncate("And they found that many people were sleeping better.", 25, "... (continued)")
- # # => And they f... (continued)
+ # # => "And they f... (continued)"
def truncate(text, *args)
options = args.extract_options!
unless args.empty?
@@ -73,14 +71,10 @@ module ActionView
options[:length] = args[0] || 30
options[:omission] = args[1] || "..."
end
- options.reverse_merge!(:length => 30, :omission => "...")
- if text
- l = options[:length] - options[:omission].mb_chars.length
- chars = text.mb_chars
- stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, l) || l) : l
- (chars.length > options[:length] ? chars[0...stop] + options[:omission] : text).to_s
- end
+ options.reverse_merge!(:length => 30)
+
+ text.truncate(options.delete(:length), options) if text
end
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into
diff --git a/actionpack/test/controller/new_base/middleware_test.rb b/actionpack/test/controller/new_base/middleware_test.rb
index 65942ebc15..26a66c91a6 100644
--- a/actionpack/test/controller/new_base/middleware_test.rb
+++ b/actionpack/test/controller/new_base/middleware_test.rb
@@ -28,7 +28,6 @@ module MiddlewareTest
class MyController < ActionController::Metal
use MyMiddleware
-
middleware.insert_before MyMiddleware, ExclaimerMiddleware
def index
@@ -39,8 +38,23 @@ module MiddlewareTest
class InheritedController < MyController
end
- module MiddlewareTests
- extend ActiveSupport::Testing::Declarative
+ class ActionsController < ActionController::Metal
+ use MyMiddleware, :only => :show
+ middleware.insert_before MyMiddleware, ExclaimerMiddleware, :except => :index
+
+ def index
+ self.response_body = "index"
+ end
+
+ def show
+ self.response_body = "show"
+ end
+ end
+
+ class TestMiddleware < ActiveSupport::TestCase
+ def setup
+ @app = MyController.action(:index)
+ end
test "middleware that is 'use'd is called as part of the Rack application" do
result = @app.call(env_for("/"))
@@ -52,13 +66,13 @@ module MiddlewareTest
result = @app.call(env_for("/"))
assert_equal "First!", result[1]["Middleware-Order"]
end
- end
- class TestMiddleware < ActiveSupport::TestCase
- include MiddlewareTests
+ test "middleware stack accepts only and except as options" do
+ result = ActionsController.action(:show).call(env_for("/"))
+ assert_equal "First!", result[1]["Middleware-Order"]
- def setup
- @app = MyController.action(:index)
+ result = ActionsController.action(:index).call(env_for("/"))
+ assert_nil result[1]["Middleware-Order"]
end
def env_for(url)
@@ -70,8 +84,5 @@ module MiddlewareTest
def setup
@app = InheritedController.action(:index)
end
-
- test "middleware inherits" do
- end
end
end