lib/active_admin/resource/routes.rb in activeadmin-0.6.0 vs lib/active_admin/resource/routes.rb in activeadmin-0.6.1
- old
+ new
@@ -1,46 +1,99 @@
module ActiveAdmin
class Resource
module Routes
+ # @params params [Hash] of params: {study_id: 3}
+ # @return [String] the path to this resource collection page
+ # @example "/admin/posts"
+ def route_collection_path(params = {})
+ RouteBuilder.new(self).collection_path(params)
+ end
+
+ # @param resource [ActiveRecord::Base] the instance we want the path of
+ # @return [String] the path to this resource collection page
+ # @example "/admin/posts/1"
+ def route_instance_path(resource)
+ RouteBuilder.new(self).instance_path(resource)
+ end
# Returns the routes prefix for this config
def route_prefix
namespace.module_name.try(:underscore)
end
def route_uncountable?
- controller.resources_configuration[:self][:route_collection_name] ==
- controller.resources_configuration[:self][:route_instance_name]
+ config = controller.resources_configuration[:self]
+
+ config[:route_collection_name] == config[:route_instance_name]
end
- # Returns a symbol for the route to use to get to the
- # collection of this resource
- def route_collection_path(params = {})
- route, required_params = [], []
+ private
- route << route_prefix
+ class RouteBuilder
+ def initialize(resource)
+ @resource = resource
+ end
- if belongs_to? && !belongs_to_config.optional?
- name = belongs_to_config.target.resource_name.singular
- route << name
- required_params << :"#{name}_id"
+ def collection_path(params)
+ route_name = route_name(
+ resource.controller.resources_configuration[:self][:route_collection_name],
+ (resource.route_uncountable? ? 'index_path' : 'path')
+ )
+
+ routes.send(route_name, *route_collection_params(params))
end
- route << controller.resources_configuration[:self][:route_collection_name]
+ # @return [String] the path to this resource collection page
+ # @param instance [ActiveRecord::Base] the instance we want the path of
+ # @example "/admin/posts/1"
+ def instance_path(instance)
+ route_name = route_name(resource.controller.resources_configuration[:self][:route_instance_name])
- route << (route_uncountable? ? 'index_path' : 'path')
+ routes.send(route_name, *route_instance_params(instance))
+ end
- route_name = route.compact.join("_").to_sym
+ private
- route_params = params.values_at(*required_params)
- routes.send(route_name, *route_params)
- end
+ attr_reader :resource
- private
+ def route_name(resource_path_name, suffix = 'path')
+ route = []
- def routes
- Rails.application.routes.url_helpers
- end
+ route << resource.route_prefix # "admin"
+ route << belongs_to_name if nested? # "category"
+ route << resource_path_name # "posts" or "post"
+ route << suffix # "path" or "index path"
+ route.compact.join('_').to_sym # :admin_category_posts_path
+ end
+
+
+ # @return params to pass to instance path
+ def route_instance_params(instance)
+ if nested?
+ [instance.send(belongs_to_name).to_param, instance.to_param]
+ else
+ instance.to_param
+ end
+ end
+
+ def route_collection_params(params)
+ if nested?
+ params[:"#{belongs_to_name}_id"]
+ end
+ end
+
+ def nested?
+ resource.belongs_to? && resource.belongs_to_config.required?
+ end
+
+ def belongs_to_name
+ resource.belongs_to_config.target.resource_name.singular if nested?
+ end
+
+ def routes
+ Rails.application.routes.url_helpers
+ end
+ end
end
end
end