Skip to content

Instantly share code, notes, and snippets.

View yoones's full-sized avatar
🔥
Con sentimiento y manana

Younes Serraj yoones

🔥
Con sentimiento y manana
View GitHub Profile
@yoones
yoones / commit_bcf370d689673031073ba2ac5588afe41cc315c9.patch
Created May 18, 2019 08:05
commit bcf370d689673031073ba2ac5588afe41cc315c9
commit bcf370d689673031073ba2ac5588afe41cc315c9
Author: Younes SERRAJ <[email protected]>
Date: Sun Apr 21 19:07:22 2019 +0200
Allow ActiveStorage to generate variants of BMP images
diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md
index 54fc949172..d524ecf7d6 100644
--- a/activestorage/CHANGELOG.md
+++ b/activestorage/CHANGELOG.md
@yoones
yoones / commit_78260d5663702f153f6bae64073f9659de366946.patch
Created May 18, 2019 08:02
commit 78260d5663702f153f6bae64073f9659de366946
commit 78260d5663702f153f6bae64073f9659de366946
Author: Younes SERRAJ <[email protected]>
Date: Wed Apr 17 19:25:46 2019 +0200
Mention more ActiveStorage hooks in Active Support Instrumentation guide [ci skip]
Hooks added:
- `service_download_chunk.active_storage`
- `service_update_metadata.active_storage`
- `preview.active_storage`
# Model
class Profile < ApplicationRecord
has_one_attached :avatar
accepts_nested_attributes_for :avatar_attachment, allow_destroy: true
end
# Controller
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
some_profile.avatar.analyze
some_profile.avatar.attached?
@yoones
yoones / permit_destruction.rb
Created March 13, 2019 11:05
permit_destruction.rb
params.require(:customer).permit(:some_attribute, contracts_attachments_attributes: [:id, :_destroy])
# Assuming a model defined like so:
class Post < ApplicationRecord
has_one_attached :image
end
# ...you can join against :image_attachment to select posts having attached images:
Post.joins(:image_attachment).where('published_at >= ?', Time.now)
class MyModel < ActiveRecord::Base
has_one_attached :image
validate :image_size
private
def image_size
errors.add :image, 'too big' if image.blob.byte_size > 4096
end
end
some_profile.avatar.attach(io: File.open('/path/to/file'), filename: 'avatar.png')
<%= image_tag(record.attachment.representation(resize: '500x500') %>