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

before_action like filters for inertia_share #137

Merged

Conversation

skryukov
Copy link
Contributor

This PR builds on the approach from #121 and aims to resolve the concerns raised in #115 (comment).

It introduces some complexity as we need to partially reimplement Rails callbacks to work with @ElMassimo's shared properties feature.

@bknoles, I'd like to get your opinion on this approach. Do you think it's worth it to continue refining this PR?

Supported options:

inertia_share normal_shared_prop: 1

inertia_share(normal_shared_prop: 1)

inertia_share do
  {conditionally_shared_show_prop: 1} if action_name == "show"
end

inertia_share only: :edit do
  {only_block_prop: 1}
end

inertia_share except: [:show, :index] do
  {except_block_prop: 1}
end

inertia_share if: -> { is_edit? } do
  {if_proc_prop: 1}
end

inertia_share unless: -> { !is_edit? } do
  {unless_proc_prop: 1}
end

inertia_share({only_prop: 1}, only: :edit)

inertia_share({if_prop: 1}, if: [:is_edit?, -> { true }])

inertia_share({unless_prop: 1}, unless: :not_edit?)

inertia_share({only_if_prop: 1}, only: :edit, if: -> { true })

inertia_share({except_if_prop: 1}, except: [:index, :show], if: -> { true })

Caveat:

inertia_share normal_shared_prop: 1, only: :edit
# => raises "You must not mix shared data and [:if, :unless, :only, :except] options, pass data as a hash or a block.
inertia_share(normal_shared_prop: 1, only: :edit)
# => raises "You must not mix shared data and [:if, :unless, :only, :except] options, pass data as a hash or a block.
inertia_share({normal_shared_prop: 1, only: :edit})
# => adds only: :edit as a prop
inertia_share({normal_shared_prop: 1, only: :edit}, only: :edit)
# => adds only: :edit as a prop only for :edit action

@skryukov skryukov force-pushed the inertia-share-before-action-like-filters branch from 4742060 to b998284 Compare October 21, 2024 18:13
@skryukov skryukov mentioned this pull request Oct 22, 2024
@skryukov skryukov force-pushed the inertia-share-before-action-like-filters branch from 10d11e8 to 4e1babe Compare October 28, 2024 18:30
@skryukov skryukov force-pushed the inertia-share-before-action-like-filters branch from 4e1babe to 2309b58 Compare October 28, 2024 18:32
@bknoles
Copy link
Collaborator

bknoles commented Oct 29, 2024

I love the API. Especially the clear and helpful exceptions if you try write something that could lead to keyword collision.

Last night when I looked at this, I worried about this as well:

we need to partially reimplement Rails callbacks

I.e. would it be confusing for someone new if the API to kinda sorts resembles before_action, but actually does something different?

However, after thinking about it, I think keys like if/only/except/unless are generic enough that they don't "belong" to callbacks. The pattern here feels very much like a Rails pattern, rather than a Rails callbacks pattern.

I'm in favor of adding. I would use this in existing apps.

@BrandonShar can you strawman this? Are there any other potential downsides you can think of that we should consider?

Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

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

I like it! I think this is a nice "papercut" style improvement that feels very in line with existing expectations for rails users.

Just two minor comments I'd prefer be addressed before we merge.

lib/inertia_rails/controller.rb Show resolved Hide resolved
lib/inertia_rails/controller.rb Outdated Show resolved Hide resolved
spec/inertia/conditional_sharing_spec.rb Outdated Show resolved Hide resolved
Copy link
Collaborator

@BrandonShar BrandonShar left a comment

Choose a reason for hiding this comment

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

Thanks for the back and forth!

@bknoles bknoles merged commit be142a2 into inertiajs:master Nov 2, 2024
12 checks passed
@skryukov skryukov deleted the inertia-share-before-action-like-filters branch November 3, 2024 05:41
@bknoles bknoles mentioned this pull request Nov 4, 2024
4 tasks
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.

3 participants