Skip to content

Commit

Permalink
Add icon into category
Browse files Browse the repository at this point in the history
  • Loading branch information
AbubakarBhatti2022 committed Aug 11, 2023
1 parent 8eaf852 commit 7c805c2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Spree
module Admin
module Taxonomies
class AttachmentController < Spree::Admin::BaseController
def destroy
taxonomy = Spree::Taxonomy.find(params[:taxonomy_id])
taxon = taxonomy.taxons.find(params[:taxon_id])
if taxon.destroy_attachment(params[:attachment_definition])
flash[:success] = t('spree.successfully_removed', resource: params[:attachment_definition].titleize)
else
flash[:error] = t('spree.taxon_attachment_removal_error')
end
redirect_to edit_admin_taxonomy_taxon_path(taxonomy, taxon.id)
end
end
end
end
end
13 changes: 13 additions & 0 deletions core/app/models/spree/taxonomy/active_storage_attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Spree::Taxonomy::ActiveStorageAttachment
extend ActiveSupport::Concern
include Spree::ActiveStorageAdapter

included do
has_attachment :icon,
styles: Spree::Config.taxon_image_styles,
default_style: Spree::Config.taxon_image_style_default
validate :icon_is_an_image
end
end
30 changes: 30 additions & 0 deletions core/app/models/spree/taxonomy/paperclip_attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Spree::Taxonomy::PaperclipAttachment
extend ActiveSupport::Concern

included do
has_attached_file :icon,
styles: Spree::Config.taxon_image_styles,
default_style: Spree::Config.taxon_image_style_default,
url: '/spree/taxonomies/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/taxonomies/:id/:style/:basename.:extension',
default_url: '/assets/default_taxon.png'

validates_attachment :icon,
content_type: { content_type: Spree::Config.allowed_image_mime_types }
end

def icon_present?
icon.present?
end

def destroy_attachment(definition)
return false unless respond_to?(definition)

attached_file = send(definition)
return false unless attached_file.exists?

attached_file.destroy && save
end
end

0 comments on commit 7c805c2

Please sign in to comment.