Skip to content

Commit

Permalink
Micro optimization for build_named_route_call in PolymorphicRoutes:
Browse files Browse the repository at this point in the history
1. use map instead of inject
2. use [].join("_") instead of '<<'. It is a little bit faster for ruby 1.9.2 and x2 faster for ruby 1.8.7. http://gist.github.com/548143

[#5450 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
yury authored and josevalim committed Aug 28, 2010
1 parent cc5a9c6 commit 331234e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
Expand Up @@ -148,29 +148,29 @@ def routing_type(options)
def build_named_route_call(records, inflection, options = {}) def build_named_route_call(records, inflection, options = {})
unless records.is_a?(Array) unless records.is_a?(Array)
record = extract_record(records) record = extract_record(records)
route = '' route = []
else else
record = records.pop record = records.pop
route = records.inject("") do |string, parent| route = records.map do |parent|
if parent.is_a?(Symbol) || parent.is_a?(String) if parent.is_a?(Symbol) || parent.is_a?(String)
string << "#{parent}_" parent
else else
string << ActiveModel::Naming.plural(parent).singularize ActiveModel::Naming.plural(parent).singularize
string << "_"
end end
end end
end end


if record.is_a?(Symbol) || record.is_a?(String) if record.is_a?(Symbol) || record.is_a?(String)
route << "#{record}_" route << record
else else
route << ActiveModel::Naming.plural(record) route << ActiveModel::Naming.plural(record)
route = route.singularize if inflection == :singular route = [route.join("_").singularize] if inflection == :singular
route << "_" route << "index" if ActiveModel::Naming.uncountable?(record) && inflection == :plural
route << "index_" if ActiveModel::Naming.uncountable?(record) && inflection == :plural
end end


action_prefix(options) + route + routing_type(options).to_s route << routing_type(options)

action_prefix(options) + route.join("_")
end end


def extract_record(record_or_hash_or_array) def extract_record(record_or_hash_or_array)
Expand Down

0 comments on commit 331234e

Please sign in to comment.