diff --git a/app/controllers/collection_items_controller.rb b/app/controllers/collection_items_controller.rb index f3cc41724..00156750a 100644 --- a/app/controllers/collection_items_controller.rb +++ b/app/controllers/collection_items_controller.rb @@ -23,7 +23,7 @@ class CollectionItemsController < ApplicationController @collection_items.unreviewed_by_collection end elsif params[:user_id] && (@user = User.find_by(login: params[:user_id])) && @user == current_user - @collection_items = CollectionItem.for_user(@user).includes(:collection) + @collection_items = CollectionItem.for_user(@user).includes(:collection).merge(Collection.with_attached_icon) @collection_items = case params[:status] when "approved" @collection_items.approved_by_both diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 3edd902a2..e5f980b5e 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -25,11 +25,20 @@ class CollectionsController < ApplicationController def index if params[:work_id] && (@work = Work.find_by(id: params[:work_id])) - @collections = @work.approved_collections.by_title.includes(:parent, :moderators, :children, :collection_preference, owners: [:user]).paginate(page: params[:page]) + @collections = @work.approved_collections + .by_title + .for_blurb + .paginate(page: params[:page]) elsif params[:collection_id] && (@collection = Collection.find_by(name: params[:collection_id])) - @collections = @collection.children.by_title.includes(:parent, :moderators, :children, :collection_preference, owners: [:user]).paginate(page: params[:page]) + @collections = @collection.children + .by_title + .for_blurb + .paginate(page: params[:page]) elsif params[:user_id] && (@user = User.find_by(login: params[:user_id])) - @collections = @user.maintained_collections.by_title.includes(:parent, :moderators, :children, :collection_preference, owners: [:user]).paginate(page: params[:page]) + @collections = @user.maintained_collections + .by_title + .for_blurb + .paginate(page: params[:page]) @page_subtitle = ts("%{username} - Collections", username: @user.login) else if params[:user_id] diff --git a/app/helpers/collections_helper.rb b/app/helpers/collections_helper.rb index 59fcc53ef..b1561ee7a 100644 --- a/app/helpers/collections_helper.rb +++ b/app/helpers/collections_helper.rb @@ -123,4 +123,15 @@ module CollectionsHelper [t("#{key}.rejected"), :rejected] ] end + + def standard_icon_url(collection) + return "/images/skins/iconsets/default/icon_collection.png" unless collection.icon.attached? + url_for(collection.icon.variant(resize_to_limit: [100, 100], loader: { page: nil })) + end + + # Wraps the collection's standard_icon_url in an image tag + def collection_icon_display(collection) + image_tag(standard_icon_url(collection), class: "icon") + end + end diff --git a/app/models/collection.rb b/app/models/collection.rb index 1aefaf8c7..1545e62df 100755 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -3,8 +3,6 @@ class Collection < ApplicationRecord include UrlHelpers include WorksOwner - has_one_attached :icon - # has_attached_file :icon, # styles: { standard: "100x100>" }, # path: %w(staging unproduction).include?(Rails.env) ? ":rails_root/system/collections/icons/:id_partition/:style/:filename" : ":rails_root/public:url", @@ -14,6 +12,8 @@ class Collection < ApplicationRecord # bucket: %w(staging unproduction).include?(Rails.env) ? YAML.load_file("#{Rails.root}/config/s3.yml")['bucket'] : "", # default_url: "/images/skins/iconsets/default/icon_collection.png" + has_one_attached :icon + validates :icon, file_content_type: { allow: ["image/jpeg", "image/png", "image/gif"], if: -> { icon.attached? }, @@ -180,6 +180,7 @@ class Collection < ApplicationRecord scope :prompt_meme, -> { where(challenge_type: 'PromptMeme') } scope :name_only, -> { select("collections.name") } scope :by_title, -> { order(:title) } + scope :for_blurb, -> { includes(:parent, :moderators, :children, :collection_preference, owners: [:user]).with_attached_icon } before_validation :cleanup_url def cleanup_url @@ -417,17 +418,35 @@ class Collection < ApplicationRecord end end - # Delete current icon (thus reverting to archive default icon) def delete_icon=(value) @delete_icon = !value.to_i.zero? end def delete_icon - !!@delete_icon + !!@delete_icon end - alias_method :delete_icon?, :delete_icon + alias delete_icon? delete_icon def clear_icon - self.icon = nil if delete_icon? + return unless delete_icon? + + self.icon.purge + self.icon_alt_text = nil + self.icon_comment_text = nil end + + + # Delete current icon (thus reverting to archive default icon) + # def delete_icon=(value) + # @delete_icon = !value.to_i.zero? + # end + +# def delete_icon +# !!@delete_icon +# end +# alias_method :delete_icon?, :delete_icon +# +# def clear_icon +# self.icon = nil if delete_icon? +# end end diff --git a/app/views/collection_items/_item_fields.html.erb b/app/views/collection_items/_item_fields.html.erb index de0f79510..286838217 100755 --- a/app/views/collection_items/_item_fields.html.erb +++ b/app/views/collection_items/_item_fields.html.erb @@ -33,7 +33,7 @@

<%= collection_item.item_date.to_date %>

<% end %>
- <%= image_tag(collection_item.collection.icon.url(:standard), size: "100x100", alt: collection_item.collection.icon_alt_text, class: "icon", skip_pipeline: true) %> + <%= collection_icon_display(collection_item.collection) %>
diff --git a/app/views/collections/_collection_blurb.html.erb b/app/views/collections/_collection_blurb.html.erb index 862da4095..eabc8445a 100644 --- a/app/views/collections/_collection_blurb.html.erb +++ b/app/views/collections/_collection_blurb.html.erb @@ -12,11 +12,7 @@
- <% if collection.icon.attached? %> - <%= image_tag(collection.icon.variant(resize_to_limit: [100, 100])) %> - <% else %> - <%= image_tag('/images/skins/iconsets/default/icon_user.png', class: 'icon') %> - <% end %> + <%= collection_icon_display(collection) %>

<%= set_format_for_date(collection.updated_at) %>

<% if collection.all_moderators.length > 0%> diff --git a/app/views/collections/_form.html.erb b/app/views/collections/_form.html.erb index 7b3de25bc..79a50ef16 100644 --- a/app/views/collections/_form.html.erb +++ b/app/views/collections/_form.html.erb @@ -66,23 +66,17 @@ - <% if @collection.icon %> + <% if @collection.icon.attached? %> <%= collection_form.check_box :delete_icon, {:checked => false} %> <%= collection_form.label :delete_icon, ts("Delete collection icon and revert to our default") %> - <% else %> - <% link_to image_tag('/images/skins/iconsets/default/icon_user.png', class: 'icon') %> <% end %> diff --git a/app/views/collections/_header.html.erb b/app/views/collections/_header.html.erb index 0aca6ca78..087ffbdc2 100644 --- a/app/views/collections/_header.html.erb +++ b/app/views/collections/_header.html.erb @@ -2,11 +2,7 @@

<%= link_to_unless_current(@collection.title, @collection) %>

- <% if @collection.icon.attached? %> - <%= image_tag(@collection.icon.variant(resize_to_limit: [100, 100])) %> - <% else %> - <%= image_tag('/images/skins/iconsets/default/icon_user.png', class: 'icon') %> - <% end %> + <%= collection_icon_display(@collection) %>