From 9a93844aba44319d3c8487a554124beb00ccc267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 30 May 2010 15:53:14 +0200 Subject: Add :only and :except to controllers MiddlewareStack. This allows you to do the following: class PostsController < ApplicationController use AutheMiddleware, :except => [:index, :show] end --- actionpack/lib/action_controller/metal.rb | 48 ++++++++++++++++++++-- actionpack/lib/action_dispatch/middleware/stack.rb | 8 ++-- .../test/controller/new_base/middleware_test.rb | 33 ++++++++++----- 3 files changed, 71 insertions(+), 18 deletions(-) (limited to 'actionpack') 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/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 -- cgit v1.2.3 From f5104caf3b36a206aaf8a4355a2b3a73bbf55e5b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 29 May 2010 16:40:16 -0300 Subject: rack-test version updated and removed from Gemfile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/actionpack.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index a5abe9be10..64a4441034 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', version) s.add_dependency('activemodel', version) 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('erubis', '~> 2.6.5') end -- cgit v1.2.3 From 1854209bb3d06bc81034dfec4ef75412700eae74 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 31 May 2010 07:31:08 -0300 Subject: Unforce i18n from AS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/actionpack.gemspec | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 64a4441034..040d05f050 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', version) s.add_dependency('activemodel', version) + s.add_dependency('i18n', '~> 0.4.0') s.add_dependency('rack', '~> 1.1.0') s.add_dependency('rack-test', '~> 0.5.4') s.add_dependency('rack-mount', '~> 0.6.3') -- cgit v1.2.3 From ea037ff55791a33d24773efd380b734f733c2815 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 31 May 2010 13:48:47 -0500 Subject: Base options cant live in lazy loaded helpers as they then wont be available to set for config --- actionpack/lib/action_view/base.rb | 4 ++++ actionpack/lib/action_view/helpers/active_model_helper.rb | 7 ------- 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'actionpack') 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| "
#{html_tag}
".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| "
#{html_tag}
".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| -- cgit v1.2.3 From 0bed93be25e6bcae2b1a74cec70413118be66736 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 1 Jun 2010 04:12:04 -0300 Subject: Unforce tzinfo from AS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/actionpack.gemspec | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 040d05f050..f20c964003 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -25,5 +25,6 @@ Gem::Specification.new do |s| s.add_dependency('rack', '~> 1.1.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 -- cgit v1.2.3 From 31e1445a8194f1eab70ac8c7d45797f0708c6c8e Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 1 Jun 2010 04:57:34 -0300 Subject: Unforce builder from AS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- actionpack/actionpack.gemspec | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index f20c964003..0f45cb5a4a 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -21,6 +21,7 @@ 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.4') -- cgit v1.2.3 From d57397c4b62b6474ff8eb55bfd763f5e6dcdcd40 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 1 Jun 2010 16:38:42 -0500 Subject: Extracted String#truncate from TextHelper#truncate [DHH] --- actionpack/lib/action_view/helpers/text_helper.rb | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 41423d4e2e..860c1de6af 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,7 +43,7 @@ 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 worl... # # truncate("Once upon a time in a world far far away", :separator => ' ') # # => Once upon a time in a world... @@ -50,9 +51,6 @@ module ActionView # truncate("Once upon a time in a world far far away", :length => 14) # # => 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) # @@ -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 -- cgit v1.2.3 From 315e8952dfbaecd4d5175ea4d0fd95611cad9e01 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 2 Jun 2010 01:34:39 +0200 Subject: revises the documentation of String#truncate and the truncate helper --- actionpack/lib/action_view/helpers/text_helper.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 860c1de6af..bfad9f8d31 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -43,25 +43,25 @@ module ActionView # ==== Examples # # truncate("Once upon a time in a world far far away") - # # => Once upon a time in a worl... + # # => "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.", :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 truncate 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? -- cgit v1.2.3