diff options
Diffstat (limited to 'actionpack/lib')
3 files changed, 59 insertions, 10 deletions
| diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index 2694d4c12f..c42384b741 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -14,12 +14,57 @@ module ActionController      #      #   expire_fragment('name_of_cache')      module Fragments +      extend ActiveSupport::Concern + +      included do +        if respond_to?(:class_attribute) +          class_attribute :fragment_cache_keys +        else +          mattr_writer :fragment_cache_keys +        end + +        self.fragment_cache_keys = [] + +        helper_method :fragment_cache_key if respond_to?(:helper_method) +      end + +      module ClassMethods +        # Allows you to specify controller-wide key prefixes for +        # cache fragments. Pass either a constant +value+, or a block +        # which computes a value each time a cache key is generated. +        # +        # For example, you may want to prefix all fragment cache keys +        # with a global version identifier, so you can easily +        # invalidate all caches. +        # +        #   class ApplicationController +        #     fragment_cache_key "v1" +        #   end +        # +        # When it's time to invalidate all fragments, simply change +        # the string constant. Or, progressively roll out the cache +        # invalidation using a computed value: +        # +        #   class ApplicationController +        #     fragment_cache_key do +        #       @account.id.odd? ? "v1" : "v2" +        #     end +        #   end +        def fragment_cache_key(value = nil, &key) +          self.fragment_cache_keys += [key || ->{ value }] +        end +      end +        # Given a key (as described in +expire_fragment+), returns        # a key suitable for use in reading, writing, or expiring a -      # cached fragment. All keys are prefixed with <tt>views/</tt> and uses -      # ActiveSupport::Cache.expand_cache_key for the expansion. +      # cached fragment. All keys begin with <tt>views/</tt>, +      # followed by any controller-wide key prefix values, ending +      # with the specified +key+ value. The key is expanded using +      # ActiveSupport::Cache.expand_cache_key.        def fragment_cache_key(key) -        ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views) +        head = self.class.fragment_cache_keys.map { |key| instance_exec(&key) } +        tail = key.is_a?(Hash) ? url_for(key).split("://").last : key +        ActiveSupport::Cache.expand_cache_key([*head, *tail], :views)        end        # Writes +content+ to the location signified by diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 8af94551cf..8bc3c271e2 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -162,8 +162,8 @@ module ActionController        end      end -    # Returns a safe +Hash+ representation of this parameter with all -    # unpermitted keys removed. +    # Returns a safe <tt>ActiveSupport::HashWithIndifferentAccess</tt> +    # representation of this parameter with all unpermitted keys removed.      #      #   params = ActionController::Parameters.new({      #     name: 'Senjougahara Hitagi', @@ -175,15 +175,17 @@ module ActionController      #   safe_params.to_h # => {"name"=>"Senjougahara Hitagi"}      def to_h        if permitted? -        @parameters.to_h +        @parameters.deep_dup        else          slice(*self.class.always_permitted_parameters).permit!.to_h        end      end -    # Returns an unsafe, unfiltered +Hash+ representation of this parameter. +    # Returns an unsafe, unfiltered +    # <tt>ActiveSupport::HashWithIndifferentAccess</tt> representation of this +    # parameter.      def to_unsafe_h -      @parameters.to_h +      @parameters.deep_dup      end      alias_method :to_unsafe_hash, :to_unsafe_h diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index c4228df925..2bd2e53252 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -30,9 +30,9 @@ module ActionDispatch            controller = controller req            res        = controller.make_response! req            dispatch(controller, params[:action], req, res) -        rescue NameError => e +        rescue ActionController::RoutingError            if @raise_on_name_error -            raise ActionController::RoutingError, e.message, e.backtrace +            raise            else              return [404, {'X-Cascade' => 'pass'}, []]            end @@ -42,6 +42,8 @@ module ActionDispatch          def controller(req)            req.controller_class +        rescue NameError => e +          raise ActionController::RoutingError, e.message, e.backtrace          end          def dispatch(controller, action, req, res) | 
