Sha256: cf31c5371bb100cd1359dd57e8e6e602b346401ac7dc1cce2b5774c9b9dd7269

Contents?: true

Size: 912 Bytes

Versions: 4

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true

module Archangel
  ##
  # Asset model
  #
  class Asset < ApplicationRecord
    acts_as_paranoid

    mount_uploader :file, Archangel::AssetUploader

    before_save :save_asset_attributes

    validates :file, presence: true,
                     file_size: {
                       less_than_or_equal_to:
                         Archangel.config.asset_maximum_file_size
                     }
    validates :file_name, presence: true

    validate :valid_file_name

    belongs_to :site

    protected

    def save_asset_attributes
      return unless file.present? && file_changed?

      asset_object = file.file

      self.content_type = asset_object.content_type
      self.file_size = asset_object.size
    end

    def valid_file_name
      return if /^[\w-]+\.[A-Za-z]{2,}$/ =~ file_name

      errors.add(:file_name, Archangel.t(:file_name_invalid))
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
archangel-0.3.0 app/models/archangel/asset.rb
archangel-0.0.8 app/models/archangel/asset.rb
archangel-0.0.7 app/models/archangel/asset.rb
archangel-0.0.6 app/models/archangel/asset.rb