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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

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

ElMassimo and others added 23 commits June 14, 2024 18:27
With this alone, the same Rails app could serve different Inertia apps,
setting a different version for each one in base controllers.
Also, rename it to `inertia_ssr_head`, which is a better description.
Shared data is now frozen to prevent

Also, match the method names for instance variables.

Updated a test that used to call `inertia_share` inside a `before_action`
as that seems like something a user should never do.
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