Sha256: aee06da3ebcfa72786da7e71fe7020c0bbee089961f9399607ba2f19ff660f3e

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

module Pageflow
  module UploadedFile
    extend ActiveSupport::Concern

    included do
      belongs_to :uploader, class_name: 'User', optional: true
      belongs_to :entry, optional: true
      belongs_to :parent_file, polymorphic: true, foreign_type: :parent_file_model_type, optional: true

      has_many :usages, :as => :file, :class_name => 'Pageflow::FileUsage', :dependent => :destroy
      has_many :using_revisions, :through => :usages, :source => :revision
      has_many :using_entries, :through => :using_revisions, :source => :entry
      has_many :using_accounts, :through => :using_entries, :source => :account

      validate :parent_allows_type_for_nesting, :parent_belongs_to_same_entry
    end

    def parent_allows_type_for_nesting
      if parent_file.present?
        parent_class = parent_file.class
        file_type_of_parent = Pageflow.config.file_types.find_by_model!(parent_class)
        models_of_nested_file_types = file_type_of_parent.nested_file_types.map(&:model)
        unless models_of_nested_file_types.include?(self.class)
          errors[:base] << 'File type of provided parent file does not permit nesting files of '\
                           "type #{self.class.name}"
        end
      end
    end

    def parent_belongs_to_same_entry
      if parent_file.present?
        unless parent_file.using_entries.include?(entry)
          errors[:base] << 'Parent file does not belong to same entry as nested file'
        end
      end
    end

    def nested_files(model)
      model_table_name = model.table_name
      model
        .select("#{model_table_name}.*")
        .where("#{model_table_name}.parent_file_id = #{id} AND "\
               "#{model_table_name}.parent_file_model_type = '#{self.class.name}'")
    end

    def file_type
      Pageflow.config.file_types.find_by_model!(self.class)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pageflow-13.0.0.beta6 app/models/concerns/pageflow/uploaded_file.rb
pageflow-13.0.0.beta5 app/models/concerns/pageflow/uploaded_file.rb
pageflow-13.0.0.beta4 app/models/concerns/pageflow/uploaded_file.rb
pageflow-13.0.0.beta3 app/models/concerns/pageflow/uploaded_file.rb
pageflow-13.0.0.beta2 app/models/concerns/pageflow/uploaded_file.rb
pageflow-13.0.0.beta1 app/models/concerns/pageflow/uploaded_file.rb