mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
simplified route method name generation
This commit is contained in:
parent
7b9964c2ad
commit
b89dcde822
2 changed files with 19 additions and 27 deletions
|
@ -135,57 +135,47 @@ module ActionDispatch
|
||||||
|
|
||||||
record = record_list.pop
|
record = record_list.pop
|
||||||
|
|
||||||
inflection = :singular
|
inflection = lambda { |name| name.singular_route_key }
|
||||||
should_pop = true
|
should_pop = true
|
||||||
|
|
||||||
if record.try(:persisted?)
|
if record.try(:persisted?)
|
||||||
should_pop = false
|
should_pop = false
|
||||||
elsif options[:action] == 'new'
|
elsif options[:action] == 'new'
|
||||||
else
|
else
|
||||||
inflection = :plural
|
inflection = lambda { |name| name.route_key }
|
||||||
end
|
end
|
||||||
|
|
||||||
record = record.respond_to?(:to_model) ? record.to_model : record
|
args = []
|
||||||
|
|
||||||
#if options[:action] && options[:action].to_s == "new"
|
|
||||||
# inflection = :singular
|
|
||||||
#elsif (record.respond_to?(:persisted?) && !record.persisted?)
|
|
||||||
# inflection = :plural
|
|
||||||
#elsif record.is_a?(Class)
|
|
||||||
# inflection = :plural
|
|
||||||
#else
|
|
||||||
# args << record
|
|
||||||
# inflection = :singular
|
|
||||||
#end
|
|
||||||
|
|
||||||
route = record_list.map { |parent|
|
route = record_list.map { |parent|
|
||||||
if parent.is_a?(Symbol) || parent.is_a?(String)
|
case parent
|
||||||
|
when Symbol, String
|
||||||
parent.to_s
|
parent.to_s
|
||||||
|
when Class
|
||||||
|
args << parent
|
||||||
|
parent.model_name.singular_route_key
|
||||||
else
|
else
|
||||||
model_name_from_record_or_class(parent).singular_route_key
|
args << parent.to_model
|
||||||
|
parent.to_model.class.model_name.singular_route_key
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
route <<
|
route <<
|
||||||
if record.is_a?(Symbol) || record.is_a?(String)
|
case record
|
||||||
|
when Symbol, String
|
||||||
record.to_s
|
record.to_s
|
||||||
|
when Class
|
||||||
|
args << record unless should_pop
|
||||||
|
inflection.call record.model_name
|
||||||
else
|
else
|
||||||
if inflection == :singular
|
args << record.to_model unless should_pop
|
||||||
model_name_from_record_or_class(record).singular_route_key
|
inflection.call record.to_model.class.model_name
|
||||||
else
|
|
||||||
model_name_from_record_or_class(record).route_key
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
route << routing_type(options)
|
route << routing_type(options)
|
||||||
|
|
||||||
named_route = action_prefix(options) + route.join("_")
|
named_route = action_prefix(options) + route.join("_")
|
||||||
|
|
||||||
args.pop if should_pop
|
|
||||||
args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
|
|
||||||
|
|
||||||
args.collect! { |a| convert_to_model(a) }
|
|
||||||
|
|
||||||
recipient.send(named_route, *args, opts)
|
recipient.send(named_route, *args, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,8 @@ class PeopleHelperTest < ActionView::TestCase
|
||||||
with_test_route_set do
|
with_test_route_set do
|
||||||
person = Struct.new(:name) {
|
person = Struct.new(:name) {
|
||||||
extend ActiveModel::Naming
|
extend ActiveModel::Naming
|
||||||
|
def to_model; self; end
|
||||||
|
def persisted?; true; end
|
||||||
def self.name; 'Mocha::Mock'; end
|
def self.name; 'Mocha::Mock'; end
|
||||||
}.new "David"
|
}.new "David"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue