class ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder
:nodoc:
def self.build(action, type)
def self.build(action, type) prefix = action ? "#{action}_" : "" suffix = type if action.to_s == 'new' HelperMethodBuilder.singular prefix, suffix else HelperMethodBuilder.plural prefix, suffix end end
def self.get(action, type)
def self.get(action, type) type = type.to_s CACHE[type].fetch(action) { build action, type } end
def self.path; CACHE['path'.freeze][nil]; end
def self.path; CACHE['path'.freeze][nil]; end
def self.plural(prefix, suffix)
def self.plural(prefix, suffix) new(->(name) { name.route_key }, prefix, suffix) end
def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options) builder = get action, type case record_or_hash_or_array when Array record_or_hash_or_array = record_or_hash_or_array.compact if record_or_hash_or_array.empty? raise ArgumentError, "Nil location provided. Can't build URI." end if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy) recipient = record_or_hash_or_array.shift end method, args = builder.handle_list record_or_hash_or_array when String, Symbol method, args = builder.handle_string record_or_hash_or_array when Class method, args = builder.handle_class record_or_hash_or_array when nil raise ArgumentError, "Nil location provided. Can't build URI." else method, args = builder.handle_model record_or_hash_or_array end if options.empty? recipient.send(method, *args) else recipient.send(method, *args, options) end end
def self.singular(prefix, suffix)
def self.singular(prefix, suffix) new(->(name) { name.singular_route_key }, prefix, suffix) end
def self.url; CACHE['url'.freeze][nil]; end
def self.url; CACHE['url'.freeze][nil]; end
def get_method_for_class(klass)
def get_method_for_class(klass) name = @key_strategy.call klass.model_name get_method_for_string name end
def get_method_for_string(str)
def get_method_for_string(str) "#{prefix}#{str}_#{suffix}" end
def handle_class(klass)
def handle_class(klass) [get_method_for_class(klass), []] end
def handle_class_call(target, klass)
def handle_class_call(target, klass) target.send get_method_for_class klass end
def handle_list(list)
def handle_list(list) record_list = list.dup record = record_list.pop args = [] route = record_list.map { |parent| case parent when Symbol, String parent.to_s when Class args << parent parent.model_name.singular_route_key else args << parent.to_model parent.to_model.model_name.singular_route_key end } route << case record when Symbol, String record.to_s when Class @key_strategy.call record.model_name else model = record.to_model if model.persisted? args << model model.model_name.singular_route_key else @key_strategy.call model.model_name end end route << suffix named_route = prefix + route.join("_") [named_route, args] end
def handle_model(record)
def handle_model(record) args = [] model = record.to_model named_route = if model.persisted? args << model get_method_for_string model.model_name.singular_route_key else get_method_for_class model end [named_route, args] end
def handle_model_call(target, model)
def handle_model_call(target, model) method, args = handle_model model target.send(method, *args) end
def handle_string(record)
def handle_string(record) [get_method_for_string(record), []] end
def handle_string_call(target, str)
def handle_string_call(target, str) target.send get_method_for_string str end
def initialize(key_strategy, prefix, suffix)
def initialize(key_strategy, prefix, suffix) @key_strategy = key_strategy @prefix = prefix @suffix = suffix end