ActiveAdmin.register BxBlockCatalogue::Catalogue, as: 'Catalogue' do
menu parent: "Catalogue Management"
actions :all, :except => [:new]
permit_params :category_id, :sub_category_id, :brand_id, :name, :sku, :description, :manufacture_date, :length, :breadth, :height, :availability, :stock_qty, :weight, :price, :recommended, :on_sale, :sale_price, :discount, :block_qty, :account_id, :product_number, catalogue_variants_attributes: [:id, :name, :description, :catalogue_variant_color_id, :catalogue_variant_size_id, :price, :stock_qty, :length, :breadth, :height, :block_qty, :sku, :_destroy]
scope("All Catalogues", default: true) do |scope|
scope.joins(:account)
end
scope("Seller Catalogues") do |scope|
scope.joins(:account).where(accounts: { role: "seller" })
end
scope("Stylist Catalogues") do |scope|
scope.joins(:account).where(accounts: { role: "stylist" })
end
filter :category, as: :searchable_select
filter :sub_category, as: :searchable_select
filter :name
filter :sku
filter :availability
filter :stock_qty
filter :product_number
filter :account, as: :searchable_select
filter :category_status, as: :select, collection: -> { BxBlockCategories::Category.all.pluck(:status).uniq.map { |status| [status.titleize, status] } }
filter :created_at
filter :updated_at
index do
selectable_column
id_column
column :category
column :sub_category
column :name
column :sku
column :description
column :availability
column :stock_qty
column :product_number
# column :account
column "Account" do |object|
if object.account.role == "seller"
link_to object&.account&.full_name, "/admin/sellers/#{object.account_id}"
elsif object.account.role == "stylist"
link_to object&.account&.full_name, "/admin/stylists/#{object.account_id}"
elsif object.account.role == "driver"
link_to object&.account&.full_name, "/admin/drivers/#{object.account_id}"
else
object.account
end
end
column "Catalogue Status" do |object|
object&.category&.status&.titleize
end
column :created_at
# column :updated_at
actions do |cat|
item "Variants", "/admin/catalogue_variants?q[catalogue_id_eq]=#{cat.id}&commit=Filter&order=id_desc", class: "member_link"
end
end
show do
attributes_table do
row :category
row :sub_category
row :name
row :sku
row :description
row :availability
row :stock_qty
row :product_number
row "Account" do |object|
if object.account.role == "seller"
link_to object&.account&.full_name, "/admin/sellers/#{object.account_id}"
elsif object.account.role == "stylist"
link_to object&.account&.full_name, "/admin/stylists/#{object.account_id}"
elsif object.account.role == "driver"
link_to object&.account&.full_name, "/admin/drivers/#{object.account_id}"
else
object.account
end
end
row :created_at
row :updated_at
row "Images" do
ul do
resource.images.each do |image|
span(style: 'margin-right: 15px;') do
image_tag(image, class: "catalogue-image", size: "100x100")
end
end
end
end
end
end
controller do
helper_method :subcategories_for_catalogue
def subcategories_for_catalogue(catalogue)
if catalogue.category.present?
BxBlockCategories::Category.find(catalogue.category_id).sub_categories.pluck(:name, :id)
else
[] # Return an empty array if no category is selected yet
end
end
def update
@catalogue = BxBlockCatalogue::Catalogue.find(params[:id])
if @catalogue.update(permitted_params[:catalogue])
catalogue_images(@catalogue, params)
else
render :edit
end
end
def catalogue_images(catalogue, params)
@catalogue = catalogue
if params[:catalogue][:remove_images].present?
params[:catalogue][:remove_images].reject(&:empty?).each do |image_id|
@catalogue.images.find(image_id).purge
end
end
@catalogue.images.attach(params[:catalogue][:images]) if params[:catalogue][:images]
# Remove selected images for catalogue variants
if params[:catalogue][:catalogue_variants_attributes]
params[:catalogue][:catalogue_variants_attributes].each do |_, variant_attributes|
cat_exist_image(@catalogue, variant_attributes)
end
end
# Attach new images for catalogue variants
if params[:catalogue][:catalogue_variants_attributes]
params[:catalogue][:catalogue_variants_attributes].each do |_, variant_attributes|
catalogue_variants_exist_image(@catalogue, variant_attributes)
end
end
redirect_to admin_catalogue_path(@catalogue), notice: "Catalogue updated successfully."
end
def cat_exist_image(catalogue, variant_attributes)
if variant_attributes[:remove_variant_images].present?
variant_attributes[:remove_variant_images].reject(&:empty?).each do |image_id|
catalogue.catalogue_variants.find(variant_attributes[:id]).images.find(image_id).purge
end
end
end
def catalogue_variants_exist_image(catalogue, variant_attributes)
if variant_attributes[:images].present?
check_catalogue_variant_new_record(catalogue, variant_attributes)
end
end
def check_catalogue_variant_new_record(catalogue, variant_attributes)
if variant_attributes[:id].present?
variant_id = variant_attributes[:id].to_i
catalogue.catalogue_variants.find(variant_id).images.attach(variant_attributes[:images])
else
new_variant = catalogue.catalogue_variants.create("catalogue_variant_color_id"=>variant_attributes[:catalogue_variant_color_id], "catalogue_variant_size_id"=>variant_attributes[:catalogue_variant_size_id], "price"=>variant_attributes[:price], "stock_qty"=>variant_attributes[:price], "length"=>variant_attributes[:length], "breadth"=>variant_attributes[:breadth], "height"=>variant_attributes[:height], "block_qty"=>variant_attributes[:block_qty], "sku"=>variant_attributes[:sku],)
new_variant.images.attach(variant_attributes[:images])
end
end
end
form do |f|
f.inputs "Catalogue Details" do
f.input :name
f.input :description
f.input :category_id, as: :searchable_select, collection: BxBlockCategories::Category.all.collect {|var| [var.name, var.id] }, include_blank: false
# f.input :sub_category_id, as: :searchable_select, collection: BxBlockCategories::SubCategory.all.collect {|var| [var.name, var.id] }, include_blank: false
# f.input :sub_category_id, as: :searchable_select, collection: []
f.input :sub_category_id, as: :searchable_select, collection: subcategories_for_catalogue(f.object), include_blank: false
f.input :sku
f.input :manufacture_date, as: :date_picker, input_html: { max: Date.current.strftime("%Y-%m-%d") }
f.input :availability
f.input :stock_qty
f.input :account_id, as: :searchable_select, collection: AccountBlock::Account.all.collect {|var| [var.full_name, var.id] }, include_blank: false
f.input :product_number
if f.object.images.present?
f.input :remove_images, as: :check_boxes, collection: f.object.images.map { |image| [image_tag(image, class: 'checkbox-image', style: 'width:50px;height:50px;'), image.id] }, input_html: { class: "custom-checkbox-label" }
end
f.input :images, as: :file, input_html: { multiple: true }, label: "Add New Images"
end
f.has_many :catalogue_variants, heading: 'Catalogue Variants', allow_destroy: true, new_record: 'Add Variant' do |variant_form|
variant_form.input :catalogue_variant_color_id, as: :searchable_select, collection: BxBlockCatalogue::CatalogueVariantColor.all.collect {|var| [var.name, var.id] }, include_blank: false
variant_form.input :catalogue_variant_size_id, as: :searchable_select, collection: BxBlockCatalogue::CatalogueVariantSize.all.collect {|var| [var.name, var.id] }, include_blank: false
variant_form.input :price
variant_form.input :stock_qty
variant_form.input :length
variant_form.input :breadth
variant_form.input :height
variant_form.input :block_qty
variant_form.input :sku
# hint = variant_form.object.images.map do |image|
# image_tag(image, style: 'width: 100px; height: 100px;')
# end.join('<br>').html_safe
if variant_form.object.images.present?
variant_form.input :remove_variant_images, as: :check_boxes, collection: variant_form.object.images.map { |image| [image_tag(image, class: 'checkbox-image', style: 'width:50px;height:50px;'), image.id] }, input_html: { class: 'custom-checkbox-label' }
end
variant_form.input :images, as: :file, input_html: { multiple: true }, label: "Add Variant Images"
# variant_form.input :images, as: :file, input_html: { multiple: true}, :hint => hint
end
f.actions
end
end