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

Override Alchemy::Page.ransackable_scopes #2328

Merged
merged 1 commit into from
May 4, 2022

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented May 3, 2022

Ransack has a feature by which one can pass the name of a scope to a
ransack query as the key of a query param with a value of true, like so:

GET http://localhost.localdomain:3000/api/pages?q[published]=true

This feature can be used very nicely in the page select for an
Alchemy::Ingredients::Page to restrict that e.g. only public pages are
available:

[elements.yml]
- name: featured_page
  ingredients:
  - role: page
    type: Page
    settings:
      query_params:
        page_layout_start: "new_"
        published: true

However, for that to work, :published must be in the
Alchemy::Pages.ransackable_scopes array. By default this method
returns an empty array.
See https://github.com/activerecord-hackery/ransack/blob/be8c4642f927e8aa41204009c46c269158201cc7/lib/ransack/adapters/active_record/base.rb#L61-L68

for the origin of the method.

I've added a few more scopes that I think are fine to safelist here.

Checklist

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change

Ransack has a feature by which one can pass the name of a scope to a
ransack query as the key of a query param with a value of true, like so:

```
GET http://localhost.localdomain:3000/api/pages?q[published]=true
```

This feature can be used very nicely in the page select for an
`Alchemy::Ingredients::Page` to restrict that e.g. only public pages are
available:

```
[elements.yml]
- name: featured_page
  ingredients:
  - role: page
    type: Page
    settings:
      query_params:
        page_layout_start: "new_"
        published: true
```

However, for that to work, `:published` must be in the
`Alchemy::Pages.ransackable_scopes` array. By default this method
returns an empty array.
See https://github.com/activerecord-hackery/ransack/blob/be8c4642f927e8aa41204009c46c269158201cc7/lib/ransack/adapters/active_record/base.rb#L61-L68

for the origin of the method.

I've added a few more scopes that I think are fine to safelist here.
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks.

@tvdeyen tvdeyen merged commit 025e011 into AlchemyCMS:main May 4, 2022
tvdeyen added a commit that referenced this pull request May 6, 2022
Override Alchemy::Page.ransackable_scopes
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