Skip to content

Add asset save/loading#734

Open
mwtrew wants to merge 4 commits intomainfrom
1168-2_store-and-return-scratch-assets
Open

Add asset save/loading#734
mwtrew wants to merge 4 commits intomainfrom
1168-2_store-and-return-scratch-assets

Conversation

@mwtrew
Copy link
Contributor

@mwtrew mwtrew commented Mar 16, 2026

Status

Points for consideration:

  • See the ADR for performance and security implications.

What's changed?

  • Implements show and create for Scratch assets using ActiveStorage.
  • Note that authentication has not been implemented in this PR.

@cla-bot cla-bot bot added the cla-signed label Mar 16, 2026
@github-actions
Copy link

github-actions bot commented Mar 16, 2026

Test coverage

89.77% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/23153859513

@mwtrew mwtrew marked this pull request as ready for review March 16, 2026 16:05
Copilot AI review requested due to automatic review settings March 16, 2026 16:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Implements Scratch asset persistence and retrieval in the API using ActiveStorage, aligning with the referenced ADR for Scratch project storage and enabling save/load of uploaded assets.

Changes:

  • Added ScratchAsset model + DB table for storing asset metadata and attaching uploaded files via ActiveStorage.
  • Implemented Scratch asset show (redirect to blob) and create (store-once upload) endpoints and updated routes to include optional format.
  • Added request specs and a factory for creating Scratch assets with attachments; added middleware + gem to default content-type for binary uploads.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
spec/features/scratch/showing_a_scratch_asset_spec.rb Removes old placeholder “show SVG” request spec.
spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb Adds request specs covering asset create + show redirect behavior.
spec/factories/scratch_assets.rb Adds a scratch_asset factory with an attached fixture file.
db/schema.rb Includes new scratch_assets table in schema.
db/migrate/20260310161646_create_scratch_assets.rb Adds migration to create scratch_assets.
config/routes.rb Updates Scratch asset create route to accept .:format.
config/application.rb Inserts middleware to default POST content-type for scratch asset uploads.
app/models/scratch_asset.rb Introduces ScratchAsset model with attachment and validations.
app/controllers/api/scratch/assets_controller.rb Implements show redirect and create upload logic for assets.
Gemfile.lock Adds rack_content_type_default dependency resolution.
Gemfile Adds rack_content_type_default (and relocates flipper-ui).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

# frozen_string_literal: true

class ScratchAsset < ApplicationRecord
validates :filename, presence: true
Comment on lines 38 to 40
get '/assets/internalapi/asset/:id(.:format)/get/' => 'assets#show'
post '/assets/:id' => 'assets#create'
post '/assets/:id(.:format)' => 'assets#create'
end
Comment on lines +15 to +21
begin
filename_with_extension = "#{params[:id]}.#{params[:format]}"
ScratchAsset.find_or_create_by!(filename: filename_with_extension) do |a|
a.file.attach(io: request.body, filename: filename_with_extension)
end
rescue ActiveRecord::RecordNotUnique => e
logger.error(e)
def create
begin
filename_with_extension = "#{params[:id]}.#{params[:format]}"
ScratchAsset.find_or_create_by!(filename: filename_with_extension) do |a|
mwtrew and others added 2 commits March 16, 2026 16:13
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants