Permalink
Browse files

avoid wildcard resolution if we specify enclosing_resources

  • Loading branch information...
Paolo Dona
Paolo Dona committed Jan 11, 2014
1 parent 9de5fb7 commit e0059bdf9cf138ce92c267993b4524eb3798748d
Showing with 14 additions and 12 deletions.
  1. +4 −2 lib/resources_controller.rb
  2. +10 −10 lib/resources_controller/request_path_introspection.rb
@@ -466,7 +466,7 @@ def resources_controller_for(name, options = {}, &block)
before_filter(:load_enclosing_resources, when_options.dup) unless load_enclosing_resources_filter_exists?

self.specifications = []
specifications << '*' unless options.delete(:load_enclosing) == false
specifications << '*' unless options.delete(:load_enclosing) == false || options[:in]

unless (actions = options.delete(:actions)) == false
actions ||= options[:singleton] ? ResourcesController.singleton_actions : ResourcesController.actions
@@ -536,7 +536,9 @@ def nested_in(*names, &block)

names.each do |name|
ensure_sane_wildcard if name == '*'
specifications << (name.to_s =~ /^(\*|\?(.*))$/ ? name.to_s : Specification.new(name, options, &block))
# try to see if there is already an existing specification with this name
specification = resource_specification_map.values.select {|s| s.name.to_s == name.to_s}.first
specifications << (name.to_s =~ /^(\*|\?(.*))$/ ? name.to_s : (specification || Specification.new(name, options, &block)))
end
end

@@ -8,16 +8,16 @@ module RequestPathIntrospection
def request_path
@request_path ||= params[:resource_path] || request.path
end

def nesting_request_path
@nesting_request_path ||= remove_namespace(remove_current_segment(request_path))
end

# returns an array of hashes like {:segment => 'forum', :singleton => false}
def nesting_segments
@nesting_segments ||= segments_for_path_and_keys(nesting_request_path, param_keys)
end

# returns an array of segments correspopnding to the namespace of the controller.
# If your controller is at a non standard location wrt it's path, you can modify this array in a before filter
# to help resources_controller do the right thing
@@ -28,11 +28,11 @@ def namespace_segments
end
@namespace_segments
end

def param_keys
params.keys.map(&:to_s).select{|k| k[-3..-1] == '_id'}
end

private
def remove_current_segment(path)
if respond_to?(:resource_specification) && resource_specification.singleton?
@@ -41,19 +41,19 @@ def remove_current_segment(path)
path.sub(%r(/#{current_segment}(?!.+/#{current_segment}).*$), '')
end
end

def current_segment
respond_to?(:resource_specification) ? resource_specification.segment : controller_name
end

def remove_namespace(path)
if namespace_segments.any?
path.sub(%r(^/#{namespace_segments.join('/')}), '')
else
path
end
end

def segments_for_path_and_keys(path, keys)
key_segments = keys.map{|k| segment_for_key(k)}
path_segments = path[1..-1].to_s.split('/')
@@ -69,7 +69,7 @@ def segments_for_path_and_keys(path, keys)
end
segments
end

def segment_for_key(key)
if respond_to?(:specifications) && spec = specifications.find{|s| s.respond_to?(:key) && s.key == key.to_s}
spec.segment
@@ -80,4 +80,4 @@ def segment_for_key(key)
end
end
end
end
end

0 comments on commit e0059bd

Please sign in to comment.