Sha256: 9dadfa061c6a1d3d3cefde4c7aea1b5a759826a5a96b8541d4b92b2f3ed973f6
Contents?: true
Size: 1.3 KB
Versions: 24
Compression:
Stored size: 1.3 KB
Contents
# frozen_string_literal: true module PagesCore module PageModel module Images extend ActiveSupport::Concern included do belongs_to_image :image, optional: true belongs_to_image :meta_image, class_name: "Image", optional: true has_many :page_images, -> { order("position") }, inverse_of: :page, dependent: :destroy has_many( :images, -> { where(page_images: { primary: false }).order("position") }, through: :page_images ) after_save :update_primary_image accepts_nested_attributes_for :page_images, reject_if: proc { |a| a["image_id"].blank? }, allow_destroy: true end def image? image_id? end def image super.try { |i| i.localize(locale) } end def images super.in_locale(locale) end def page_images super.in_locale(locale) end private def update_primary_image new_id = page_images.find_by(primary: true)&.image_id update(image_id: new_id) if new_id != image_id end end end end
Version data entries
24 entries across 24 versions & 1 rubygems