Sha256: 41b5524aa86e827311fa737a17972146eb0ab554c3211cd31c536d4eb8f1178d

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

module Locomotive
  class ContentAsset

    include Locomotive::Mongoid::Document    

    ## extensions ##
    include Concerns::Asset::Types
    include Concerns::Asset::Vignette
    include Concerns::Asset::Checksum
    include ActionView::Helpers::NumberHelper

    ## fields ##
    field :content_type,  type: String
    field :width,         type: Integer
    field :height,        type: Integer
    field :size,          type: Integer
    field :position,      type: Integer, default: 0

    ## associations ##
    belongs_to :site, class_name: 'Locomotive::Site', validate: false, autosave: false

    ## validations ##
    validates_presence_of :source
    validate :maximum_file_size

    ## behaviours ##
    mount_uploader :source, ContentAssetUploader, mount_on: :source_filename

    ## scopes ##
    scope :ordered, -> { order_by(created_at: :desc) }
    scope :by_filename, ->(query) { where(source_filename: /.*#{query}.*/i) }
    scope :by_exact_filename, ->(filename) { where(source_filename: filename) }

    ## methods ##

    alias :name :source_filename

    def extname
      return nil unless self.source? && self.source_filename
      File.extname(self.source_filename).gsub(/^\./, '')
    end

    def as_json(options = nil)
      super.merge(
        thumbnail_url: self.big_vignette_url,
        size_to_human: number_to_human_size(self.size)
      )
    end

    def maximum_file_size
      errors.add(:source, :maximum_file_size_exceeded) if size_changed? && size > site.maximum_uploaded_file_size
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 app/models/locomotive/content_asset.rb
locomotivecms-4.2.0.alpha1 app/models/locomotive/content_asset.rb
locomotivecms-4.1.1 app/models/locomotive/content_asset.rb
locomotivecms-4.1.0 app/models/locomotive/content_asset.rb
locomotivecms-4.1.0.rc1 app/models/locomotive/content_asset.rb