Sha256: cea2af3ded7b49ce56c7d304300b65bc7b0012899774184963b023cfdc1a7408

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

module Workarea
  module Catalog
    class ProductImage
      include ApplicationDocument
      extend Dragonfly::Model

      field :option, type: String
      field :position, type: Integer
      field :image_name, type: String
      field :image_uid, type: String

      #
      # The values for the following fields are automatically populated via
      # Dragonfly built-in analysers. The values can be accessed by the "magic
      # attributes" that are dynamically added to the model.
      #
      # More info:
      # - http://markevans.github.io/dragonfly/imagemagick/#analysers
      # - http://markevans.github.io/dragonfly/models/#magic-attributes
      #
      field :image_width, type: Integer
      field :image_height, type: Integer
      field :image_aspect_ratio, type: Float
      field :image_portrait, type: Boolean
      field :image_landscape, type: Boolean
      field :image_format, type: String
      field :image_image, type: Boolean
      field :image_inverse_aspect_ratio, type: Float

      embedded_in :product,
        class_name: 'Workarea::Catalog::Product',
        inverse_of: :images,
        touch: true

      dragonfly_accessor :image, app: :workarea

      after_save { _parent.touch }
      after_destroy { _parent.touch }

      default_scope -> { order_by(position: :asc, updated_at: :desc) }

      def valid?(*)
        self.position ||= _parent.images.length - 1
        super
      end

      def placeholder?
        false
      end

      def respond_to_missing?(method_name, include_private = false)
        super || image.respond_to?(method_name)
      end

      def method_missing(sym, *args, &block)
        image.send(sym, *args, &block) if image.respond_to?(sym)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-core-3.4.18 app/models/workarea/catalog/product_image.rb
workarea-core-3.4.17 app/models/workarea/catalog/product_image.rb