Skip to content

Instantly share code, notes, and snippets.

@yshmarov
Created May 14, 2021 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yshmarov/f4515ebf631d3cc6f1720b0a91dbc7e7 to your computer and use it in GitHub Desktop.
Save yshmarov/f4515ebf631d3cc6f1720b0a91dbc7e7 to your computer and use it in GitHub Desktop.
blob active_Storage
app/views/active_storage/blobs/_blob.html.haml
<%= render "active_storage/blobs/blob", blob: @post.avatar %>
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% elsif blob.image? %>
<%= image_tag blob, width: "200px" %>
<% elsif blob.audio? %>
<audio controls>
<source src="<%= rails_blob_url(blob) %>" type="<%= blob.content_type %>"></source>
</audio>
<% elsif blob.video? %>
<video controls width="480">
<source src="<%= rails_blob_url(blob) %>" type="<%= blob.content_type %>"></source>
</video>
<% end %>
<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
@yshmarov
Copy link
Author

gem 'image_processing' # sudo apt install imagemagick
= image_tag course.avatar.variant(resize_to_limit: [100, 100])
@blob.variant(resize: '250x250').processed.service_url(disposition: params[:disposition])

@yshmarov
Copy link
Author

<% @post.images.each do |image| %>
  <% if image.image? %>
    <%= image_tag image, width: "200px" %>
  <% elsif image.video? %>
    <video controls width="320" height="240">
      <source src="<%= rails_blob_url(image) %>" type="<%= image.content_type %>"></source>
    </video>
  <% elsif image.audio? %>
    <audio controls>
      <source src="<%= rails_blob_url(image) %>" type="<%= image.content_type %>"></source>
    </audio>
  <% else %>
    <%= link_to image.filename, rails_blob_path(image, disposition: "attachment") %>
  <% end %>
<% end %>

@yshmarov
Copy link
Author

yshmarov commented May 14, 2021

delete attached

  def purge_avatar
    @post = Post.find(params[:id])
    @post.avatar.purge_later
    redirect_back fallback_location: root_path, notice: "success"
  end
  resources :posts do
    member do
      delete :purge_avatar
    end
  end
    <%= link_to "Delete avatar", purge_avatar_post_path(@post), method: :delete %>

@yshmarov
Copy link
Author

yshmarov commented May 14, 2021

  delete "attachments/:id/purge", to: "posts#purge_attachment", as: 'purge_attachment'
  def purge_attachment
    attachment = ActiveStorage::Attachment.find(params[:id])
    attachment.purge_later
    redirect_back fallback_location: root_path, notice: "success"
  end
    <% @post.attachments.each do |attachment| %>
      <%= link_to "Delete item", purge_attachment_path(attachment), method: :delete %>

@yshmarov
Copy link
Author

yshmarov commented May 15, 2021

Action Mailer

class UserMailer < ApplicationMailer

def user_created
    @greeting = Post.count
    @user = params[:user]
    @post = params[:post]
    attachments.inline['favicon2.png'] = File.read('app/assets/images/favicon2.png')
    mail(
	to: email_address_with_name(User.first.email, User.first.email), 
	cc: User.all.pluck(:email),
	bcc: "secret@secret.com", 
	subject: "new post created!"
	)
    end
end
<%= image_tag attachments['favicon2.png'].url, width: 100, alt: 'Corsego' %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment