Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
= 5.1.0
* performance improvements across named route helpers, request introspection, wildcard loading,
form_for_resource, parameter filtering, and Specification initialization:
- defer Rails named route lookups in NamedRouteHelper until helper method generation, checking
route availability via respond_to? and replacing dynamic send calls with direct route calls
- extract NamedRouteHelper#extract_resource_url_options using du_opts.dup / in-place merge! and
forward arguments cleanly with `...` across NamedRouteHelper and Helper#method_missing
- scan RequestPathIntrospection path segments by index without array shifting, and simplify
segment_for_key / resource_params / load_wildcards_from loops to avoid temporary allocations
- avoid redundant #new_record? calls in Helper#form_for_resource_options and skip eager
pluralization / foreign_key inflection in Specification when options already supply them
* raise ResourceMismatch instead of NoMethodError when nesting segments are exhausted in
load_enclosing_resource_from_specification, and forward blocks/keywords via method_missing

= 5.0.3
* use native argument forwarding in resource service proxies to reduce temporary allocations,
with regression coverage for positional hashes, keywords, blocks, and custom finders
Expand Down
53 changes: 37 additions & 16 deletions lib/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,11 @@ def load_wildcard(as = nil)
segment = seg[:segment]
singleton = seg[:singleton]

if resource_specification_map[segment]
spec = resource_specification_map[segment]
spec = spec.dup.tap {|s| s.as = as} if as
if (spec = resource_specification_map[segment])
if as
spec = spec.dup
spec.as = as
end
else
spec = Specification.new(singleton ? segment : segment.singularize, :singleton => singleton, :as => as)
end
Expand All @@ -697,32 +699,51 @@ def load_wildcard(as = nil)
# and how many single wildcards are prior to that. What is left over from
# the current route enclosing names will be the number of wildcards we need to load
def load_wildcards_from(start)
specs = specifications.slice(start..-1)
encls = nesting_segments.slice(enclosing_resources.size..-1)
specs = specifications
segments = nesting_segments
encl_offset = enclosing_resources.size

spec_idx = nil
idx = start
while idx < specs.length
if specs[idx].is_a?(Specification)
spec_idx = idx
break
end
idx += 1
end

if spec = specs.find {|s| s.is_a?(Specification)}
spec_seg = encls.index {|e| e[:segment] == spec.segment && e[:singleton] == spec.singleton?} or ResourcesController.raise_resource_mismatch(self)
number_of_wildcards = spec_seg - (specs.index(spec) -1)
if spec_idx
spec = specs[spec_idx]
seg_idx = encl_offset
while seg_idx < segments.length
e = segments[seg_idx]
break if e[:segment] == spec.segment && e[:singleton] == spec.singleton?
seg_idx += 1
end
ResourcesController.raise_resource_mismatch(self) if seg_idx >= segments.length
number_of_wildcards = (seg_idx - encl_offset) - (spec_idx - start - 1)
else
number_of_wildcards = encls.length - (specs.length - 1)
number_of_wildcards = (segments.length - encl_offset) - (specs.length - start - 1)
end

number_of_wildcards.times { load_wildcard }
end

def load_enclosing_resource_from_specification(spec)
spec.segment == nesting_segments[enclosing_resources.size][:segment] or ResourcesController.raise_resource_mismatch(self)
spec.find_from(self).tap do |resource|
add_enclosing_resource(resource, :name => spec.name, :name_prefix => spec.name_prefix, :is_singleton => spec.singleton?, :as => spec.as)
end
seg = nesting_segments[enclosing_resources.size]
(seg && spec.segment == seg[:segment]) or ResourcesController.raise_resource_mismatch(self)
resource = spec.find_from(self)
add_enclosing_resource(resource, :name => spec.name, :name_prefix => spec.name_prefix, :is_singleton => spec.singleton?, :as => spec.as)
resource
end

def add_enclosing_resource(resource, options = {})
name = options[:name] || resource.class.name.underscore
update_name_prefix(options[:name_prefix] || (options[:name_prefix] == false ? '' : "#{name}_"))
enclosing_resources << resource
enclosing_collection_resources << resource unless options[:is_singleton]
instance_variable_set(:@enclosing_resource_name, options[:name])
@enclosing_resource_name = options[:name]
instance_variable_set(:"@#{name}", resource)
instance_variable_set(:"@#{options[:as]}", resource) if options[:as]
end
Expand Down Expand Up @@ -795,7 +816,7 @@ def service
end

class SingletonResourceService < ResourceService
def find(*args, **kwargs)
def find(...)
if resource_specification.find
resource_specification.find_custom(controller)
elsif controller.enclosing_resources.size > 0
Expand All @@ -814,7 +835,7 @@ def new(...)
end
end

