Comments (17)
Ok i figured out part 1.
The maglev migrations / active storage do not work with uuids
For clarity, ive modified all the maglev migrations copied over to use uuids. This works fine now, uploads are fixed.
It might be worth looking at the migrations and changing to use the orm id generator types like active storage does.
from maglev-core.
ActiveStorage handles this well i think, if you want to take a look
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[7.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types
create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :key ], unique: true
end
create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type
if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end
t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false
t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
from maglev-core.
@did i really do appreciate you digging into this, hopefully things will continue to work well for me atleast in the meantime. Maglev seems awesome so far.
from maglev-core.
But still failing to serve the uploaded images so digging into that.
from maglev-core.
Ok well i had to patch Maglev::Asset to make this work, but theres probably a better way, would love some help.
Maglev::Asset.class_eval do
def self.find(friendly_name)
# Extract uuid from friendly_name using a regex
uuid = friendly_name.match(/(?<uuid>[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})/)['uuid']
super(uuid)
end
end
from maglev-core.
hey @j-mcnally. sorry for being late in the game. Let me read everything :-)
from maglev-core.
No worried @did appreciate you taking a peek for me. I have an unideal working solution, but feels like im hacking too much of the Maglev::Asset model, not really clear how its "supposed to work".
from maglev-core.
The failing error on asset load was
Started GET "/maglev/assets/17841ed1-bb76-4a79-bb56-90e95238da5b-fabian-centeno-uy60pjuhqoo-unsplash-1.jpg" for ::1 at 2023-12-08 11:14:34 -0500
Processing by Maglev::AssetsController#show as JPEG
Parameters: {"id"=>"17841ed1-bb76-4a79-bb56-90e95238da5b-fabian-centeno-uy60pjuhqoo-unsplash-1"}
Maglev::Asset Load (0.8ms) SELECT "maglev_assets".* FROM "maglev_assets" WHERE "maglev_assets"."id" = $1 LIMIT $2 [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 4ms (ActiveRecord: 0.8ms | Allocations: 1704)
ActiveRecord::RecordNotFound (Couldn't find Maglev::Asset with 'id'=17841ed1-bb76-4a79-bb56-90e95238da5b-fabian-centeno-uy60pjuhqoo-unsplash-1):
from maglev-core.
Thanks for the clear explanation. I'm confused because Rails is supposed to take care of "friendly" urls by parsing ids (either integer based or UUID base). Hmm..
And writing a spec for this specific use case, it's not an easy task...
from maglev-core.
its likely some edge case with the "Friendly part" and "UUIDs" i noticed that if i pass anything other than a UUID to find, it auto nullls it out, likely something rails added for safety with UUIDs without considering how normal id lookups work. So unfortunately this feels like an upstream bug.
from maglev-core.
I never really knew it worked this way. But look at this http://tutorials.jumpstartlab.com/topics/controllers/friendly-urls.html it appears the friendly thing works because of the way .to_i works
Loading development environment (Rails 7.1.2)
3.2.2 :001 > "1-abc-123.jpg".to_i
=> 1
3.2.2 :002 >
which explains why this works ints and not uuids if the find call is having the slug have .to_i called on it.
My hottest take would be to use something other than find for the this one specific lookup use case and then allow it to be more safely overwritten with other id implementations.
from maglev-core.
This article explains how UUIDs or non valid uuids are converted to nil
https://pawelurbanek.com/uuid-order-rails
from maglev-core.
I knew about the to_i
trick regarding friendly urls. And I also read Pawel's article!
Actually, we need to 2 things to make Maglev support uuids.
- the installation procedure must have an option to install the migrations with uuids as the primary keys of the Maglev models.
- handle uuids in the Maglev friendly urls (https://www.fastruby.io/blog/ruby/uuid/friendlier-uuid-urls-in-ruby.html)
from maglev-core.
in the meantime, I'm going to play with a new Maglev site and uuids.
from maglev-core.
@j-mcnally you're very welcome and thanks for taking the time to find a solution!
from maglev-core.
hey @j-mcnally!
Thanks to this PR #72 (not yet merged), I successfully managed to create a Maglev site within an application which uses UUIDs.
Let me know if you see any issues or if you've comments.
from maglev-core.
this is great, the comprehensive support across models for uuids will be a win win i think for users.
from maglev-core.
Related Issues (20)
- Alt text form field is empty after reload
- Issue with Vite arising in Avo integration directions HOT 11
- Can you please update this `vite-plugin-ruby ` version to 3.2.2 ?
- Create versions of predefined pages in other locales
- Maglev PRO Version HOT 1
- Internal Error: maglev@workspace:.: This package doesn't seem to be present in your lockfile HOT 3
- Running Installer - Vite Compatibility Issue HOT 5
- Homepage video broken HOT 1
- Can't have two sections with same name in different categories
- Rails 7.1 support HOT 4
- Headless progress HOT 2
- Page Search by path incorrectly searches locale
- Install fails on first step of guide because of vite incompatibility HOT 6
- docs.maglev.dev/guides/i18n does not exist HOT 1
- Switching language while preserving site location HOT 1
- Fatal error: Integer does not have #dig method HOT 1
- How to modify frontend editor files HOT 1
- Options to add submenus not available HOT 1
- Installation error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from maglev-core.