Skip to content

Conversation

@devjoaov
Copy link

@devjoaov devjoaov commented Aug 19, 2025

Description 📖

This pull request adds support for customizing generated helper names in routes using a new js_helper_name parameter. This allows developers to override the automatic helper name generation and maintain consistent naming patterns across polymorphic controllers and complex route structures.

Background 📜

This was happening because the automatic helper name generation creates inconsistent names for polymorphic controllers. For example, with an admin_update_logs controller that handles both receipts and referrals, the generated helpers were:

export default {
  index: /* #__PURE__ */ definePathHelper('get', '/admin/v2/receipts/:receipt_id/admin_update_logs'),
  adminV2ReferralAdminUpdateLogs: /* #__PURE__ */ definePathHelper('get', '/admin/v2/referrals/:referral_id/admin_update_logs'),
}

The first route gets a generic index name while the second gets a verbose auto-generated name, creating inconsistency in the JavaScript API and making it harder to maintain a predictable naming pattern across related routes.

The Fix 🔨

By changing the generator to check for a js_helper_name parameter in route defaults before falling back to automatic name generation:

def helper
  js_helper_name = @route.defaults[:js_helper_name]
  return js_helper_name if js_helper_name.present?

  # existing automatic generation logic...
end

And allowing developers to specify custom names in their routes:

Rails.application.routes.draw do
  namespace :admin do
    namespace :v2 do
      resources :receipts do
        resources :admin_update_logs, only: [:index], js_helper_name: "adminV2ReceiptAdminUpdateLogs"
      end
      
      resources :referrals do
        resources :admin_update_logs, only: [:index], js_helper_name: "adminV2ReferralAdminUpdateLogs"
      end
    end
  end
end

This now generates consistent, descriptive helper names:

export default {
  adminV2ReceiptAdminUpdateLogs: definePathHelper('get', '/admin/v2/receipts/:receipt_id/admin_update_logs'),
  adminV2ReferralAdminUpdateLogs: definePathHelper('get', '/admin/v2/referrals/:referral_id/admin_update_logs'),
}

The fix maintains backward compatibility while giving developers full control over helper naming for complex route structures and polymorphic controllers.

@ElMassimo
Copy link
Owner

Hi there! I like the idea of being able to customize the route name explicitly, but I'd prefer if it was nested under the export option, as the features added in #50.

For example:

resources :admin_update_logs, only: [:index], export: {helper: "adminV2ReceiptAdminUpdateLogs"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants