2007-05-12 17:12:31 -04:00
|
|
|
module ActionController
|
|
|
|
module PolymorphicRoutes
|
2007-05-17 20:36:14 -04:00
|
|
|
def polymorphic_url(record_or_hash, options = {})
|
2007-05-12 17:12:31 -04:00
|
|
|
record = extract_record(record_or_hash)
|
|
|
|
|
|
|
|
case
|
|
|
|
when options[:action] == "new"
|
2007-05-17 20:36:14 -04:00
|
|
|
send(
|
2007-05-14 13:30:35 -04:00
|
|
|
action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options)
|
|
|
|
)
|
2007-05-12 17:12:31 -04:00
|
|
|
|
|
|
|
when record.respond_to?(:new_record?) && record.new_record?
|
2007-05-17 20:36:14 -04:00
|
|
|
send(
|
2007-05-14 13:30:35 -04:00
|
|
|
action_prefix(options) + RecordIdentifier.plural_class_name(record) + routing_type(options)
|
|
|
|
)
|
2007-05-12 17:12:31 -04:00
|
|
|
|
|
|
|
else
|
2007-05-17 20:36:14 -04:00
|
|
|
send(
|
2007-05-12 17:12:31 -04:00
|
|
|
action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options), record_or_hash
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-05-17 20:36:14 -04:00
|
|
|
def polymorphic_path(record_or_hash)
|
|
|
|
polymorphic_url(record_or_hash, :routing_type => :path)
|
2007-05-12 17:12:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
%w( edit new formatted ).each do |action|
|
2007-05-17 20:36:14 -04:00
|
|
|
module_eval <<-EOT, __FILE__, __LINE__
|
|
|
|
def #{action}_polymorphic_url(record_or_hash)
|
|
|
|
polymorphic_url(record_or_hash, :action => "#{action}")
|
2007-05-12 17:12:31 -04:00
|
|
|
end
|
|
|
|
|
2007-05-17 20:36:14 -04:00
|
|
|
def #{action}_polymorphic_path(record_or_hash)
|
|
|
|
polymorphic_url(record_or_hash, :action => "#{action}", :routing_type => :path)
|
2007-05-12 17:12:31 -04:00
|
|
|
end
|
|
|
|
EOT
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
def action_prefix(options)
|
|
|
|
options[:action] ? "#{options[:action]}_" : ""
|
|
|
|
end
|
2007-05-17 20:36:14 -04:00
|
|
|
|
2007-05-12 17:12:31 -04:00
|
|
|
def routing_type(options)
|
|
|
|
"_#{options[:routing_type] || "url"}"
|
|
|
|
end
|
2007-05-17 20:36:14 -04:00
|
|
|
|
2007-05-12 17:12:31 -04:00
|
|
|
def extract_record(record_or_hash)
|
|
|
|
record_or_hash.is_a?(Hash) ? record_or_hash[:id] : record_or_hash
|
|
|
|
end
|
|
|
|
end
|
2007-05-17 20:36:14 -04:00
|
|
|
end
|