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

Support cross slice assets #1372

Merged
merged 4 commits into from Feb 11, 2024
Merged

Support cross slice assets #1372

merged 4 commits into from Feb 11, 2024

Conversation

timriley
Copy link
Member

@timriley timriley commented Jan 26, 2024

Update asset helpers to accept Hanami::Assets::Asset instances and use the URL from those, instead of trying to look up the asset from the assets in the view context.

See spec/integration/assets/cross_slice_assets_helpers_spec.rb (added in this PR) for a full demonstration, but in short, using assets from across slices involves:

(1) Importing the assets object from one slice to another:

module Admin
  class Slice < Hanami::Slice
    import keys: ["assets"], from: Hanami.app.container, as: :app
  end
end

(2) Making the imported assets component available in the view context:

module Admin
  module Views
    class Context < Hanami::View::Context
      include Deps[app_assets: "app.assets"]
    end
  end
end

(3) Using the asset objects directly with the assets helpers:

<%= javascript_tag(app_assets["app.js"]) %>

The recommended way to use assets is in full isolation, which is why I don't think we need to further streamline the steps above. But doing this shows how it is possible and should allow people to achieve cross-slice asset usage if their situation requires it.

@timriley timriley self-assigned this Jan 26, 2024
@timriley timriley changed the base branch from main to per-slice-assets-provider January 26, 2024 03:57
Base automatically changed from per-slice-assets-provider to main February 11, 2024 09:45
@timriley timriley marked this pull request as ready for review February 11, 2024 12:11
@timriley timriley added this to the v2.1.0 milestone Feb 11, 2024
@timriley timriley merged commit 35285bd into main Feb 11, 2024
6 checks passed
@timriley timriley deleted the support-cross-slice-assets branch February 11, 2024 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant