Migrate many image calls to Active Storage

This commit is contained in:
Eric 2020-05-15 14:01:59 -05:00
parent 057007e85e
commit a7cb3419cd
20 changed files with 48 additions and 32 deletions

View file

@ -6,16 +6,16 @@ class ImageAttachmentsController < ApplicationController
def create
# must return json with a link attr
# http://editor.froala.com/server-integrations/php-image-upload
@image = ImageAttachment.new(file: params[:file])
@image = ImageAttachment.new(clean_params_create)
if @image.save
render json: { link: @image.file_url }
render json: { link: url_for(@image.file) }
else
render json: @image.errors.full_messages, status: :unprocessable_entity
end
end
def remove
@image = ImageAttachment.select { |img| img.file_url == params[:src] }.first
@image = ImageAttachment.select { |img| url_for(img.file) == clean_params_remove[:src] }.first
if @image
@image.destroy
render json: @image
@ -23,4 +23,13 @@ class ImageAttachmentsController < ApplicationController
render json: {}, status: :unprocessable_entity
end
end
private
def clean_params_create
params.require(:file)
end
def clean_params_remove
params.require(:src)
end
end

View file

@ -28,7 +28,9 @@ class NonprofitsController < ApplicationController
@active_campaigns = campaigns.active
@any_past_campaigns = campaigns.past.any?
@nonprofit_background_image = FetchBackgroundImage.with_model(@nonprofit)
@nonprofit_background_image = @nonprofit.background_image.attached? ?
url_for(@nonprofit.background_image_by_size(:normal)) :
url_for(Image::DefaultNonprofitUrl)
respond_to do |format|
format.html

View file

@ -11,7 +11,7 @@
<div class='campaignMedia-video' if-branded='background, lightest'>
<%= render 'components/media/youtube_iframe', id: @campaign.youtube_video_id %>
</div>
<% elsif @campaign.main_image? %>
<img class='campaignMedia-image' src='<%= @campaign.main_image_url(:normal) %>'>
<% elsif @campaign.main_image.attached? %>
<%= image_tag @campaign.main_image_by_size(:normal), class: 'campaignMedia-image' %>
<% end %>
</div>

View file

@ -5,7 +5,9 @@
<% metric = QueryCampaignMetrics.on_donations(campaign.id) %>
<tr>
<td class='u-padding--0 u-width--200 u-hideIf--400'>
<img src='<%= campaign.main_image_url(:normal) %>'>
<% if campaign.main_image.attached?%>
<%= image_tag campaign.main_image_by_size(:normal) %>
<% end %>
</td>
<td class='u-padding--10'>
<% if campaign.end_datetime %>

View file

@ -67,7 +67,8 @@
<fieldset>
<label>Main Image <small>(at least 500x400)</small></label>
<p><small>Used for previews and social media</small></p>
<div class='image-upload u-inlineBlock' style='background-image: url("<%= @campaign.main_image_url(:thumb) %>");'>
<div class='image-upload u-inlineBlock' style='background-image: url("<%= @campaign.main_image.attached?
? url_for(@campaign.main_image_by_size(:thumb)) : '' %>");'>
<span><i class='fa fa-pencil'></i> Edit</span>
<input type='file' name='campaign[main_image]'>
</div>

View file

@ -20,7 +20,7 @@
app.is_parent_campaign = <%= @campaign.parent_campaign? %>
appl.def('has_video', <%= @campaign.video_url.present? %>)
appl.def('campaign_is_deleted', <%= @campaign.deleted || false %>)
appl.def('has_main_image', <%= @campaign.main_image.file.present? %>)
appl.def('has_main_image', <%= @campaign.main_image.attached? %>)
</script>
<%= render 'schema', campaign: @campaign, url: @url %>
@ -45,13 +45,13 @@
<%= content_for :facebook_tags do %>
<meta property="og:title" content="<%= raw @campaign.name %>">
<meta property="og:description" content="<%= @campaign.summary.present? ? raw(@campaign.summary) : raw(@campaign.name) %>">
<meta property="og:image" content="<%= @campaign.main_image_url(:normal) %>">
<meta property="og:image" content="<%= @campaign.main_image.attached? ? @campaign.main_image_by_size(:normal) : "" %>">
<% end %>
<%= content_for :twitter_tags do %>
<meta property="twitter:title" content="<%= raw @campaign.name %>">
<meta property="twitter:description" content="<%= raw @campaign.summary %>">
<meta property="twitter:image" content="<%= @campaign.main_image_url(:normal) %>">
<meta property="twitter:image" content="<%= @campaign.main_image.attached? ? @campaign.main_image_by_size(:normal) : "" %>">
<% end %>
<% if current_campaign_editor? %>

View file

@ -84,7 +84,7 @@
<fieldset class='u-marginTop--5'>
<label>Preview Image</label>
<p><small>Used for sharing on social media</small></p>
<div class='image-upload u-inlineBlock' style='background-image: url("<%= @event.main_image_url(:thumb) %>")'>
<div class='image-upload u-inlineBlock' style='background-image: url("<%= @event.main_image.attached? ? url_for(@event.main_image_by_size(:thumb)) : '' %>")'>
<span><i class='fa fa-pencil'></i> Edit</span>
<input type='file' name='event[main_image]'>
</div>

View file

@ -9,13 +9,13 @@
<%= content_for :facebook_tags do %>
<meta property="og:title" content="<%= raw @event.name %>" />
<meta property="og:description" content="<%= @event.summary.present? ? raw(@event.summary) : raw(@event.name) %>" />
<meta property="og:image" content="<%= @event.main_image_url(:normal) %>" />
<meta property="og:image" content="<%= @event.main_image.attached? ? @event.main_image_by_size(:normal) : "" %>" />
<% end %>
<%= content_for :twitter_tags do %>
<meta property="twitter:title" content="<%= raw @event.name %>" />
<meta property="twitter:description" content="<%= raw @event.summary %>" />
<meta property="twitter:image" content="<%= @event.main_image_url(:normal) %>" />
<meta property="twitter:image" content="<%= @event.main_image.attached? ? @event.main_image_by_size(:normal) : "" %>" />
<% end %>
<%= content_for :javascripts do %>

View file

@ -8,13 +8,13 @@
<%= content_for :facebook_tags do %>
<meta property="og:title" content="<%= raw @event.name %>" />
<meta property="og:description" content="<%= raw @event.summary %>" />
<meta property="og:image" content="<%= @event.main_image_url(:normal) %>" />
<meta property="og:image" content="<%= @event.main_image.attached? ? @event.main_image_by_size(:normal) : "" %>" />
<% end %>
<%= content_for :twitter_tags do %>
<meta property="twitter:title" content="<%= raw @event.name %>" />
<meta property="twitter:description" content="<%= raw @event.summary %>" />
<meta property="twitter:image" content="<%= @event.main_image_url(:normal) %>" />
<meta property="twitter:image" content="<%= @event.main_image.attached? ? @event.main_image_by_size(:normal) : "" %>" />
<% end %>
<%= content_for :javascripts do %>

View file

@ -2,7 +2,7 @@
<% if current_role?([:nonprofit_admin,:nonprofit_associate]) %>
<section class='sideNav-section'>
<a class='sideNav-link' href='<%= administered_nonprofit.url%>'>
<img class='sideNav-profile' src="<%= administered_nonprofit.logo_url(:small) %>">
<%= image_tag administered_nonprofit.logo_by_size(:small), class:"sideNav-profile" %>
<span class='sideNav-text'><%= administered_nonprofit.name %></span>
</a>

View file

@ -3,8 +3,8 @@
<section class='sideNav-section'>
<a class='sideNav-link' href='<%= profile_url(current_user.profile) %>'>
<% if current_user.profile.picture? %>
<img class='sideNav-profile' src="<%= current_user.profile.get_profile_picture(:tiny) %>">
<% if current_user.profile.picture.attached? %>
<%= image_tag current_user.profile.picture_by_size(:tiny), class: 'sideNav-profile' %>
<% else %>
<i class="sideNav-icon icon-user-1"></i>
<% end %>

View file

@ -98,7 +98,7 @@
<div class='layout--two'>
<fieldset>
<label class='u-marginBottom--10'>Logo</label>
<div class='image-upload u-margin--0' style='background-image:url("<%= @nonprofit.logo_url(:normal) %>")'>
<div class='image-upload u-margin--0' style='background-image:url("<%= @nonprofit.logo.attached? ? url_for(@nonprofit.logo_by_size(:normal)) : '' %>")'>
<span><i class='fa fa-image'></i> Upload</span>
<input type='file' name='nonprofit[logo]'>
</div>

View file

@ -3,7 +3,7 @@
<table>
<tr>
<td class='fundraisingHeader-content-sideElementTd'>
<div class='fundraisingHeader-content-logo' style="background-image:url('<%= @nonprofit.logo_url(:normal).to_s %>');">
<div class='fundraisingHeader-content-logo' style="background-image:url('<%= @nonprofit.logo.attached? ? url_for(@nonprofit.logo_by_size(:normal)) : "" %>');">
</div>
</td>
<td class='fundraisingHeader-content-textTd'>

View file

@ -1,11 +1,11 @@
<%- # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later -%>
<!-- partial start: nonprofits/_overview_media -->
<div class='overview-media'>
<% if @nonprofit.main_image.file %>
<% if @nonprofit.main_image.attached? %>
<div>
<div style='overflow: hidden; height: initial;'>
<%= image_tag @nonprofit.main_image_url(:nonprofit_carousel).to_s %>
<%= image_tag @nonprofit.main_image_by_size(:nonprofit_carousel) %>
</div>
</div>

View file

@ -23,7 +23,7 @@
</p>
<div class='u-inlineBlock'>
<div class='image-upload' style='background-image:url("<%= @nonprofit.main_image_url(:thumb_explore) %>")'>
<div class='image-upload' style='background-image:url("<%= @nonprofit.main_image.attached? ? url_for(@nonprofit.main_image_by_size(:thumb_explore)) : '' %>")'>
<span><i class='fa fa-image'></i> Select</span>
<input type='file' name='nonprofit[main_image]'>
</div>

View file

@ -33,13 +33,13 @@
<%= content_for :facebook_tags do %>
<meta property='og:title' content='I support <%= @nonprofit.name %>!'>
<meta property='og:description' content='<%= @nonprofit.summary %>'>
<meta property='og:image' content='<%= @nonprofit.main_image_url(:nonprofit_carousel) %>'>
<meta property='og:image' content='<%= @nonprofit.main_image.attached? ? @nonprofit.main_image_by_size(:nonprofit_carousel) : "" %>'>
<% end %>
<%= content_for :twitter_tags do %>
<meta property="twitter:title" content="I support <%= raw @nonprofit.name %>!">
<meta property="twitter:description" content="<%= raw @nonprofit.summary %>">
<meta property="twitter:image" content="<%= @nonprofit.main_image_url(:nonprofit_carousel) %>" />
<meta property="twitter:image" content="<%= @nonprofit.main_image.attached? ? @nonprofit.main_image_by_size(:nonprofit_carousel) : "" %>" />
<% end %>
<% content_for :body_id do %>donate<% end %>

View file

@ -28,9 +28,9 @@
<table class='table--blank' style='background: rgb(250, 250, 250)'>
<tr>
<% if @nonprofit.logo_url %>
<% if @nonprofit.logo.attached? %>
<td style='width: 100px; padding: 10px;'>
<img style='border: 1px solid rgba(0,0,0,0.05);' src="<%= @nonprofit.logo_url(:normal).to_s %>">
<%= image_tag @nonprofit.logo_by_size(:normal), style:'border: 1px solid rgba(0,0,0,0.05);'%>
</td>
<% end %>

View file

@ -7,12 +7,12 @@
<%= content_for :facebook_tags do %>
<meta property="og:title" content="<%= raw @nonprofit.name %>" />
<meta property="og:description" content="<%= raw @nonprofit.tagline %>" />
<meta property="og:image" content="<%= @nonprofit.main_image.attached? ? @nonprofit.main_image_by_size(:nonprofit_carousel) : nil %>" />
<meta property="og:image" content="<%= @nonprofit.main_image.attached? ? @nonprofit.main_image_by_size(:nonprofit_carousel) : "" %>" />
<% end %>
<%= content_for :twitter_tags do %>
<meta property="twitter:title" content="<%= raw @nonprofit.name %>" />
<meta property="twitter:description" content="<%= raw @nonprofit.tagline %>" />
<meta property="twitter:image" content="<%= @nonprofit.main_image.attached? ? @nonprofit.main_image_by_size(:nonprofit_carousel) : nil %>" />
<meta property="twitter:image" content="<%= @nonprofit.main_image.attached? ? @nonprofit.main_image_by_size(:nonprofit_carousel) : "" %>" />
<% end %>
<%= content_for :stylesheets do %>

View file

@ -16,7 +16,9 @@
<br>
<%= image_tag @nonprofit.logo_url(:normal).to_s %>
<% if @nonprofit.logo.attached? %>
<%= image_tag url_for(@nonprofit.logo_by_size(:normal)) %>
<% end %>
<% if params[:title] %>
<h4><%= params[:title] %></h4>
<% else %>

View file

@ -14,7 +14,7 @@
<table>
<tr>
<td>
<span class='donorProfileHeader-photo' style="background-image: url('<%= @profile.get_profile_picture(:normal) %>');"></span>
<span class='donorProfileHeader-photo' style="background-image: url('<%= @profile.picture.attached? && url_for(@profile.picture_by_size(:normal)) %>');"></span>
</td>
<td class='u-paddingLeft--15'>
<% supporter = (@profile.supporters && @profile.supporters.length != 0) ? supporter : false %>