Topic: route helpers for nested catalog categories and items

I develop online shop and I need organize non standard routing scheme for catalog categories and catalog items.

For example,
index page of the category should have path:

example.com/catalog/category/subcategory_1/../subcategory_n/

and show page of the catalog item should have path:

example.com/catalog/category/subcategory_1/../subcategory_n/target-item

I have records in routes.rb:

get '/*catalog_path' => 'catalog_categories#show', constraints: CatalogCategoryRequestConstraint.new, as: :catalog_category
get '/*catalog_path' => 'catalog_items#show', as: :catalog_item

and all works fine, but I need use pretty named helpers, like

catalog_item_path(CatalogItem.first)
catalog_category_path(CatalogItem.first)

for generating proper nested urls.

For solving this problem I define helper methods in application controller

helper_method :catalog_item_path

def catalog_item_path(catalog_item)
  catalog_path = (catalog_item.categories_path_to_root + [catalog_item.slug]).join('/')
  super(catalog_path: catalog_path)
end

and I can access to this helpers in all views and controllers.

QUESTION:

Can I define / patch / override this helpers in Rails.application.routes.url_helpers ?

I need it for access to this helpers in several service classes and modules that do not have direct access to controller and view contexts. For example I have exporter class

class CatalogXmlExporter
  include Rails.application.routes.url_helpers
  
  def export(xml)
    # some code
    
    xml.url catalog_item_path(current_catalog_item)
    
    # some code
  end
end

and catalog_item_path not return right value here.

Sorry for my basic english skills and many thanks for your advice:)

Last edited by rainrb (2013-03-07 10:49:48)