lib/express_templates/components/capabilities/resourceful.rb in express_templates-0.9.4 vs lib/express_templates/components/capabilities/resourceful.rb in express_templates-0.9.5
- old
+ new
@@ -6,14 +6,17 @@
def self.included(base)
base.class_eval do
has_argument :id, "The name of the collection", type: :symbol, optional: false
has_option :collection, 'Provide an explicit collection as a resource.'
- has_option :collection_path, 'Provide an explicit path for the collection resource.'
+ has_option :collection_path, 'Provide an explicit path for the collection resource.', type: [:string, :proc]
has_option :resource_class, 'Overrides namespaced resource_class for using resources from a different module or namespace.'
- has_option :resource_path, 'Overides the resource path which is otherwise inferred from the class name.'
+ has_option :resource_path, 'Overides the resource path which is otherwise inferred from the class name.', type: [:string, :proc]
has_option :path_prefix, 'Rarely used. Override inferred path_prefix for path helpers.'
+ # note there is some duplication here.
+ # resource_path can be specified as a proc which can specify a namespace
+ # TODO: investigate which approach is better and deprecate if desirable
has_option :namespace, 'Rarely used. Overrides inferred namespace for resources.'
end
end
def namespace
@@ -121,34 +124,67 @@
end
end
def collection_path
if config[:collection_path]
- config[:collection_path]
+ if config[:collection_path].respond_to?(:call)
+ config[:collection_path].call()
+ else
+ config[:collection_path]
+ end
else
- #super
- helpers.instance_eval "#{collection_name_with_prefix}_path"
+ helpers.instance_eval collection_path_helper
end
end
+ def collection_path_helper
+ if path_namespace
+ "#{path_namespace}.#{collection_name_with_prefix}_path"
+ else
+ "#{collection_name_with_prefix}_path"
+ end
+ end
+
def collection_name_with_prefix
if path_prefix
"#{path_prefix}_#{collection_name}"
else
collection_name
end
end
def resource_path_helper
- "#{resource_name_with_path_prefix}_path"
+ if path_namespace
+ "#{path_namespace}.#{resource_name_with_path_prefix}_path"
+ else
+ "#{resource_name_with_path_prefix}_path"
+ end
end
- def resource_path(ivar=false)
+ def path_namespace
+ resource_class_name = resource_class.to_s
+ resource_class_name.match(/::/) ?
+ resource_class_name.split("::").first.try(:underscore) : nil
+ end
+
+ # accepts boolean to indicate whether to use an ivar or not
+ # and also may accept a resource on which we call to_param
+ def resource_path(ivar_or_resource = nil)
if config[:resource_path]
- config[:resource_path]
+ if config[:resource_path].respond_to?(:call) &&
+ ivar_or_resource.respond_to?(:to_param) &&
+ ![true, false].include?(ivar_or_resource)
+ config[:resource_path].call(ivar_or_resource)
+ else
+ config[:resource_path]
+ end
else
- # super
- helpers.instance_eval("#{resource_path_helper}(#{ivar ? '@' : ''}#{resource_name})")
+ if ivar_or_resource.respond_to?(:to_param) &&
+ ![true, false].include?(ivar_or_resource)
+ helpers.instance_eval("#{resource_path_helper}('#{ivar_or_resource.to_param}')")
+ else
+ helpers.instance_eval("#{resource_path_helper}(#{ivar_or_resource ? '@' : ''}#{resource_name})")
+ end
end
end
def resource_name_with_path_prefix
if path_prefix