def destroy(*args, **kwargs)
def destroy(...)
find.destroy
end

Expand Down
26 changes: 15 additions & 11 deletions lib/resources_controller/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def self.included(base)
# Calls form_for with the appropriate action and method for the resource
#
# resource.new_record? is used to decide between a create or update action
# (unless both :html[:method] and :url are provided)
#
# You can optionally pass a resource object, default is to use self.resource
#
Expand Down Expand Up @@ -80,12 +81,12 @@ def form_for_resource(*args, &block)

# Delegate named_route helper method to the controller. Create the delegation
# to short circuit the method_missing call for future invocations.
def method_missing(method, *args, &block)
def method_missing(method, ...)
if controller.resource_named_route_helper_method?(method)
self.class.send(:delegate, method, :to => :controller)
controller.send(method, *args)
controller.send(method, ...)
else
super(method, *args, &block)
super
end
end

Expand All @@ -96,15 +97,18 @@ def respond_to_missing?(method, include_private = false)

private
def form_for_resource_options(resource, resource_name, options)
options.dup.tap do |options|
options[:html] ||= {}
options[:html][:method] ||= resource.new_record? ? :post : :put
options[:as] = resource_name
args = options[:url_options] ? [options.delete(:url_options)] : []
options[:url] ||= if resource.new_record?
controller.resource_specification.singleton? ? resource_path(*args) : resources_path(*args)
options.dup.tap do |opts|
is_new = nil
opts[:html] ||= {}
opts[:html][:method] ||= (is_new = resource.new_record?) ? :post : :put
opts[:as] = resource_name
url_options = opts[:url_options] ? opts.delete(:url_options) : nil
opts[:url] ||= if controller.resource_specification.singleton?
url_options ? resource_path(url_options) : resource_path
elsif is_new.nil? ? resource.new_record? : is_new
url_options ? resources_path(url_options) : resources_path
else
controller.resource_specification.singleton? ? resource_path(*args) : resource_path(resource, *args)
url_options ? resource_path(resource, url_options) : resource_path(resource)
end
end
end
Expand Down
103 changes: 64 additions & 39 deletions lib/resources_controller/named_route_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ class CantMapRoute < ArgumentError #:nodoc:
# methods
module NamedRouteHelper

def method_missing(method, *args, &block)
def method_missing(method, ...)
# TODO: test that methods are only defined once
if resource_named_route_helper_method?(method, raise_error = true)
if resource_named_route_helper_method?(method, true)
define_resource_named_route_helper_method(method)
send(method, *args)
send(method, ...)
elsif resource_named_route_helper_method_for_name_prefix?(method)
define_resource_named_route_helper_method_for_name_prefix(method)
send(method, *args)
send(method, ...)
else
super(method, *args, &block)
super
end
end

Expand All @@ -52,15 +52,15 @@ def respond_to_missing?(method, include_private = false)
end

# return true if the passed method (e.g. 'resources_path') corresponds to a defined
# named route helper method
# named route helper method (checked via respond_to? without querying named_routes)
def resource_named_route_helper_method?(resource_method, raise_error = false)
method_str = resource_method.to_s
return false unless method_str.end_with?('_path', '_url')

if method_str.include?('enclosing_resource')
_, route_method = route_and_method_from_enclosing_resource_method_and_name_prefix(method_str, name_prefix)
route_method = route_method_from_enclosing_resource_method_and_name_prefix(method_str, name_prefix)
elsif method_str.include?('resource')
_, route_method = route_and_method_from_resource_method_and_name_prefix(method_str, name_prefix)
route_method = route_method_from_resource_method_and_name_prefix(method_str, name_prefix)
else
return false
end
Expand All @@ -84,31 +84,42 @@ def raise_resource_url_mapping_error(resource_method, route_method)
end_str
end

# passed something like (^|.*_)enclosing_resource(s)_.*(url|path)\z, will
# return the [route, route_method] for the expanded resource
def route_and_method_from_enclosing_resource_method_and_name_prefix(method, name_prefix)
if enclosing_resource
enclosing_route = name_prefix.delete_suffix('_')
method_str = method.to_s
is_plural = method_str.include?('enclosing_resources')
substitution = is_plural ? enclosing_route.pluralize : enclosing_route
route_method = method_str.sub(is_plural ? 'enclosing_resources' : 'enclosing_resource', substitution)
route_key = route_method.end_with?('_path') ? route_method.delete_suffix('_path') : route_method.delete_suffix('_url')
[Rails.application.routes.named_routes.get(route_key.to_sym), route_method]
else
def route_method_from_enclosing_resource_method_and_name_prefix(method, name_prefix)
unless enclosing_resource
raise NoMethodError, "Tried to map :#{method} but there is no enclosing_resource for this controller"
end

