Module: Pageflow::UploadedFile

Extended by:
ActiveSupport::Concern
Included in:
HostedFile
Defined in:
app/models/concerns/pageflow/uploaded_file.rb

Instance Method Summary collapse

Instance Method Details

#cache_keyObject



50
51
52
53
54
55
56
57
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 50

def cache_key
  # Ensure the cache key changes when the state changes. There are
  # cases during processing where the state is updated multiple
  # times in a single second. Since `cache_key` relies on
  # `updated_at`, which only is acurate to the second, we need to
  # prevent caching outdated information.
  "#{super}-#{state}"
end

#can_upload?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 59

def can_upload?
  # Overwritten in HostedFile based on initial state_machine-state.
  # Only true directly after creation.
  false
end

#direct_upload_configObject



65
66
67
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 65

def direct_upload_config
  Pageflow.config.paperclip_direct_upload_options.call(attachment)
end

#file_typeObject



46
47
48
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 46

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

#nested_files(model) ⇒ Object



38
39
40
41
42
43
44
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 38

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

#parent_allows_type_for_nestingObject



18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 18

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

#parent_belongs_to_same_entryObject



30
31
32
33
34
35
36
# File 'app/models/concerns/pageflow/uploaded_file.rb', line 30

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