I'm using Active Storage to upload files. I would like to not have to guess which tag to use to select the active storage object based on content_type. I've created a case statement to auto select in a view that workds, but in order to practice DRY I would like to break it out into a helper or method.
Original View Code:
<% case @board.image.content_type
when "image/png" %>
<%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?), :class => "img-fluid") %>
<% when "image/jpeg" %>
<%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?), :class => "img-fluid") %>
<% when "image/gif" %>
<%= image_tag((@board.image.variant(resize: @board.resolution) if @board.image.attached?), :class => "img-fluid") %>
<% when "video/mp4" %>
<%= video_tag(url_for(@board.image),controls: false, autoplay: true, muted: true, loop: true, class:"vid-fluid") %>
<% when "application/pdf" %>
<%= image_tag @board.image.blob.preview(resize: "1920x1080"), :class => "img-fluid" %>
<% when "application/msword" %>
<%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
<% when "application/vnd.openxmlformats-officedocument.wordprocessingml.document" %>
<%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
<% when "application/vnd.ms-excel" %>
<%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
<% when "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" %>
<%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
<% when "application/vnd.ms-powerpoint" %>
<%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
<% when "application/vnd.openxmlformats-officedocument.presentationml.presentation" %>
<%= image_tag @board.image.blob.preview(resize: @board.resolution), :class => "img-fluid" %>
<% else %>
<%= link_to 'Back', boards_path %>
<% end %>
Here is my attempt at creating a helper, but I have no idea how I would be able to pass the other arguments I need for the tags.
module BoardsHelper
def tag_selector(content_type)
<% case content_type
when "image/png" %>
<%= image_tag %>
<% when "image/jpeg" %>
<%= image_tag %>
<% when "image/gif" %>
<%= image_tag %>
<% when "video/mp4" %>
<%= video_tag %>
<% when "application/pdf" %>
<%= image_tag %>
<% when "application/msword" %>
<%= image_tag %>
<% when "application/vnd.openxmlformats-officedocument.wordprocessingml.document" %>
<%= image_tag %>
<% when "application/vnd.ms-excel" %>
<%= image_tag %>
<% when "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" %>
<%= image_tag %>
<% when "application/vnd.ms-powerpoint" %>
<%= image_tag %>
<% when "application/vnd.openxmlformats-officedocument.presentationml.presentation" %>
<%= image_tag %>
<% else %>
<%= image_tag %>
<% end %>
end
end
Why would you not just pass the board into your helper?
module BoardsHelper
def tag_selector(board, action_name)
size = action_name == 'index' ? '300x300' : board.resolution
case board.image.content_type
when "image/png", "image/jpeg", "image/gif"
image_tag((board.image.variant(resize: size) if board.image.attached?), class: "img-fluid")
....
end
end
end