enclosing_route = name_prefix.delete_suffix('_')
method_str = method.to_s
is_plural = method_str.include?('enclosing_resources')
substitution = is_plural ? enclosing_route.pluralize : enclosing_route
method_str.sub(is_plural ? 'enclosing_resources' : 'enclosing_resource', substitution)
end
# passed something like (^|.*_)resource(s)_.*(url|path)\z, will

# passed something like (^|.*_)enclosing_resource(s)_.*(url|path)\z, will
# return the [route, route_method] for the expanded resource
def route_and_method_from_resource_method_and_name_prefix(method, name_prefix)
def route_and_method_from_enclosing_resource_method_and_name_prefix(method, name_prefix)
route_method = route_method_from_enclosing_resource_method_and_name_prefix(method, name_prefix)
[named_route_for_helper_method(route_method), route_method]
end

def route_method_from_resource_method_and_name_prefix(method, name_prefix)
method_str = method.to_s
is_resources = method_str.include?('resources_')
replacement = "#{name_prefix}#{is_resources ? resources_route_name : route_name}"
route_method = method_str.sub(is_resources ? 'resources' : 'resource', replacement)
route_key = route_method.end_with?('_path') ? route_method.delete_suffix('_path') : route_method.delete_suffix('_url')
[Rails.application.routes.named_routes.get(route_key.to_sym), route_method]
method_str.sub(is_resources ? 'resources' : 'resource', replacement)
end

# passed something like (^|.*_)resource(s)_.*(url|path)\z, will
# return the [route, route_method] for the expanded resource
def route_and_method_from_resource_method_and_name_prefix(method, name_prefix)
route_method = route_method_from_resource_method_and_name_prefix(method, name_prefix)
[named_route_for_helper_method(route_method), route_method]
end

def named_route_for_helper_method(route_method)
route_key = route_method.delete_suffix(route_method.end_with?('_path') ? '_path' : '_url')
Rails.application.routes.named_routes.get(route_key.to_sym)
end

# route_name is fixed for the life of the controller, and pluralize is the most
Expand All @@ -120,17 +131,33 @@ def resources_route_name
# defines a method that calls the appropriate named route method, with appropriate args.
def define_resource_named_route_helper_method(method)
self.class.send :module_eval, <<-end_eval, __FILE__, __LINE__
def #{method}(*args)
send "#{method}_for_\#{name_prefix}", *args
def #{method}(...)
if name_prefix.to_s.empty?
#{method}_for_(...)
else
send(:"#{method}_for_\#{name_prefix}", ...)
end
end
end_eval
end

def resource_named_route_helper_method_for_name_prefix?(method)
method_str = method.to_s
return false unless method_str.include?('_for_')
idx = method_str.index('_for_')
return false unless idx

resource_named_route_helper_method?(method_str.split('_for_', 2).first)
resource_named_route_helper_method?(method_str[0, idx])
end

def extract_resource_url_options(args)
du_opts = default_url_options
if args.last.is_a?(Hash) && args.last.extractable_options?
options = args.pop
options.merge!(du_opts) unless du_opts.empty?
options
elsif !du_opts.empty?
du_opts.dup
end
end

def define_resource_named_route_helper_method_for_name_prefix(method)
Expand All @@ -142,11 +169,10 @@ def define_resource_named_route_helper_method_for_name_prefix(method)

self.class.send :module_eval, <<-end_eval, __FILE__, __LINE__
def #{method}(*args)
options = args.extract_options!
options.merge!(default_url_options)
args = args.size < #{required_args} ? enclosing_collection_resources + args : enclosing_collection_resources - [enclosing_resource] + args
args << options unless options.empty?
send :#{route_method}, *args
options = extract_resource_url_options(args)
encl = args.size < #{required_args} ? enclosing_collection_resources : enclosing_collection_resources - [enclosing_resource]
args << options if options && !options.empty?
#{route_method}(*encl, *args)
end
end_eval

Expand All @@ -156,11 +182,10 @@ def #{method}(*args)

self.class.send :module_eval, <<-end_eval, __FILE__, __LINE__
def #{method}(*args)
options = args.extract_options!
options.merge!(default_url_options)
options = extract_resource_url_options(args)
#{"args.unshift(resource) if enclosing_collection_resources.size + args.size < #{required_args}" if required_args > 0}
args << options unless options.empty?
send :#{route_method}, *enclosing_collection_resources, *args
args << options if options && !options.empty?
#{route_method}(*enclosing_collection_resources, *args)
end
end_eval
end
Expand Down
Loading
Loading