Module: ActiveLeonardo::Test
- Defined in:
- lib/generators/active_leonardo.rb
Overview
module Nested
protected
#Add leonardo namespace to class_path
#def class_path
# super + base_namespaces
#end
#product => products_path
#product under category => category_products_path(@category)
#product under brand/category => brand_category_products_path(@brand, @category)
def list_resources_path
"#{underscore_resource_path(:parent_singular_resource_plural)}_path(#{formatted_parent_resources("@")})"
end
#product under category => category_products_path(category)
#product under brand/category => brand_category_products_path(@brand, category)
#TODO: figure out how to build links for a particular resource in the path
def list_resources_path_back
return unless nested?
"#{underscore_resource_path(:parent_singular_resource_plural)}_path(#{formatted_parent_resources("@").reverse.sub(/@/, "").reverse})"
end
#product => "product"
#product under category => "[@category, product]"
#product under brand/category => "[@brand, @category, product]"
def destroy_resource_path(prefix_resource="")
formatted_resource_path("@", prefix_resource, "[]")
end
#product => "product"
#product under category => "[@category, product]"
#product under brand/category => "[@brand, @category, product]"
def show_resource_path(prefix_resource="")
formatted_resource_path("@", prefix_resource, "[]")
end
#product => "@product"
#product under category => "[@category, @product]"
#product under brand/category => "[@brand, @category, @product]"
def form_resource_path
formatted_resource_path("@", "@", "[]")
end
#product => new_product_path
#product under category => new_category_product_path(@category)
#product under brand/category => new_brand_category_product_path(@brand, @category)
def new_resource_path
"new_#{underscore_resource_path}_path(#{formatted_parent_resources("@")})"
end
#product => edit_product_path(@product)
#product under category => edit_category_product_path(@category, @product)
#product under brand/category => edit_brand_category_product_path(@brand, @category, @product)
def edit_resource_path(prefix_resource="")
"edit_#{underscore_resource_path}_path(#{formatted_resource_path("@", prefix_resource)})"
end
#product under brand/category => "[brand, category, product]" or "[@brand, @category, @product]" or "@brand, @category, @product" or [product.brand, product.category, product]
def formatted_resource_path(prefix_parent="", prefix_resource="", delimiter="", resource=nil)
formatted_resource_base resource_path(prefix_parent, prefix_resource, resource), delimiter
end
#product under brand/category => "[brand, category]" or "[@brand, @category]" or "@brand, @category" or product.brand, product.category
def formatted_parent_resources(prefix_parent="", delimiter="", resource=nil)
prefix_parent = "#{resource}." if resource
formatted_resource_base parent_resources(prefix_parent), delimiter
end
def formatted_resource_base(resources, delimiter="")
str_resources = resources.join(', ')
resources.size > 1 ? "#{delimiter[0..0]}#{str_resources}#{delimiter[1..1]}" : str_resources
end
#product under brand/category => "brand_category_product"
def underscore_resource_path(names=:all_singular)
case names
when :all_singular
resource_path.join('_')
#when :all_plural
#who needs?
when :parent_singular_resource_plural
resource_path.join('_').pluralize
else
"#{names.to_s}_not_supported"
end
end
#product under brand/category => ["brand", "category", "product"] or ["@brand", "@category", "@product"]
def resource_path(prefix_parent="", prefix_resource="", resource=nil, prefix_namespace="")
if resource
prefix_parent = "#{resource}."
else
resource = singular_table_name
end
prefix_namespace = ":" if prefix_namespace.empty? && prefix_parent.size>0
if nested?
(base_namespaces(prefix_namespace) + parent_resources(prefix_parent)) << "#{prefix_resource}#{resource}"
else
base_namespaces(prefix_namespace) << "#{prefix_resource}#{resource}"
end
end
#product under brand/category => "categories"
def plural_last_parent
plural_parent_resources.last
end
#product under brand/category => ["brands", "categories"] or ["@brands", "@categories"]
def plural_parent_resources(prefix_parent="")
base_parent_resources.map{|m| "#{prefix_parent}#{m.pluralize}"}
end
#product under brand/category => ["brand", "category"] or ["@brand", "@category"]
def parent_resources(prefix_parent="")
base_parent_resources.map{|m| "#{prefix_parent}#{m}"}
end
#product under brand/category => "category"
def last_parent
base_parent_resources.last
end
#product under brand/category => ["brand", "category"]
def base_parent_resources
return [] unless [:under].present?
[:under].split('/').map{|m| m.underscore}
end
def nested?
[:under].present?
end
### NAMESPACE ###
def leospaced?
[:leospace].present?
end
def base_namespaces(prefix="")
return [] unless [:leospace].present?
[:leospace].split('/').map{|m| "#{prefix}#{m.underscore}"}
end
def last_namespace(prefix="")
base_namespaces(prefix).last
end
def formatted_namespace_path(separator='/')
return "" unless leospaced?
"#{base_namespaces.join(separator)}#{separator}"
end
module Test
protected
#Add parent(s) param(s) to request
#get :index for a product under category => get :index, :category_id => product.category_id.to_s
def nested_params_http_request(value=nil)
return unless nested?
", " << base_parent_resources.map{|m| ":#{m}_id => #{value ? value.to_s.inspect : "#{file_name}.#{m}_id.to_s"}"}.join(', ')
end
#Create new parent(s) and add it to request
#get :index for a product under category => get :index, :category_id => Factory(:category).id.to_s
def nested_params_http_request_new_parent
return unless nested?
", " << base_parent_resources.map{|m| ":#{m}_id => Factory(:#{m}).id.to_s"}.join(', ')
end
#product => products_path
#product under category => category_products_path(product.category)
#product under brand/category => brand_category_products_path(product.brand, product.category)
def list_resources_path_test(resource=nil, prefix_parent=nil)
unless prefix_parent
resource ||= singular_table_name
prefix_parent = "#{resource}."
end
"#{underscore_resource_path(:parent_singular_resource_plural)}_path(#{formatted_parent_resources(prefix_parent, "", resource)})"
end
#product => "product"
#product under category => "[category, product]" or "[product.category, product]"
#product under brand/category => "[brand, category, product]" or "[product.brand, product.category, product]"
def show_resource_path_test(resource=nil, prefix_parent=nil, prefix_resource="")
resource ||= singular_table_name
prefix_parent = prefix_parent || "#{resource}."
formatted_resource_path(prefix_parent, prefix_resource, "[]", resource)
end
#product => new_product_path
#product under category => new_category_product_path(product.category)
#product under brand/category => new_brand_category_product_path(product.brand, product.category)
def new_resource_path_test(resource=nil, prefix_parent=nil)
resource ||= singular_table_name
prefix_parent = prefix_parent || "#{resource}."
"new_#{underscore_resource_path}_path(#{formatted_parent_resources(prefix_parent, "",resource)})"
end
end
end