Module: Card::Set::Abstract::Attachment

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/web.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/coded.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/local.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/paths.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/storage_type.rb,
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/upload_cache.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/web.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/cloud.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/coded.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/local.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/paths.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/storage_type.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/upload_cache.rb

Overview

Set: Abstract (Attachment)

Defined Under Namespace

Modules: Cloud, Coded, Local, Paths, StorageType, UploadCache, Web

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Instance Attribute Details

#empty_ok=(value) ⇒ Object (writeonly)

Sets the attribute empty_ok

Parameters:

  • value

    the value to set the attribute empty_ok to.



8
9
10
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 8

def empty_ok=(value)
  @empty_ok = value
end

Class Method Details

.included(host_class) ⇒ Object



10
11
12
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 10

def self.included host_class
  host_class.extend CarrierWave::CardMount
end

.source_locationObject



7
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-carrierwave/set/abstract/attachment.rb"; end

Instance Method Details

#assign_set_specific_attributesObject



83
84
85
86
87
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 83

def assign_set_specific_attributes
  # reset content if we really have something to upload
  self.content = nil if set_specific[attachment_name.to_s].present?
  super
end

#attachment_before_actObject



71
72
73
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 71

def attachment_before_act
  send "#{attachment_name}_before_act"
end

#attachment_changed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 63

def attachment_changed?
  send "#{attachment_name}_changed?"
end

#attachment_format(ext) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 104

def attachment_format ext
  if ext.present? && attachment && (original_ext = attachment.extension.sub(/^\./, ""))
    if ["file", original_ext].member? ext
      original_ext
    elsif (exts = Mime::Types[attachment.content_type])
      if exts.find { |mt| mt.extensions.member? ext }
        ext
      else
        exts[0].extensions[0]
      end
    end
  end
rescue => e
  Rails.logger.info "attachment_format issue: #{e.message}"
  nil
end

#attachment_is_changing?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 67

def attachment_is_changing?
  send "#{attachment_name}_is_changing?"
end

#confirm_original_extension(ext) ⇒ Object



134
135
136
137
138
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment.rb', line 134

def confirm_original_extension ext
  return unless ["file", original_extension].member? ext

  original_extension
end

#create_versions?(_new_file) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 75

def create_versions? _new_file
  true
end

#delete_files_for_action(action) ⇒ Object



89
90
91
92
93
94
95
96
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 89

def delete_files_for_action action
  with_selected_action_id(action.id) do
    attachment.file.delete
    attachment.versions.each_value do |version|
      version.file.delete
    end
  end
end

#detect_extension(ext) ⇒ Object



124
125
126
127
128
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment.rb', line 124

def detect_extension ext
  return unless (mime_types = MIME::Types[attachment.content_type])

  recognized_extension?(mime_types, ext) ? ext : mime_types[0].extensions[0]
end

#empty_ok?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 79

def empty_ok?
  @empty_ok
end

#event: correct_identifierObject

we need a card id for the path so we have to update db_content when we have an id



20
21
22
23
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 20

event :correct_identifier, :finalize, on: :create, when: proc { |c| !c.web? } do
  update_column(:db_content, attachment.db_content)
  expire
end

#event: save_original_filenameObject



25
26
27
28
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 25

event :save_original_filename, :prepare_to_store, on: :save, when: :file_ready_to_save? do
  return unless @current_action
  @current_action.update! comment: original_filename
end

#event: select_file_revisionObject



14
15
16
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 14

event :select_file_revision, after: :select_action do
  attachment.retrieve_from_store!(attachment.identifier)
end

#event: validate_file_existObject



30
31
32
33
34
35
36
37
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 30

event :validate_file_exist, :validate, on: :create do
  return if empty_ok?
  if will_be_stored_as == :web
    errors.add "url is missing" if content.blank?
  elsif !attachment.file.present?
    errors.add attachment_name, "is missing"
  end
end

#event: write_identifierObject



39
40
41
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 39

event :write_identifier, after: :save_original_filename, when: proc { |c| !c.web? } do
  self.content = attachment.db_content
end

#file_ready_to_save?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 43

def file_ready_to_save?
  attachment.file.present? &&
    !preliminary_upload? &&
    !save_preliminary_upload? &&
    attachment_is_changing?
end

#item_names(_args = {}) ⇒ Object

needed for flexmail attachments. hacky.



50
51
52
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 50

def item_names _args={} # needed for flexmail attachments.  hacky.
  [name]
end

#original_extensionObject



140
141
142
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment.rb', line 140

def original_extension
  @original_extension ||= attachment&.extension&.sub(/^\./, "")
end

#original_filenameObject



54
55
56
57
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 54

def original_filename
  return content.split("/").last if web?
  attachment.original_filename
end

#recognized_extension?(mime_types, ext) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment.rb', line 130

def recognized_extension? mime_types, ext
  mime_types.find { |mt| mt.extensions.member? ext }
end

#rescuing_extension_issuesObject



117
118
119
120
121
122
# File 'card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment.rb', line 117

def rescuing_extension_issues
  yield
rescue StandardError => e
  Rails.logger.info "attachment_format issue: #{e.message}"
  nil
end

#revision(action, before_action = false) ⇒ Object



98
99
100
101
102
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 98

def revision action, before_action=false
  return unless (result = super)
  result[:empty_ok] = true
  result
end

#unfilled?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment.rb', line 59

def unfilled?
  !attachment.present? && !save_preliminary_upload? && !subcards? && blank_content?
end