rails-activestoragelisted
Install: claude install-skill mickzijdel/rails-toolkit
# Rails ActiveStorage Patterns
## 1. Attachments Concern — Rich Text Introspection
Access all attachments from a model's rich text content and identify remote images/videos embedded in ActionText:
```ruby
# app/models/concerns/attachments.rb
module Attachments
extend ActiveSupport::Concern
# Variants used by ActionText embeds. Processed immediately on attachment to avoid
# read replica issues (lazy variants would attempt writes on read replicas).
#
# Patched into ActionText::RichText in config/initializers/action_text.rb
VARIANTS = {
# The `n: -1` loader option bypasses GIF-incompatible intent filtering in
# vipsthumbnail, preserving all frames in animated GIFs. Only `n` is accepted
# as an override; the full parameter name `intent` doesn't work.
small: { loader: { n: -1 }, resize_to_limit: [ 800, 600 ] },
large: { loader: { n: -1 }, resize_to_limit: [ 1024, 768 ] }
}
def attachments
rich_text_record&.embeds || []
end
def has_attachments?
attachments.any?
end
def remote_images
@remote_images ||= rich_text_record&.body&.attachables&.grep(ActionText::Attachables::RemoteImage) || []
end
def remote_videos
@remote_videos ||= rich_text_record&.body&.attachables&.grep(ActionText::Attachables::RemoteVideo) || []
end
private
def rich_text_record
@rich_text_record ||= begin
association = self.class.reflect_on_all_associations(:has_one).find { it.klass == ActionText::RichText }
public_