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

Extended by:
Card::Set
Defined in:
tmpsets/set/mod013-carrierwave/abstract/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #pattern_and_modules_from_path, #process_base_modules, #register_set, #tmp_file_frame, #tmp_file_template, #tmp_file_wrapped_content, #write_tmp_file

Methods included from Helpers

#abstract_set?, #all_set?, #shortname

Methods included from Card::Set::AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#all_set_format_mod!, #define_on_format, #format, #register_set_format, #view

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer

Methods included from Event

#event

Instance Attribute Details

#action_id_of_cached_uploadObject

Returns the value of attribute action_id_of_cached_upload.



144
145
146
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 144

def action_id_of_cached_upload
  @action_id_of_cached_upload
end

#empty_ok=(value) ⇒ Object (writeonly)

Sets the attribute empty_ok

Parameters:

  • value

    the value to set the attribute empty_ok to.



146
147
148
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 146

def empty_ok=(value)
  @empty_ok = value
end

Class Method Details

.included(host_class) ⇒ Object



6
7
8
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 6

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

Instance Method Details

#assign_attachment(file, original_filename) ⇒ Object



55
56
57
58
59
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 55

def assign_attachment file, original_filename
  send "#{attachment_name}=", file
  write_identifier
  @current_action.update_attributes! comment: original_filename
end

#assign_set_specific_attributesObject



216
217
218
219
220
221
222
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 216

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

#attachment_changed?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 128

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

#attachment_format(ext) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 263

def attachment_format ext
  if ext.present? && attachment && (original_ext = attachment.extension)
    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

#clear_upload_cache_dir_for_new_cardsObject



224
225
226
227
228
229
230
231
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 224

def clear_upload_cache_dir_for_new_cards
  Dir.entries(tmp_upload_dir).each do |filename|
    if filename =~ /^\d+/
      path = File.join(tmp_upload_dir, filename)
      FileUtils.rm path if Card.older_than_five_days? File.ctime(path)
    end
  end
end

#create_versions?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 132

def create_versions?
  true
end

#delete_files_for_action(action) ⇒ Object



233
234
235
236
237
238
239
240
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 233

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

#empty_ok?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 148

def empty_ok?
  @empty_ok
end

#file_ready_to_save?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 68

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

#item_names(_args = {}) ⇒ Object

needed for flexmail attachments. hacky.



108
109
110
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 108

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

#load_from_modObject



158
159
160
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 158

def load_from_mod
  @mod
end

#load_from_mod=(value) ⇒ Object



152
153
154
155
156
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 152

def load_from_mod= value
  @mod = value
  write_identifier
  @store_in_mod = true if value
end

#mod_dirObject

place for files of mod file cards



193
194
195
196
197
198
199
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 193

def mod_dir
  mod = @mod || mod_file?
  Card.paths["mod"].to_a.each do |mod_path|
    dir = File.join(mod_path, mod, "file", codename)
    return dir if Dir.exist? dir
  end
end

#mod_file?Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 201

def mod_file?
  return @mod if @store_in_mod
  # when db_content was changed assume that it's no longer a mod file
  return if db_content_changed? || !content.present?
  case content
  when %r{^:[^/]+/([^.]+)} then Regexp.last_match(1) # current mod_file format
  when /^\~/               then false  # current id file format
  else
    if (lines = content.split("\n")) && (lines.size == 4)
      # old format, still used in card_changes.
      lines.last
    end
  end
end

#original_filenameObject



112
113
114
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 112

def original_filename
  attachment.original_filename
end

#preliminary_upload?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 120

def preliminary_upload?
  Card::Env && Card::Env.params[:attachment_upload]
end

#retrieve_dirObject



170
171
172
173
174
175
176
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 170

def retrieve_dir
  if mod_file?
    mod_dir
  else
    upload_dir
  end
end

#save_preliminary_upload?Boolean

Returns:

  • (Boolean)


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

def save_preliminary_upload?
  @action_id_of_cached_upload.present?
end

#store_dirObject



162
163
164
165
166
167
168
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 162

def store_dir
  if @store_in_mod
    mod_dir
  else
    upload_dir
  end
end

create filesystem links to files from prior action



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 243

def symlink_to prior_action_id
  return unless prior_action_id != last_action_id
  save_action_id = selected_action_id
  links = {}

  self.selected_action_id = prior_action_id
  attachment.versions.each do |name, version|
    links[name] = version.store_path
  end
  original = attachment.store_path

  self.selected_action_id = last_action_id
  attachment.versions.each do |name, version|
    ::File.symlink links[name], version.store_path
  end
  ::File.symlink original, attachment.store_path

  self.selected_action_id = save_action_id
end

#tmp_upload_dir(_action_id = nil) ⇒ Object

place for files if card doesn't have an id yet



188
189
190
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 188

def tmp_upload_dir _action_id=nil
  "#{Card.paths['files'].existent.first}/#{upload_cache_card.id}"
end

#unfilled?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 116

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

#upload_cache_cardObject

used for uploads for new cards until the new card is created



137
138
139
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 137

def upload_cache_card
  @upload_cache_card ||= Card["new_#{attachment_name}".to_sym]
end

#upload_dirObject

place for files of regular file cards



179
180
181
182
183
184
185
# File 'tmpsets/set/mod013-carrierwave/abstract/attachment.rb', line 179

def upload_dir
  if id
    "#{Card.paths['files'].existent.first}/#{id}"
  else
    tmp_upload_dir
  end
end