# rubocop:disable Style/Documentation module Behaveable module RouteExtractor # Generate url location. # # ==== Parameters # * behaveable - Behaveable object. # * resource - Resource object. (member routes). # # ==== Returns # * Route - Url location. def extract(behaveable = nil, resource = nil) resource_name = resource_name_from(params) behaveable_name = behaveable_name_from(behaveable) location_url = "api_#{resource_name}_url" return regular(location_url, resource) unless behaveable location_url = "api_#{behaveable_name}_#{resource_name}_url" nested(location_url, behaveable, resource) end private # Handle non-nested url location. # # ==== Parameters # * location_url - Url route as string. # * resource - Resource object. # # ==== Returns # * Route - Url location. def regular(location_url, resource) return send(location_url) unless resource send(location_url, resource) end # Handle nested url location. # # ==== Parameters # * location_url - Url route as string. # * behaveable - Behaveable object. # * resource - Resource object. # # ==== Returns # * Route - Url location. def nested(location_url, behaveable, resource) return send(location_url, behaveable) unless resource send(location_url, behaveable, resource) end # Get resource name from params. # # ==== Parameters # * params - ApplicationController's params. # # ==== Returns # * String - Resource name (singular or plural). def resource_name_from(params) inflection = params[:id].present? ? 'singular' : 'plural' params[:controller].split('/').last.send("#{inflection}ize") end # Get behaveable class name. # # ==== Parameters # * behaveable - Behaveable object. # # ==== Returns # * String - Behaveable class snake case name or nil. def behaveable_name_from(behaveable) return unless behaveable behaveable.class.name.underscore end end end # rubocop:enable Style/Documentation