Skip to content
This repository
Browse code

Check validity of options[:as] just once

  • Loading branch information...
commit 7a7ec74c1c32667e93f4bcd11d227d6134b0fa59 1 parent ac1a5d4
José Valim josevalim authored

Showing 1 changed file with 18 additions and 12 deletions. Show diff stats Hide diff stats

  1. +18 12 actionpack/lib/action_view/renderer/partial_renderer.rb
30 actionpack/lib/action_view/renderer/partial_renderer.rb
@@ -337,11 +337,16 @@ def setup(context, options, block)
337 337 end
338 338 end
339 339
  340 + if as = options[:as]
  341 + raise_invalid_identifier(as) unless as.to_s =~ /\A[a-z_]\w*\z/
  342 + as = as.to_sym
  343 + end
  344 +
340 345 if @path
341   - @variable, @variable_counter = retrieve_variable(@path)
  346 + @variable, @variable_counter = retrieve_variable(@path, as)
342 347 @template_keys = retrieve_template_keys
343 348 else
344   - paths.map! { |path| retrieve_variable(path).unshift(path) }
  349 + paths.map! { |path| retrieve_variable(path, as).unshift(path) }
345 350 end
346 351
347 352 self
@@ -450,21 +455,22 @@ def retrieve_template_keys
450 455 keys
451 456 end
452 457
453   - IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
454   - "make sure your partial name starts with a lowercase letter or underscore, " +
455   - "and is followed by any combination of letters, numbers and underscores."
456   -
457   - def retrieve_variable(path)
458   - variable = if as = @options[:as]
459   - raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path)) unless as.to_s =~ /\A[a-z_]\w*\z/
460   - as.to_sym
461   - else
  458 + def retrieve_variable(path, as)
  459 + variable = as || begin
462 460 base = path[-1] == "/" ? "" : File.basename(path)
463   - raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path)) unless base =~ /\A_?([a-z]\w*)(\.\w+)*\z/
  461 + raise_invalid_identifier(path) unless base =~ /\A_?([a-z]\w*)(\.\w+)*\z/
464 462 $1.to_sym
465 463 end
466 464 variable_counter = :"#{variable}_counter" if @collection
467 465 [variable, variable_counter]
468 466 end
  467 +
  468 + IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
  469 + "make sure your partial name starts with a lowercase letter or underscore, " +
  470 + "and is followed by any combination of letters, numbers and underscores."
  471 +
  472 + def raise_invalid_identifier(path)
  473 + raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path))
  474 + end
469 475 end
470 476 end

0 comments on commit 7a7ec74

Please sign in to comment.
Something went wrong with that request. Please try again.