Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ransack_allow_*): ransackable_* class method definition helpers #1546

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

casaper
Copy link

@casaper casaper commented Feb 15, 2025

Add helper methods for defining Ransack-allowed elements:

  • ransack_allow_attributes :column_a, :column_b
  • ransack_allow_associations :association_a, :association_b
  • ransack_allow_scopes :scope_a, :scope_b

These helpers provide a more convenient and cleaner way to define the ransackable_* class methods.
They use define_singleton_method internally, maintaining full compatibility with Ransack's existing functionality.


In a project, I implemented these methods on its ApplicationRecord class.
To me, it feels more readable when I define the allowed_attributes, allowed_associations and allowed_scopes in this way.

class MyModel < ApplicationRecord
  ransack_allow_attributes  :column_a, :column_b
  ransack_allow_associations :association_a, :association_b
  ransack_allow_scopes :scope_a, :scope_b
end

Ransack's codebase would not require any further changes for this, since it simply uses define_singleton_method to define the methods I would otherwise have created.

I reckon introducing these would require proper documentation.
No worries - I'm happy to help with that and do as much of the groundwork as I can.

However, first I want to find out if the maintainers think this idea has merit at all.
This PR is more of a trial balloon to gauge what the Ransack maintainers think of these methods.

Add helper methods for defining Ransack-allowed elements:

- `ransack_allow_attributes :column_a, :column_b`
- `ransack_allow_associations :association_a, :association_b`
- `ransack_allow_scopes :scope_a, :scope_b`

These helpers provide a more convenient and cleaner way to
define the ransackable_* class methods.
They use define_singleton_method internally, maintaining full
compatibility with Ransack's existing functionality.
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.

1 participant