Sha256: 9cf8c6774b43215843d644cd9399ee91a8cf45f968cc42c343d9a10bb222ea0a

Contents?: true

Size: 1.9 KB

Versions: 14

Compression:

Stored size: 1.9 KB

Contents

require_dependency 'landable/asset_uploader'

require 'carrierwave/orm/activerecord'
require 'digest/md5'

module Landable
  class Asset < ActiveRecord::Base
    include Landable::TableName
    include Landable::Librarian
    
    mount_uploader :data, Landable::AssetUploader
    alias :file :data
    alias :file= :data=

    # This bit of indirection allows us to generate predictable
    # URLs in the test environment.
    def self.url_generator
      @url_generator ||= proc { |asset| asset.data.try(:url) }
    end

    belongs_to :author
    has_and_belongs_to_many :pages, join_table: Page.assets_join_table_name
    has_and_belongs_to_many :page_revisions, join_table: PageRevision.assets_join_table_name
    has_and_belongs_to_many :themes, join_table: Theme.assets_join_table_name

    before_validation :write_metadata, on: :create

    validates_presence_of     :data, :author
    validates_presence_of     :name, :mime_type, :md5sum, :file_size
    validates_uniqueness_of   :md5sum
    validates_numericality_of :file_size, only_integer: true
    validates_format_of       :name, :with => /^[\w\._-]+$/, :on => :create, :multiline => true, :message => 'can only contain alphanumeric characters, periods, underscores, and dashes'

    def public_url
      self.class.url_generator.call(self)
    end

    def duplicate_of
      return unless data.present?
      self.class.where(md5sum: calculate_md5sum).first
    end

    def associated_pages
      paths = []
      Page.where("body like ?", "%#{self.name}%").each do |page|
        paths.push(page.path)
      end
      paths
    end

    private

    def calculate_md5sum
      Digest::MD5.hexdigest(data.read) if data.present?
    end

    def write_metadata
      return unless data.present?
      self.md5sum    = calculate_md5sum
      self.mime_type = data.file.content_type.presence || 'application/octet-stream'
      self.file_size = data.file.size
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
landable-1.13.1 app/models/landable/asset.rb
landable-1.12.3 app/models/landable/asset.rb
landable-1.12.2 app/models/landable/asset.rb
landable-1.12.1 app/models/landable/asset.rb
landable-1.11.1 app/models/landable/asset.rb
landable-1.11.0 app/models/landable/asset.rb
landable-1.10.0.rc2 app/models/landable/asset.rb
landable-1.10.0.rc1 app/models/landable/asset.rb
landable-1.9.2 app/models/landable/asset.rb
landable-1.9.1 app/models/landable/asset.rb
landable-1.9.0 app/models/landable/asset.rb
landable-1.9.0.rc2 app/models/landable/asset.rb
landable-1.9.0.rc1 app/models/landable/asset.rb
landable-1.8.0 app/models/landable/asset.rb