Files
openfoodnetwork/app/models/enterprise_group.rb
Prikesh Savla f4d59305d7 Upgraded gem active_storage_validations from 1.1.2 to 3.0.2 and fixed any upgrade related issues
Changed all references of processable_image to processable_file which was a breaking change from v1 to v2 https://github.com/igorkasyanchuk/active_storage_validations/tree/3.0.2?tab=readme-ov-file#upgrading-from-1x-to-2x

Also it upgraded the way of validating files from just the file name and content type, so tests also needed to change for file upload checks

Refactored all the similar file image validator content type in Spree::Image::ACCEPTED_CONTENT_TYPES and Updated ImageBuilder.import method to use the url.path when getting filename.
2025-12-08 22:12:01 +05:30

91 lines
2.8 KiB
Ruby

# frozen_string_literal: true
require 'open_food_network/locking'
class EnterpriseGroup < ApplicationRecord
include PermalinkGenerator
acts_as_list
has_and_belongs_to_many :enterprises, join_table: 'enterprise_groups_enterprises'
belongs_to :owner, class_name: 'Spree::User', inverse_of: :owned_groups, optional: true
belongs_to :address, class_name: 'Spree::Address'
accepts_nested_attributes_for :address
validates :address, associated: true
before_validation :set_undefined_address_fields
before_validation :set_unused_address_fields
before_validation :sanitize_permalink
after_save :unset_undefined_address_fields
after_find :unset_undefined_address_fields
validates :name, presence: true
validates :description, presence: true
validates :permalink, uniqueness: true, presence: true
delegate :phone, :address1, :address2, :city, :zipcode, :state, :country, to: :address
has_one_attached :logo, service: image_service
has_one_attached :promo_image, service: image_service
validates :logo,
processable_file: true,
content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES
validates :promo_image,
processable_file: true,
content_type: ::Spree::Image::ACCEPTED_CONTENT_TYPES
scope :by_position, -> { order('position ASC') }
scope :on_front_page, -> { where(on_front_page: true) }
scope :managed_by, lambda { |user|
if user.admin?
where(nil)
else
where(owner_id: user.id)
end
}
def set_unused_address_fields
return if address.blank?
address.firstname = address.lastname = address.company = I18n.t(:unused)
end
def set_undefined_address_fields
return if address.blank?
address.phone.present? || address.phone = I18n.t(:undefined)
address.address1.present? || address.address1 = I18n.t(:undefined)
address.city.present? || address.city = I18n.t(:undefined)
address.state.present? || address.state = address.country.states.first
address.zipcode.present? || address.zipcode = I18n.t(:undefined)
end
def unset_undefined_address_fields
return if address.blank?
address.phone = address.phone.sub(/^#{I18n.t(:undefined)}$/, '')
address.address1 = address.address1.sub(/^#{I18n.t(:undefined)}$/, '')
address.city = address.city.sub(/^#{I18n.t(:undefined)}$/, '')
address.zipcode = address.zipcode.sub(/^#{I18n.t(:undefined)}$/, '')
end
def to_param
permalink
end
# Remove any unsupported HTML.
def long_description=(html)
super(HtmlSanitizer.sanitize_and_enforce_link_target_blank(html))
end
private
def sanitize_permalink
return unless permalink.blank? || permalink_changed?
requested = permalink.presence || permalink_was.presence || name.presence || 'group'
self.permalink = create_unique_permalink(requested.parameterize)
end
end